From 5603f40e9fceeeda8f8e4e45cdcc395daec00ebc Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 2 Dec 2019 21:25:56 +0100 Subject: [PATCH] Some new commands (that don't really work) --- royalpack/commands/__init__.py | 12 +++-- royalpack/commands/googlevideo.py | 13 ++++++ royalpack/commands/play.py | 8 +++- royalpack/commands/soundcloud.py | 74 +++---------------------------- royalpack/commands/yahoovideo.py | 13 ++++++ royalpack/commands/youtube.py | 71 ++--------------------------- 6 files changed, 50 insertions(+), 141 deletions(-) create mode 100644 royalpack/commands/googlevideo.py create mode 100644 royalpack/commands/yahoovideo.py diff --git a/royalpack/commands/__init__.py b/royalpack/commands/__init__.py index c0c2ea56..a0caa62f 100644 --- a/royalpack/commands/__init__.py +++ b/royalpack/commands/__init__.py @@ -16,14 +16,16 @@ from .play import PlayCommand # from .queue import QueueCommand from .skip import SkipCommand from .summon import SummonCommand -# from .youtube import YoutubeCommand -# from .soundcloud import SoundcloudCommand +from .youtube import YoutubeCommand +from .soundcloud import SoundcloudCommand # from .zawarudo import ZawarudoCommand from .emojify import EmojifyCommand from .leagueoflegends import LeagueoflegendsCommand from .diarioquote import DiarioquoteCommand # from .mp3 import Mp3Command from .peertube import PeertubeCommand +from .googlevideo import GooglevideoCommand +from .yahoovideo import YahoovideoCommand # Enter the commands of your Pack here! available_commands = [ @@ -44,14 +46,16 @@ available_commands = [ # QueueCommand, SkipCommand, SummonCommand, - # YoutubeCommand, - # SoundcloudCommand, + YoutubeCommand, + SoundcloudCommand, # ZawarudoCommand, EmojifyCommand, LeagueoflegendsCommand, DiarioquoteCommand, # Mp3Command, PeertubeCommand, + GooglevideoCommand, + YahoovideoCommand, ] # Don't change this, it should automatically generate __all__ diff --git a/royalpack/commands/googlevideo.py b/royalpack/commands/googlevideo.py new file mode 100644 index 00000000..f61bad56 --- /dev/null +++ b/royalpack/commands/googlevideo.py @@ -0,0 +1,13 @@ +from .play import PlayCommand + + +class GooglevideoCommand(PlayCommand): + name: str = "googlevideo" + + aliases = ["gv"] + + description: str = "Cerca un video su Google Video e lo aggiunge alla coda della chat vocale." + + syntax = "{ricerca}" + + _URL_FORMAT = "gvsearch:{url}" diff --git a/royalpack/commands/play.py b/royalpack/commands/play.py index 5889d4d8..7212467e 100644 --- a/royalpack/commands/play.py +++ b/royalpack/commands/play.py @@ -15,6 +15,8 @@ class PlayCommand(Command): syntax = "{url}" + _URL_FORMAT = "{url}" + async def run(self, args: CommandArgs, data: CommandData) -> None: url = args.joined() # if not (url.startswith("http://") or url.startswith("https://")): @@ -29,7 +31,8 @@ class PlayCommand(Command): else: guild_id = None response: Dict[str, Any] = await self.interface.call_herald_event("discord", "discord_play", - url=url, guild_id=guild_id) + url=self._URL_FORMAT.format(url=url), + guild_id=guild_id) too_long: List[Dict[str, Any]] = response["too_long"] if len(too_long) > 0: @@ -51,3 +54,6 @@ class PlayCommand(Command): else: reply += numberemojiformat([a["title"] for a in added]) await data.reply(reply) + + if len(added) + len(too_long) == 0: + raise ExternalError("Nessun video trovato.") \ No newline at end of file diff --git a/royalpack/commands/soundcloud.py b/royalpack/commands/soundcloud.py index 64ad73d2..372f4981 100644 --- a/royalpack/commands/soundcloud.py +++ b/royalpack/commands/soundcloud.py @@ -1,77 +1,13 @@ -import typing -import pickle -import datetime -import discord -from royalnet.commands import * -from royalnet.utils import asyncify -from royalnet.audio import YtdlDiscord -from royalnet.bots import DiscordBot +from .play import PlayCommand -class SoundcloudCommand(Command): +class SoundcloudCommand(PlayCommand): name: str = "soundcloud" aliases = ["sc"] - description: str = "Cerca una canzone su Soundcloud e la aggiunge alla coda della chat vocale." + description: str = "Cerca un video su SoundCloud e lo aggiunge alla coda della chat vocale." - syntax = "[ [guild] ] {url}" + syntax = "{ricerca}" - @staticmethod - async def _legacy_soundcloud_handler(bot: "DiscordBot", guild_name: typing.Optional[str], search: str): - # Find the matching guild - if guild_name: - guilds: typing.List[discord.Guild] = bot.client.find_guild_by_name(guild_name) - else: - guilds = bot.client.guilds - if len(guilds) == 0: - raise CommandError("Server non trovato.") - if len(guilds) > 1: - raise CommandError("Il nome del server è ambiguo.") - guild = list(bot.client.guilds)[0] - # Ensure the guild has a PlayMode before adding the file to it - if not bot.music_data.get(guild): - raise CommandError("Il bot non è in nessun canale vocale.") - # Create url - ytdl_args = { - "format": "bestaudio/best", - "outtmpl": f"./downloads/{datetime.datetime.now().timestamp()}_%(title)s.%(ext)s" - } - # Start downloading - dfiles: typing.List[YtdlDiscord] = await asyncify(YtdlDiscord.create_from_url, f'scsearch:{search}', - **ytdl_args) - await bot.add_to_music_data(dfiles, guild) - # Create response dictionary - return { - "videos": [{ - "title": dfile.info.title, - "discord_embed_pickle": str(pickle.dumps(dfile.info.to_discord_embed())) - } for dfile in dfiles] - } - - _event_name = "_legacy_soundcloud" - - def __init__(self, interface: CommandInterface): - super().__init__(interface) - if interface.name == "discord": - interface.register_herald_action(self._event_name, self._legacy_soundcloud_handler) - - async def run(self, args: CommandArgs, data: CommandData) -> None: - guild_name, search = args.match(r"(?:\[(.+)])?\s*?") - if search.startswith("http://") or search.startswith("https://"): - raise CommandError(f"Il comando [c]{self.interface.prefix}soundcloud[/c] funziona solo per cercare audio su" - f" Soundcloud con un dato nome.\n" - f"Se vuoi riprodurre una canzone da un URL, usa [c]{self.interface.prefix}play[/c]!") - response = await self.interface.call_herald_action("discord", self._event_name, { - "guild_name": guild_name, - "search": search - }) - if len(response["videos"]) == 0: - raise CommandError(f"Il video non può essere scaricato a causa di un blocco imposto da Soundcloud.") - for video in response["videos"]: - if self.interface.name == "discord": - # This is one of the unsafest things ever - embed = pickle.loads(eval(video["discord_embed_pickle"])) - await data.message.channel.send(content="▶️ Aggiunto alla coda:", embed=embed) - else: - await data.reply(f"▶️ Aggiunto alla coda: [i]{video['title']}[/i]") + _URL_FORMAT = "scsearch:{url}" diff --git a/royalpack/commands/yahoovideo.py b/royalpack/commands/yahoovideo.py new file mode 100644 index 00000000..9536b06a --- /dev/null +++ b/royalpack/commands/yahoovideo.py @@ -0,0 +1,13 @@ +from .play import PlayCommand + + +class YahoovideoCommand(PlayCommand): + name: str = "yahoovideo" + + aliases = ["yv"] + + description: str = "Cerca un video su Yahoo Video e lo aggiunge alla coda della chat vocale." + + syntax = "{ricerca}" + + _URL_FORMAT = "yvsearch:{url}" diff --git a/royalpack/commands/youtube.py b/royalpack/commands/youtube.py index 04f28fec..ca9743e6 100644 --- a/royalpack/commands/youtube.py +++ b/royalpack/commands/youtube.py @@ -1,76 +1,13 @@ -import typing -import pickle -import datetime -import discord -from royalnet.commands import * -from royalnet.utils import asyncify -from royalnet.audio import YtdlDiscord -from royalnet.bots import DiscordBot +from .play import PlayCommand -class YoutubeCommand(Command): +class YoutubeCommand(PlayCommand): name: str = "youtube" aliases = ["yt"] description: str = "Cerca un video su YouTube e lo aggiunge alla coda della chat vocale." - syntax = "[ [guild] ] {url}" + syntax = "{ricerca}" - @classmethod - async def _legacy_youtube_handler(cls, bot: "DiscordBot", guild_name: typing.Optional[str], search: str): - # Find the matching guild - if guild_name: - guilds: typing.List[discord.Guild] = bot.client.find_guild_by_name(guild_name) - else: - guilds = bot.client.guilds - if len(guilds) == 0: - raise CommandError("Server non trovato.") - if len(guilds) > 1: - raise CommandError("Il nome del server è ambiguo.") - guild = list(bot.client.guilds)[0] - # Ensure the guild has a PlayMode before adding the file to it - if not bot.music_data.get(guild): - raise CommandError("Il bot non è in nessun canale vocale.") - # Create url - ytdl_args = { - "format": "bestaudio/best", - "outtmpl": f"./downloads/{datetime.datetime.now().timestamp()}_%(title)s.%(ext)s" - } - # Start downloading - dfiles: typing. List[YtdlDiscord] = await asyncify(YtdlDiscord.create_from_url, f'ytsearch:{search}', **ytdl_args) - await bot.add_to_music_data(dfiles, guild) - # Create response dictionary - return { - "videos": [{ - "title": dfile.info.title, - "discord_embed_pickle": str(pickle.dumps(dfile.info.to_discord_embed())) - } for dfile in dfiles] - } - - _event_name = "_legacy_youtube" - - def __init__(self, interface: CommandInterface): - super().__init__(interface) - if interface.name == "discord": - interface.register_herald_action(self._event_name, self._legacy_youtube_handler) - - async def run(self, args: CommandArgs, data: CommandData) -> None: - guild_name, search = args.match(r"(?:\[(.+)])?\s*?") - if search.startswith("http://") or search.startswith("https://"): - raise CommandError(f"Il comando [c]{self.interface.prefix}youtube[/c] funziona solo per cercare video su" - f" YouTube con un dato nome.\n" - f"Se vuoi riprodurre una canzone da un URL, usa [c]{self.interface.prefix}play[/c]!") - response = await self.interface.call_herald_action("discord", self._event_name, { - "guild_name": guild_name, - "search": search - }) - if len(response["videos"]) == 0: - raise CommandError(f"Il video non può essere scaricato a causa di un blocco imposto da YouTube.") - for video in response["videos"]: - if self.interface.name == "discord": - # This is one of the unsafest things ever - embed = pickle.loads(eval(video["discord_embed_pickle"])) - await data.message.channel.send(content="▶️ Aggiunto alla coda:", embed=embed) - else: - await data.reply(f"▶️ Aggiunto alla coda: [i]{video['title']}[/i]") + _URL_FORMAT = "ytsearch:{url}"