diff --git a/discordbot.py b/discordbot.py index 057cc699..7f776083 100644 --- a/discordbot.py +++ b/discordbot.py @@ -1,16 +1,13 @@ import datetime - import discord import discord.opus import functools -from discord.ext import commands import db -import re import errors +import youtube_dl # Init the event loop import asyncio - loop = asyncio.get_event_loop() # Init the config reader @@ -23,6 +20,7 @@ client = discord.Client() discord.opus.load_opus("libopus-0.dll") voice_client = None voice_player = None +voice_queue = asyncio.Queue() async def find_user(user: discord.User): @@ -31,6 +29,31 @@ async def find_user(user: discord.User): return user +def create_music_embed(ytdl_data: dict): + embed = discord.Embed(type="rich", + title=ytdl_data['title'] if 'title' in ytdl_data else None, + url=ytdl_data['webpage_url'] if 'webpage_url' in ytdl_data else None, + colour=discord.Colour(13375518)) + # Uploader + if "uploader" in ytdl_data and ytdl_data["uploader"] is not None: + embed.set_author(name=ytdl_data["uploader"], url=ytdl_data["uploader_url"] if "uploader_url" in ytdl_data else None) + # Thumbnail + if "thumbnail" in ytdl_data: + embed.set_thumbnail(url=ytdl_data["thumbnail"]) + # Duration + embed.add_field(name="Durata", value=str(datetime.timedelta(seconds=ytdl_data["duration"]))) + # Views + if "view_count" in ytdl_data and ytdl_data["view_count"] is not None: + embed.add_field(name="Visualizzazioni", value="{:_}".format(ytdl_data["view_count"]).replace("_", " ")) + # Likes + if "like_count" in ytdl_data and ytdl_data["like_count"] is not None: + embed.add_field(name="Mi piace", value="{:_}".format(ytdl_data["like_count"]).replace("_", " ")) + # Dislikes + if "dislike_count" in ytdl_data and ytdl_data["dislike_count"] is not None: + embed.add_field(name="Non mi piace", value="{:_}".format(ytdl_data["dislike_count"]).replace("_", " ")) + return embed + + @client.event async def on_message(message: discord.Message): if message.content.startswith("!register"): @@ -39,7 +62,8 @@ async def on_message(message: discord.Message): try: username = message.content.split(" ", 1)[1] except IndexError: - await client.send_message(message.channel, "⚠️ Non hai specificato un username!") + await client.send_message(message.channel, "⚠️ Non hai specificato un username!\n" + "Sintassi corretta: `!register `") return try: d = db.Discord.create(session, @@ -60,70 +84,72 @@ async def on_message(message: discord.Message): global voice_client voice_client = await client.join_voice_channel(message.author.voice.voice_channel) await client.send_message(message.channel, f"✅ Mi sono connesso in <#{message.author.voice.voice_channel.id}>.") - elif message.content.startswith("!play"): - # Display typing status + elif message.content.startswith("!madd"): await client.send_typing(message.channel) - # Check if the bot is connected to voice chat - if voice_client is None or not voice_client.is_connected(): - await client.send_message(message.channel, f"⚠ Il bot non è connesso in nessun canale.") - return - # Get the URL from the message + # The bot should be in voice chat + if voice_client is None: + await client.send_message(message.channel, "⚠️ Non sono connesso alla cv!\n" + "Fammi entrare scrivendo `!cv` mentre sei in chat vocale.") + # Find the sent url try: url = message.content.split(" ", 1)[1] except IndexError: - await client.send_message(message.channel, "⚠️ Non hai specificato un URL.") + await client.send_message(message.channel, "⚠️ Non hai specificato un url!\n" + "Sintassi corretta: `!madd