diff --git a/discordbot.py b/discordbot.py index 6cf7a9c1..ce4a18e7 100644 --- a/discordbot.py +++ b/discordbot.py @@ -8,7 +8,6 @@ import sys import db import youtube_dl import concurrent.futures -import platform import typing import os import asyncio @@ -43,570 +42,6 @@ config.read("config.ini") # Radio messages radio_messages = ["https://www.youtube.com/watch?v=3-yeK1Ck4yk"] -radio_messages_enabled = False -radio_message_in = int(config["Discord"]["radio_messages_every"]) - -# noinspection PyUnreachableCode -if __debug__: - version = "discord-py-rewrite" - commit_msg = "_Aggiornamento di Discordbot all'APIv6_" -else: - # Find the latest git tag - old_wd = os.getcwd() - try: - os.chdir(os.path.dirname(__file__)) - version = str(subprocess.check_output(["git", "describe", "--tags"]), encoding="utf8").strip() - commit_msg = str(subprocess.check_output(["git", "log", "-1", "--pretty=%B"]), encoding="utf8").strip() - except Exception: - version = "❓" - finally: - os.chdir(old_wd) - -# FFmpeg settings -ffmpeg_settings = {} - - -class DurationError(Exception): - pass - - -class InfoNotRetrievedError(Exception): - pass - - -class FileNotDownloadedError(Exception): - pass - - -class AlreadyDownloadedError(Exception): - pass - - -class InvalidConfigError(Exception): - pass - - -class Video: - def __init__(self, url: str=None, file: str=None, info: dict=None, enqueuer: discord.Member=None): - self.url = url - if file is None and info is None: - self.file = str(hash(url)) + ".opus" - elif info is not None: - self.file = re.sub(r'[/\\?*"<>|!:]', "_", info["title"]) + ".opus" - else: - self.file = file - self.downloaded = False if file is None else True - self.info = info - self.enqueuer = enqueuer - - def __str__(self): - if self.info is None or "title" not in self.info: - return f"`{self.file}`" - return f"_{self.info['title']}_" - - def plain_text(self): - if self.info is None or "title" not in self.info: - return self.file - return self.info['title'] - - async def download(self, progress_hooks: typing.List["function"]=None): - # File already downloaded - if self.downloaded: - raise AlreadyDownloadedError() - # No progress hooks - if progress_hooks is None: - progress_hooks = [] - # Check if under max duration - if self.info is not None and self.info.get("duration", 0) > int(config["YouTube"]["max_duration"]): - raise DurationError() - # Download the file - with youtube_dl.YoutubeDL({"noplaylist": True, - "format": "best", - "postprocessors": [{ - "key": 'FFmpegExtractAudio', - "preferredcodec": 'opus' - }], - "outtmpl": f"./opusfiles/{self.file}", - "progress_hooks": progress_hooks, - "quiet": True}) as ytdl: - await loop.run_in_executor(executor, functools.partial(ytdl.download, [self.url])) - self.downloaded = True - - def create_player(self) -> discord.PCMVolumeTransformer: - # Check if the file has been downloaded - if not self.downloaded: - raise FileNotDownloadedError() - return discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(f"./opusfiles/{self.file}", **ffmpeg_settings)) - - -class RoyalDiscordBot(discord.Client): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.main_channel: typing.Optional[discord.TextChannel] = None - self.main_guild: typing.Optional[discord.Guild] = None - - async def on_ready(self): - # Get the main channel - self.main_channel = self.get_channel(int(config["Discord"]["main_channel"])) - if not isinstance(self.main_channel, discord.TextChannel): - raise InvalidConfigError("The main channel is not a TextChannel!") - # Get the main guild - self.main_guild = self.get_guild(int(config["Discord"]["server_id"])) - if not isinstance(self.main_guild, discord.Guild): - raise InvalidConfigError("The main guild does not exist!") - await self.main_channel.send(f"ℹ Royal Bot avviato e pronto a ricevere comandi!\n" - f"Ultimo aggiornamento: `{version}: {commit_msg}`") - await self.change_presence(status=discord.Status.online, activity=None) - - async def on_message(self, message: discord.Message): - if message.channel != self.main_channel or message.author.bot: - return - sentry.user_context({ - "discord": { - "discord_id": message.author.id, - "name": message.author.name, - "discriminator": message.author.discriminator - } - }) - if not message.content.startswith("!"): - await message.channel.send(f":warning: In questa chat sono consentiti solo comandi per il bot.\n" - f"Riinvia il tuo messaggio in un altro canale!") - await message.delete() - return - data = message.content.split(" ") - if data[0] not in commands: - await message.channel.send(":warning: Comando non riconosciuto.") - return - await commands[data[0]](channel=message.channel, - author=message.author, - params=data) - - async def on_error(self, event_method, *args, **kwargs): - ei = sys.exc_info() - print("ERRORE CRITICO:\n" + repr(ei[1]) + "\n\n" + repr(ei)) - try: - await self.main_channel.send(f"☢️ **ERRORE CRITICO NELL'EVENTO** `{event_method}`\n" - f"Il bot si è chiuso e si dovrebbe riavviare entro qualche minuto.\n" - f"Una segnalazione di errore è stata automaticamente mandata a Steffo.\n\n" - f"Dettagli dell'errore:\n" - f"```python\n" - f"{repr(ei[1])}\n" - f"```") - await self.change_presence(status=discord.Status.invisible) - await self.close() - except Exception as e: - print("ERRORE CRITICO PIU' CRITICO:\n" + repr(e) + "\n\n" + repr(sys.exc_info())) - loop.stop() - sentry.captureException(exc_info=ei) - exit(1) - - async def feed_pipe(self, connection): - await self.wait_until_ready() - while True: - msg = await loop.run_in_executor(executor, connection.recv) - if msg == "get cv": - discord_members = list(self.main_guild.members) - connection.send(discord_members) - elif msg == "stop": - await self.logout() - exit(0) - elif msg.startswith("!"): - data = msg.split(" ") - if data[0] not in commands: - connection.send("error") - continue - await commands[data[0]](channel=self.get_channel(config["Discord"]["main_channel"]), - author=None, - params=data) - connection.send("success") - - -# Init the executor -executor = concurrent.futures.ThreadPoolExecutor(max_workers=3) - -# Init the Sentry client -sentry = raven.Client(config["Sentry"]["token"], - release=version, - install_logging_hook=False, - hook_libraries=[]) - - -def command(func): - """Decorator. Runs the function as a Discord command.""" - async def new_func(channel: discord.TextChannel, author: discord.Member, params: typing.List[str], *args, **kwargs): - if author is not None: - sentry.user_context({ - "discord_id": author.id, - "username": f"{author.name}#{author.discriminator}" - }) - else: - sentry.user_context({ - "source": "Telegram" - }) - try: - result = await func(channel=channel, author=author, params=params, *args, **kwargs) - except Exception: - ei = sys.exc_info() - try: - await channel.send(f"☢ **ERRORE DURANTE L'ESECUZIONE DEL COMANDO {params[0]}**\n" - f"Il comando è stato ignorato.\n" - f"Una segnalazione di errore è stata automaticamente mandata a Steffo.\n\n" - f"Dettagli dell'errore:\n" - f"```python\n" - f"{repr(ei[1])}\n" - f"```") - except Exception: - pass - sentry.captureException(exc_info=ei) - else: - return result - return new_func - - -# def requires_voice_client(func): -# "Decorator. Ensures the voice client is connected before running the command." -# async def new_func(channel: discord.TextChannel, author: discord.Member, params: typing.List[str], *args, **kwargs): -# global voice_client -# if voice_client is None or not voice_client.is_connected(): -# await client.send_message(channel, -# "⚠️ Non sono connesso alla cv!\n" -# "Fammi entrare scrivendo `!cv` mentre sei in chat vocale.") -# return -# return await func(channel=channel, author=author, params=params, *args, **kwargs) -# return new_func - - -def requires_rygdb(func, optional=False): - async def new_func(channel: discord.TextChannel, author: discord.Member, params: typing.List[str], *args, **kwargs): - session = db.Session() - dbuser = await loop.run_in_executor(executor, - session.query(db.Discord) - .filter_by(discord_id=author.id) - .join(db.Royal) - .first) - await loop.run_in_executor(executor, session.close) - if not optional and dbuser is None: - await channel.send("⚠️ Devi essere registrato su Royalnet per poter utilizzare questo comando.") - return - return await func(channel=channel, author=author, params=params, dbuser=dbuser, *args, **kwargs) - return new_func - - -@command -async def cmd_ping(channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): - await channel.send(f"Pong!") - - -# @command -# async def cmd_cv(channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): -# if author is None: -# await channel.send("⚠ Questo comando richiede un autore.") -# return -# if author.voice is None or author.voice.voice_channel is None: -# await channel.send("⚠ Non sei in nessun canale!") -# return -# if voice_client is not None and voice_client.is_connected(): -# await voice_client.move_to(author.voice.voice_channel) -# else: -# voice_client = await client.join_voice_channel(author.voice.voice_channel) -# await client.send_message(channel, f"✅ Mi sono connesso in <#{author.voice.voice_channel.id}>.") - - -# async def add_video_from_url(url, index: typing.Optional[int]=None, enqueuer: discord.Member=None): -# # Retrieve info -# with youtube_dl.YoutubeDL({"quiet": True, -# "ignoreerrors": True, -# "simulate": True}) as ytdl: -# info = await loop.run_in_executor(executor, -# functools.partial(ytdl.extract_info, url=url, download=False)) -# if info is None: -# await client.send_message(client.get_channel(config["Discord"]["main_channel"]), -# f"⚠ Non è stato trovato nessun video all'URL `{url}`," -# f" pertanto non è stato aggiunto alla coda.") -# return -# if "entries" in info: -# # This is a playlist -# for entry in info["entries"]: -# if index is not None: -# voice_queue.insert(index, Video(url=entry["webpage_url"], info=entry, enqueuer=enqueuer)) -# else: -# voice_queue.append(Video(url=entry["webpage_url"], info=entry, enqueuer=enqueuer)) -# return -# # This is a single video -# if index is not None: -# voice_queue.insert(index, Video(url=url, info=info, enqueuer=enqueuer)) -# else: -# voice_queue.append(Video(url=url, info=info, enqueuer=enqueuer)) - - -# async def add_video_from_file(file, index: typing.Optional[int]=None, enqueuer: discord.Member=None): -# if index is not None: -# voice_queue.insert(index, Video(file=file, enqueuer=enqueuer)) -# else: -# voice_queue.append(Video(file=file, enqueuer=enqueuer)) - - -# @command -# @requires_voice_client -# async def cmd_play(channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): -# if len(params) < 2: -# await client.send_message(channel, "⚠ Non hai specificato una canzone da riprodurre!\n" -# "Sintassi: `!play `") -# return -# # If the radio messages are enabled... -# global radio_messages_enabled -# if radio_messages_enabled: -# global radio_message_in -# radio_message_in -= 1 -# if radio_message_in <= 0: -# radio_message = random.sample(radio_messages, 1)[0] -# radio_message_in = int(config["Discord"]["radio_messages_every"]) -# await add_video_from_url(radio_message) -# await client.send_message(channel, f"✅ Aggiunto un messaggio radio, disattiva con `!radiomessages off`.") -# # Parse the parameter as URL -# url = re.match(r"(?:https?://|ytsearch[0-9]*:).*", " ".join(params[1:]).strip("<>")) -# if url is not None: -# # This is a url -# await add_video_from_url(url.group(0), enqueuer=author) -# await client.send_message(channel, f"✅ Video aggiunto alla coda.") -# return -# # Parse the parameter as file -# file_path = os.path.join(os.path.join(os.path.curdir, "opusfiles"), " ".join(params[1:])) -# if os.path.exists(file_path): -# # This is a file -# await add_video_from_file(file=file_path, enqueuer=author) -# await client.send_message(channel, f"✅ Video aggiunto alla coda.") -# return -# file_path += ".opus" -# if os.path.exists(file_path): -# # This is a file -# await add_video_from_file(file=file_path, enqueuer=author) -# await client.send_message(channel, f"✅ Video aggiunto alla coda.") -# return -# # Search the parameter on youtube -# search = " ".join(params[1:]) -# # This is a search -# await add_video_from_url(url=f"ytsearch:{search}", enqueuer=author) -# await client.send_message(channel, f"✅ Video aggiunto alla coda.") - - -# @command -# @requires_voice_client -# async def cmd_skip(channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): -# global voice_player -# if voice_player is None: -# await client.send_message(channel, "⚠ Non c'è nessun video in riproduzione.") -# return -# voice_player.stop() -# await client.send_message(channel, f"⏩ Video saltato.") - - -# @command -# @requires_voice_client -# async def cmd_remove(channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): -# if len(voice_queue) == 0: -# await client.send_message(channel, "⚠ Non c'è nessun video in coda.") -# return -# if len(params) == 1: -# index = len(voice_queue) - 1 -# else: -# try: -# index = int(params[1]) - 1 -# except ValueError: -# await client.send_message(channel, "⚠ Il numero inserito non è valido.\n" -# "Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`") -# return -# if len(params) < 3: -# if abs(index) >= len(voice_queue): -# await client.send_message(channel, "⚠ Il numero inserito non corrisponde a nessun video nella playlist.\n" -# "Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`") -# return -# video = voice_queue.pop(index) -# await client.send_message(channel, f":regional_indicator_x: {str(video)} è stato rimosso dalla coda.") -# return -# try: -# start = int(params[1]) - 1 -# except ValueError: -# await client.send_message(channel, "⚠ Il numero iniziale inserito non è valido.\n" -# "Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`") -# return -# if start >= len(voice_queue): -# await client.send_message(channel, "⚠ Il numero iniziale inserito non corrisponde a nessun video nella" -# " playlist.\n" -# "Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`") -# return -# try: -# end = int(params[2]) - 2 -# except ValueError: -# await client.send_message(channel, "⚠ Il numero finale inserito non è valido.\n" -# "Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`") -# return -# if end >= len(voice_queue): -# await client.send_message(channel, "⚠ Il numero finale inserito non corrisponde a nessun video nella" -# " playlist.\n" -# "Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`") -# return -# if start > end: -# await client.send_message(channel, "⚠ Il numero iniziale è maggiore del numero finale.\n" -# "Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`") -# return -# del voice_queue[start:end] -# await client.send_message(channel, f":regional_indicator_x: {end - start} video rimossi dalla coda.") - - -# @command -# async def cmd_queue(channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): -# if len(voice_queue) == 0: -# await client.send_message(channel, "**Video in coda:**\n" -# "nessuno") -# return -# msg = "**Video in coda:**\n" -# for index, video in enumerate(voice_queue[:10]): -# msg += f"{queue_emojis[index]} {str(video)}\n" -# if len(voice_queue) > 10: -# msg += f"più altri {len(voice_queue) - 10} video!" -# await client.send_message(channel, msg) - - -# @command -# @requires_voice_client -# async def cmd_shuffle(channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): -# if len(voice_queue) == 0: -# await client.send_message(channel, "⚠ Non ci sono video in coda!") -# return -# random.shuffle(voice_queue) -# await client.send_message(channel, "♠️ ♦️ ♣️ ♥️ Shuffle completo!") - - -# @command -# @requires_voice_client -# async def cmd_clear(channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): -# global voice_queue -# if len(voice_queue) == 0: -# await client.send_message(channel, "⚠ Non ci sono video in coda!") -# return -# voice_queue = [] -# await client.send_message(channel, ":regional_indicator_x: Tutti i video in coda rimossi.") - - -# @command -# @requires_voice_client -# async def cmd_dump_voice_player_error(channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): -# global voice_player -# if voice_player is None: -# return -# await client.send_message(channel, f"```\n{str(voice_player.error)}\n```") - - -@command -async def cmd_register(channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): - session = db.Session() - if len(params) < 1: - await channel.send("⚠️ Non hai specificato un username!\n" - "Sintassi corretta: `!register `") - return - try: - # noinspection PyTypeChecker - d = db.Discord.create(session, - royal_username=params[0], - discord_user=author) - except errors.AlreadyExistingError: - await channel.send("⚠ Il tuo account Discord è già collegato a un account RYG " - "o l'account RYG che hai specificato è già collegato a un account Discord.") - return - session.add(d) - session.commit() - session.close() - await channel.send("✅ Sincronizzazione completata!") - - -# @command -# @requires_voice_client -# async def cmd_forceplay(channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): -# if voice_player is not None: -# voice_player.stop() -# if len(params) < 2: -# await client.send_message(channel, "⚠ Non hai specificato una canzone da riprodurre!\n" -# "Sintassi: `!forceplay `") -# return -# # Parse the parameter as URL -# url = re.match(r"(?:https?://|ytsearch[0-9]*:).*", " ".join(params[1:]).strip("<>")) -# if url is not None: -# # This is a url -# await add_video_from_url(url.group(0), enqueuer=author, index=0) -# await client.send_message(channel, f"✅ Riproduzione del video forzata.") -# return -# # Parse the parameter as file -# file_path = os.path.join(os.path.join(os.path.curdir, "opusfiles"), " ".join(params[1:])) -# if os.path.exists(file_path): -# # This is a file -# await add_video_from_file(file=file_path, enqueuer=author, index=0) -# await client.send_message(channel, f"✅ Riproduzione del video forzata.") -# return -# file_path += ".opus" -# if os.path.exists(file_path): -# # This is a file -# await add_video_from_file(file=file_path, enqueuer=author, index=0) -# await client.send_message(channel, f"✅ Riproduzione del video forzata.") -# return -# # Search the parameter on youtube -# search = " ".join(params[1:]) -# # This is a search -# await add_video_from_url(url=f"ytsearch:{search}", enqueuer=author, index=0) -# await client.send_message(channel, f"✅ Riproduzione del video forzata.") - - -# @command -# async def cmd_radiomessages(channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): -# global radio_messages_enabled -# if len(params) < 2: -# radio_messages_enabled = not radio_messages_enabled -# else: -# if params[1].lower() == "on": -# radio_messages_enabled = True -# elif params[1].lower() == "off": -# radio_messages_enabled = False -# else: -# await client.send_message(channel, "⚠ Sintassi del comando non valida.\n" -# "Sintassi: `!radiomessages [on|off]`") -# return -# await client.send_message(channel, -# f"📻 Messaggi radio **{'attivati' if radio_messages_enabled else 'disattivati'}**.") - - -# async def queue_predownload_videos(): -# while True: -# for index, video in enumerate(voice_queue[:int(config["YouTube"]["predownload_videos"])].copy()): -# if video.downloaded: -# continue -# try: -# with async_timeout.timeout(int(config["YouTube"]["download_timeout"])): -# await video.download() -# except asyncio.TimeoutError: -# await client.send_message(client.get_channel(config["Discord"]["main_channel"]), -# f"⚠️ Il download di {str(video)} ha richiesto più di" -# f" {config['YouTube']['download_timeout']} secondi, pertanto è stato rimosso" -# f" dalla coda.") -# del voice_queue[index] -# continue -# except DurationError: -# await client.send_message(client.get_channel(config["Discord"]["main_channel"]), -# f"⚠️ {str(video)} dura più di" -# f" {str(int(config['YouTube']['max_duration']) // 60)}" -# f" minuti, quindi è stato rimosso dalla coda.") -# del voice_queue[index] -# continue -# except Exception as e: -# await client.send_message(client.get_channel(config["Discord"]["main_channel"]), -# f"⚠️ E' stato incontrato un errore durante il download di {str(video)}," -# f" quindi è stato rimosso dalla coda.\n\n" -# f"```python\n" -# f"{str(e)}" -# f"```") -# del voice_queue[index] -# continue -# await asyncio.sleep(1) - song_special_messages = { "despacito": ":arrow_forward: this is so sad. alexa play {song}", @@ -644,74 +79,641 @@ song_special_messages = { "jump up superstar": ":arrow_forward: Is {song} the Tengen Toppa Guren Lagann opening?" } +# noinspection PyUnreachableCode +if __debug__: + version = "discord-py-rewrite" + commit_msg = "_Aggiornamento di Discordbot all'APIv6_" +else: + # Find the latest git tag + old_wd = os.getcwd() + try: + os.chdir(os.path.dirname(__file__)) + version = str(subprocess.check_output(["git", "describe", "--tags"]), encoding="utf8").strip() + commit_msg = str(subprocess.check_output(["git", "log", "-1", "--pretty=%B"]), encoding="utf8").strip() + except Exception: + version = "❓" + finally: + os.chdir(old_wd) -# async def queue_play_next_video(): -# await client.wait_until_ready() -# global voice_client -# global voice_player -# global now_playing -# while True: -# if voice_client is None: -# await asyncio.sleep(1) -# continue -# if voice_player is not None and not voice_player.is_done(): -# await asyncio.sleep(0.5) -# continue -# if len(voice_queue) == 0: -# await asyncio.sleep(0.5) -# if now_playing is not None: -# await client.change_presence() -# now_playing = None -# continue -# now_playing = voice_queue[0] -# if not now_playing.downloaded: -# await asyncio.sleep(0.5) -# continue -# voice_player = await now_playing.create_player() -# voice_player.start() -# if now_playing.enqueuer is not None: -# session = db.Session() -# enqueuer = await loop.run_in_executor(executor, session.query(db.Discord).filter_by(discord_id=now_playing.enqueuer.id).one_or_none) -# played_music = db.PlayedMusic(enqueuer=enqueuer, -# filename=now_playing.plain_text(), -# timestamp=datetime.datetime.now()) -# session.add(played_music) -# await loop.run_in_executor(executor, session.commit) -# await loop.run_in_executor(executor, session.close) -# await client.change_presence(game=discord.Game(name=now_playing.plain_text(), type=2)) -# for key in song_special_messages: -# if key in now_playing.file.lower(): -# await client.send_message(client.get_channel(config["Discord"]["main_channel"]), -# song_special_messages[key].format(song=str(now_playing))) -# break -# else: -# await client.send_message(client.get_channel(config["Discord"]["main_channel"]), -# f":arrow_forward: Ora in riproduzione: {str(now_playing)}") -# del voice_queue[0] +# FFmpeg settings +ffmpeg_settings = {} + +# Init the executor +executor = concurrent.futures.ThreadPoolExecutor(max_workers=3) + +# Init the Sentry client +sentry = raven.Client(config["Sentry"]["token"], + release=version, + install_logging_hook=False, + hook_libraries=[]) -commands = { - "!ping": cmd_ping, - # "!cv": cmd_cv, - # "!summon": cmd_cv, - # "!play": cmd_play, - # "!p": cmd_play, - # "!search": cmd_play, - # "!file": cmd_play, - # "!skip": cmd_skip, - # "!s": cmd_skip, - # "!remove": cmd_remove, - # "!cancel": cmd_remove, - # "!queue": cmd_queue, - # "!q": cmd_queue, - # "!shuffle": cmd_shuffle, - # "!clear": cmd_clear, - # "!dump_vp": cmd_dump_voice_player_error, - "!register": cmd_register, - # "!forceplay": cmd_forceplay, - # "!fp": cmd_forceplay, - # "!radiomessages": cmd_radiomessages -} +class DurationError(Exception): + pass + + +class InfoNotRetrievedError(Exception): + pass + + +class FileNotDownloadedError(Exception): + pass + + +class AlreadyDownloadedError(Exception): + pass + + +class InvalidConfigError(Exception): + pass + + +class Video: + def __init__(self, url: str = None, file: str = None, info: dict = None, enqueuer: discord.Member = None): + self.url = url + if file is None and info is None: + self.file = str(hash(url)) + ".opus" + elif info is not None: + self.file = re.sub(r'[/\\?*"<>|!:]', "_", info["title"]) + ".opus" + else: + self.file = file + self.downloaded = False if file is None else True + self.info = info + self.enqueuer = enqueuer + self.duration = None + + def __str__(self): + if self.info is None or "title" not in self.info: + return f"`{self.file}`" + return f"_{self.info['title']}_" + + def plain_text(self): + if self.info is None or "title" not in self.info: + return self.file + return self.info['title'] + + async def download(self, progress_hooks: typing.List["function"] = None): + # File already downloaded + if self.downloaded: + raise AlreadyDownloadedError() + # No progress hooks + if progress_hooks is None: + progress_hooks = [] + # Check if under max duration + self.duration = datetime.timedelta(seconds=self.info.get("duration", 0)) + if self.info is not None and self.duration.total_seconds() > int(config["YouTube"]["max_duration"]): + raise DurationError() + # Download the file + with youtube_dl.YoutubeDL({"noplaylist": True, + "format": "best", + "postprocessors": [{ + "key": 'FFmpegExtractAudio', + "preferredcodec": 'opus' + }], + "outtmpl": f"./opusfiles/{self.file}", + "progress_hooks": progress_hooks, + "quiet": True}) as ytdl: + await loop.run_in_executor(executor, functools.partial(ytdl.download, [self.url])) + self.downloaded = True + + def create_audio_source(self) -> discord.PCMVolumeTransformer: + # Check if the file has been downloaded + if not self.downloaded: + raise FileNotDownloadedError() + return discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(f"./opusfiles/{self.file}", **ffmpeg_settings)) + + +def command(func): + """Decorator. Runs the function as a Discord command.""" + + async def new_func(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str], *args, + **kwargs): + if author is not None: + sentry.user_context({ + "discord_id": author.id, + "username": f"{author.name}#{author.discriminator}" + }) + else: + sentry.user_context({ + "source": "Telegram" + }) + try: + result = await func(self, channel=channel, author=author, params=params, *args, **kwargs) + except Exception: + ei = sys.exc_info() + try: + await channel.send(f"☢ **ERRORE DURANTE L'ESECUZIONE DEL COMANDO {params[0]}**\n" + f"Il comando è stato ignorato.\n" + f"Una segnalazione di errore è stata automaticamente mandata a Steffo.\n\n" + f"Dettagli dell'errore:\n" + f"```python\n" + f"{repr(ei[1])}\n" + f"```") + except Exception: + pass + sentry.captureException(exc_info=ei) + else: + return result + + return new_func + + +def requires_connected_voice_client(func): + """Decorator. Ensures the voice client is connected before running the command.""" + + async def new_func(self: "RoyalDiscordBot", channel: discord.TextChannel, author: discord.Member, + params: typing.List[str], *args, **kwargs): + for voice_client in self.voice_clients: + if voice_client.channel in self.main_guild.channels and voice_client.is_connected(): + break + else: + await channel.send("⚠️ Non sono connesso alla cv!\n" + "Fammi entrare scrivendo `!cv` mentre sei in chat vocale.") + return + return await func(self, channel=channel, author=author, params=params, *args, **kwargs) + + return new_func + + +def requires_rygdb(func, optional=False): + async def new_func(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str], *args, + **kwargs): + session = db.Session() + dbuser = await loop.run_in_executor(executor, + session.query(db.Discord) + .filter_by(discord_id=author.id) + .join(db.Royal) + .first) + await loop.run_in_executor(executor, session.close) + if not optional and dbuser is None: + await channel.send("⚠️ Devi essere registrato su Royalnet per poter utilizzare questo comando.") + return + return await func(self, channel=channel, author=author, params=params, dbuser=dbuser, *args, **kwargs) + + return new_func + + +class RoyalDiscordBot(discord.Client): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.main_channel: typing.Optional[discord.TextChannel] = None + self.main_guild: typing.Optional[discord.Guild] = None + self.commands = { + "!ping": self.cmd_ping, + "!cv": self.cmd_cv, + "!summon": self.cmd_cv, + "!play": self.cmd_play, + "!p": self.cmd_play, + "!search": self.cmd_play, + "!file": self.cmd_play, + "!skip": self.cmd_skip, + "!s": self.cmd_skip, + "!remove": self.cmd_remove, + "!cancel": self.cmd_remove, + "!queue": self.cmd_queue, + "!q": self.cmd_queue, + "!shuffle": self.cmd_shuffle, + "!clear": self.cmd_clear, + "!register": self.cmd_register, + "!forceplay": self.cmd_forceplay, + "!fp": self.cmd_forceplay, + "!radiomessages": self.cmd_radiomessages, + "!yes": self.null, + "!no": self.null + } + self.video_queue: typing.List[Video] = [] + self.now_playing = None + self.radio_messages = False + self.next_radio_message_in = int(config["Discord"]["radio_messages_every"]) + asyncio.ensure_future(self.queue_predownload_videos()) + asyncio.ensure_future(self.queue_play_next_video()) + + async def on_ready(self): + # Get the main channel + self.main_channel = self.get_channel(int(config["Discord"]["main_channel"])) + if not isinstance(self.main_channel, discord.TextChannel): + raise InvalidConfigError("The main channel is not a TextChannel!") + # Get the main guild + self.main_guild = self.get_guild(int(config["Discord"]["server_id"])) + if not isinstance(self.main_guild, discord.Guild): + raise InvalidConfigError("The main guild does not exist!") + await self.main_channel.send(f"ℹ Royal Bot avviato e pronto a ricevere comandi!\n" + f"Ultimo aggiornamento: `{version}: {commit_msg}`") + await self.change_presence(status=discord.Status.online, activity=None) + + async def on_message(self, message: discord.Message): + if message.channel != self.main_channel or message.author.bot: + return + sentry.user_context({ + "discord": { + "discord_id": message.author.id, + "name": message.author.name, + "discriminator": message.author.discriminator + } + }) + if not message.content.startswith("!"): + await message.channel.send(f":warning: In questa chat sono consentiti solo comandi per il bot.\n" + f"Riinvia il tuo messaggio in un altro canale!") + await message.delete() + return + data = message.content.split(" ") + if data[0] not in self.commands: + await message.channel.send(":warning: Comando non riconosciuto.") + return + await self.commands[data[0]](channel=message.channel, + author=message.author, + params=data) + + async def on_error(self, event_method, *args, **kwargs): + ei = sys.exc_info() + print("ERRORE CRITICO:\n" + repr(ei[1]) + "\n\n" + repr(ei)) + try: + await self.main_channel.send(f"☢️ **ERRORE CRITICO NELL'EVENTO** `{event_method}`\n" + f"Il bot si è chiuso e si dovrebbe riavviare entro qualche minuto.\n" + f"Una segnalazione di errore è stata automaticamente mandata a Steffo.\n\n" + f"Dettagli dell'errore:\n" + f"```python\n" + f"{repr(ei[1])}\n" + f"```") + await self.change_presence(status=discord.Status.invisible) + await self.close() + except Exception as e: + print("ERRORE CRITICO PIU' CRITICO:\n" + repr(e) + "\n\n" + repr(sys.exc_info())) + loop.stop() + sentry.captureException(exc_info=ei) + exit(1) + + async def feed_pipe(self, connection): + await self.wait_until_ready() + while True: + msg = await loop.run_in_executor(executor, connection.recv) + if msg == "get cv": + discord_members = list(self.main_guild.members) + connection.send(discord_members) + elif msg == "stop": + await self.logout() + exit(0) + elif msg.startswith("!"): + data = msg.split(" ") + if data[0] not in self.commands: + connection.send("error") + continue + await self.main_channel.send(f"{msg}\n" + f"_(da Telegram)_") + await self.commands[data[0]](channel=self.get_channel(config["Discord"]["main_channel"]), + author=None, + params=data) + connection.send("success") + + async def queue_predownload_videos(self): + while True: + for index, video in enumerate(self.video_queue[:int(config["YouTube"]["predownload_videos"])].copy()): + if video.downloaded: + continue + try: + with async_timeout.timeout(int(config["YouTube"]["download_timeout"])): + await video.download() + except asyncio.TimeoutError: + await self.main_channel.send(f"⚠️ Il download di {str(video)} ha richiesto più di" + f" {config['YouTube']['download_timeout']} secondi, pertanto è stato" + f" rimosso dalla coda.") + del self.video_queue[index] + continue + except DurationError: + await self.main_channel.send(f"⚠️ {str(video)} dura più di" + f" {str(int(config['YouTube']['max_duration']) // 60)}" + f" minuti, quindi è stato rimosso dalla coda.") + del self.video_queue[index] + continue + except Exception as e: + await self.main_channel.send(f"⚠️ E' stato incontrato un errore durante il download di " + f"{str(video)}, quindi è stato rimosso dalla coda.\n\n" + f"**Dettagli sull'errore:**\n" + f"```python\n" + f"{str(e)}" + f"```") + del self.video_queue[index] + continue + await asyncio.sleep(1) + + async def queue_play_next_video(self): + await self.wait_until_ready() + while True: + # Fun things will happen with multiple voice clients! + for voice_client in self.voice_clients: + if not voice_client.is_connected() \ + or voice_client.is_playing(): + continue + if len(self.video_queue) == 0: + self.now_playing = None + await self.change_presence() + continue + now_playing = self.video_queue[0] + try: + audio_source = now_playing.create_audio_source() + except FileNotDownloadedError: + continue + voice_client.play(audio_source) + del self.video_queue[0] + activity = discord.Activity(name=now_playing.plain_text(), + type=discord.ActivityType.playing) + await self.change_presence(status=discord.Status.online, activity=activity) + if now_playing.enqueuer is not None: + session = db.Session() + enqueuer = await loop.run_in_executor(executor, session.query(db.Discord) + .filter_by(discord_id=now_playing.enqueuer.id) + .one_or_none) + played_music = db.PlayedMusic(enqueuer=enqueuer, + filename=now_playing.plain_text(), + timestamp=datetime.datetime.now()) + session.add(played_music) + await loop.run_in_executor(executor, session.commit) + await loop.run_in_executor(executor, session.close) + for key in song_special_messages: + if key in now_playing.file.lower(): + await self.main_channel.send(song_special_messages[key].format(song=str(now_playing))) + break + else: + await self.main_channel.send(f":arrow_forward: Ora in riproduzione: {str(now_playing)}") + await asyncio.sleep(1) + + async def add_video_from_url(self, url, index: typing.Optional[int] = None, enqueuer: discord.Member = None): + # Retrieve info + with youtube_dl.YoutubeDL({"quiet": True, + "ignoreerrors": True, + "simulate": True}) as ytdl: + info = await loop.run_in_executor(executor, + functools.partial(ytdl.extract_info, url=url, download=False)) + if info is None: + await self.main_channel.send(f"⚠ Non è stato trovato nessun video all'URL `{url}`," + f" pertanto non è stato aggiunto alla coda.") + return + if "entries" in info: + # This is a playlist + for entry in info["entries"]: + if index is not None: + self.video_queue.insert(index, Video(url=entry["webpage_url"], info=entry, enqueuer=enqueuer)) + else: + self.video_queue.append(Video(url=entry["webpage_url"], info=entry, enqueuer=enqueuer)) + return + # This is a single video + if index is not None: + self.video_queue.insert(index, Video(url=url, info=info, enqueuer=enqueuer)) + else: + self.video_queue.append(Video(url=url, info=info, enqueuer=enqueuer)) + + async def add_video_from_file(self, file, index: typing.Optional[int] = None, enqueuer: discord.Member = None): + if index is not None: + self.video_queue.insert(index, Video(file=file, enqueuer=enqueuer)) + else: + self.video_queue.append(Video(file=file, enqueuer=enqueuer)) + + @command + async def null(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): + pass + + @command + async def cmd_ping(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): + await channel.send(f"Pong!") + + @command + async def cmd_register(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): + session = db.Session() + if len(params) < 1: + await channel.send("⚠️ Non hai specificato un username!\n" + "Sintassi corretta: `!register `") + return + try: + # noinspection PyTypeChecker + d = db.Discord.create(session, + royal_username=params[0], + discord_user=author) + except errors.AlreadyExistingError: + await channel.send("⚠ Il tuo account Discord è già collegato a un account RYG " + "o l'account RYG che hai specificato è già collegato a un account Discord.") + return + session.add(d) + session.commit() + session.close() + await channel.send("✅ Sincronizzazione completata!") + + @command + async def cmd_cv(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): + """Summon the bot in the author's voice channel.""" + if author is None: + await channel.send("⚠ Questo comando richiede un autore.") + return + if author.voice is None: + await channel.send("⚠ Non sei in nessun canale!") + return + if author.voice.channel == self.main_guild.afk_channel: + await channel.send("⚠ Non posso connettermi al canale AFK!") + return + if author.voice.channel.bitrate < 64000: + await channel.send("ℹ️ Sei in un canale con un bitrate ridotto.\n" + "L'utilizzo del bot in quel canale ignorerà il limite di bitrate e potrebbe causare lag" + " o eccessivo consumo di dati.\n" + "Se vuoi procedere comunque, scrivi `!yes`.") + try: + await self.wait_for("message", check=lambda m: m.content == "!yes", timeout=10.0) + except asyncio.TimeoutError: + return + # Check if there's already a connected client + for voice_client in self.voice_clients: + if voice_client.channel in self.main_guild.channels and voice_client.is_connected(): + await voice_client.move_to(author.voice.channel) + await channel.send(f"⤵️ Mi sono spostato in <#{author.voice.channel.id}>.") + break + else: + await author.voice.channel.connect() + await channel.send(f"⤵️ Mi sono connesso in <#{author.voice.channel.id}>.") + + @command + @requires_connected_voice_client + async def cmd_play(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): + if len(params) < 2: + await channel.send("⚠ Non hai specificato una canzone da riprodurre!\n" + "Sintassi: `!play `") + return + channel.typing() + # If the radio messages are enabled... + if self.radio_messages: + self.next_radio_message_in -= 1 + if self.next_radio_message_in <= 0: + radio_message = random.sample(radio_messages, 1)[0] + self.next_radio_message_in = int(config["Discord"]["radio_messages_every"]) + await self.add_video_from_url(radio_message) + await channel.send(f"📻 Aggiunto un messaggio radio, disattiva con `!radiomessages off`.") + # Parse the parameter as URL + url = re.match(r"(?:https?://|ytsearch[0-9]*:).*", " ".join(params[1:]).strip("<>")) + if url is not None: + # This is a url + await self.add_video_from_url(url.group(0), enqueuer=author) + await channel.send(f"✅ Video aggiunto alla coda.") + return + # Parse the parameter as file + file_path = os.path.join(os.path.join(os.path.curdir, "opusfiles"), " ".join(params[1:])) + if os.path.exists(file_path): + # This is a file + await self.add_video_from_file(file=file_path, enqueuer=author) + await channel.send(f"✅ Video aggiunto alla coda.") + return + file_path += ".opus" + if os.path.exists(file_path): + # This is a file + await self.add_video_from_file(file=file_path, enqueuer=author) + await channel.send(f"✅ Video aggiunto alla coda.") + return + # Search the parameter on youtube + search = " ".join(params[1:]) + # This is a search + await self.add_video_from_url(url=f"ytsearch:{search}", enqueuer=author) + await channel.send(f"✅ Video aggiunto alla coda.") + + @command + @requires_connected_voice_client + async def cmd_skip(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): + for voice_client in self.voice_clients: + if voice_client.is_playing(): + voice_client.stop() + await channel.send(f"⏩ Video saltato.") + break + else: + await channel.send("⚠ Non c'è nessun video in riproduzione.") + + @command + @requires_connected_voice_client + async def cmd_remove(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): + if len(self.video_queue) == 0: + await channel.send("⚠ Non c'è nessun video in coda.") + return + if len(params) == 1: + index = len(self.video_queue) - 1 + else: + try: + index = int(params[1]) - 1 + except ValueError: + await channel.send("⚠ Il numero inserito non è valido.\n" + "Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`") + return + if len(params) < 3: + if abs(index) >= len(self.video_queue): + await channel.send("⚠ Il numero inserito non corrisponde a nessun video nella playlist.\n" + "Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`") + return + video = self.video_queue.pop(index) + await channel.send(f":regional_indicator_x: {str(video)} è stato rimosso dalla coda.") + return + try: + start = int(params[1]) - 1 + except ValueError: + await channel.send("⚠ Il numero iniziale inserito non è valido.\n" + "Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`") + return + if start >= len(self.video_queue): + await channel.send("⚠ Il numero iniziale inserito non corrisponde a nessun video nella" + " playlist.\n" + "Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`") + return + try: + end = int(params[2]) - 2 + except ValueError: + await channel.send("⚠ Il numero finale inserito non è valido.\n" + "Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`") + return + if end >= len(self.video_queue): + await channel.send("⚠ Il numero finale inserito non corrisponde a nessun video nella" + " playlist.\n" + "Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`") + return + if start > end: + await channel.send("⚠ Il numero iniziale è maggiore del numero finale.\n" + "Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`") + return + del self.video_queue[start:end] + await channel.send(f":regional_indicator_x: {end - start} video rimossi dalla coda.") + + @command + async def cmd_queue(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): + if len(self.video_queue) == 0: + await channel.send("**Video in coda:**\n" + "nessuno") + return + msg = "**Video in coda:**\n" + for index, video in enumerate(self.video_queue[:10]): + msg += f"{queue_emojis[index]} {str(video)}\n" + if len(self.video_queue) > 10: + msg += f"più altri {len(self.video_queue) - 10} video!" + await channel.send(msg) + + @command + @requires_connected_voice_client + async def cmd_shuffle(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): + if len(self.video_queue) == 0: + await channel.send("⚠ Non ci sono video in coda!") + return + random.shuffle(self.video_queue) + await channel.send("♠️ ♦️ ♣️ ♥️ Shuffle completo!") + + @command + @requires_connected_voice_client + async def cmd_clear(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): + if len(self.video_queue) == 0: + await channel.send("⚠ Non ci sono video in coda!") + return + self.video_queue = [] + await channel.send(":regional_indicator_x: Tutti i video in coda rimossi.") + + @command + @requires_connected_voice_client + async def cmd_forceplay(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): + if len(params) < 2: + await channel.send("⚠ Non hai specificato una canzone da riprodurre!\n" + "Sintassi: `!forceplay `") + return + for voice_client in self.voice_clients: + if voice_client.is_playing(): + voice_client.stop() + # Parse the parameter as URL + url = re.match(r"(?:https?://|ytsearch[0-9]*:).*", " ".join(params[1:]).strip("<>")) + if url is not None: + # This is a url + await self.add_video_from_url(url.group(0), enqueuer=author, index=0) + await channel.send(f"✅ Riproduzione del video forzata.") + return + # Parse the parameter as file + file_path = os.path.join(os.path.join(os.path.curdir, "opusfiles"), " ".join(params[1:])) + if os.path.exists(file_path): + # This is a file + await self.add_video_from_file(file=file_path, enqueuer=author, index=0) + await channel.send(f"✅ Riproduzione del video forzata.") + return + file_path += ".opus" + if os.path.exists(file_path): + # This is a file + await self.add_video_from_file(file=file_path, enqueuer=author, index=0) + await channel.send(f"✅ Riproduzione del video forzata.") + return + # Search the parameter on youtube + search = " ".join(params[1:]) + # This is a search + await self.add_video_from_url(url=f"ytsearch:{search}", enqueuer=author, index=0) + await channel.send(f"✅ Riproduzione del video forzata.") + + @command + async def cmd_radiomessages(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]): + if len(params) < 2: + self.radio_messages = not self.radio_messages + else: + if params[1].lower() == "on": + self.radio_messages = True + elif params[1].lower() == "off": + self.radio_messages = False + else: + await channel.send("⚠ Sintassi del comando non valida.\n" + "Sintassi: `!radiomessages [on|off]`") + return + await channel.send(f"📻 Messaggi radio **{'attivati' if self.radio_messages else 'disattivati'}**.") def process(users_connection=None):