From 85468ec78d993d1293fcfe5846498be99778a483 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 16 Apr 2019 14:38:45 +0200 Subject: [PATCH] y no commit --- royalgames.py | 3 +-- royalnet/audio/__init__.py | 1 + royalnet/audio/royalaudiofile.py | 14 ++++++------- royalnet/commands/__init__.py | 3 ++- royalnet/commands/play.py | 35 ++++++++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 royalnet/commands/play.py diff --git a/royalgames.py b/royalgames.py index 0321dd9c..903a40fb 100644 --- a/royalgames.py +++ b/royalgames.py @@ -12,14 +12,13 @@ loop = asyncio.get_event_loop() log = logging.root log.addHandler(logging.StreamHandler()) -log.setLevel(logging.DEBUG) logging.getLogger("royalnet.bots.telegram").setLevel(logging.DEBUG) logging.getLogger("royalnet.bots.discord").setLevel(logging.DEBUG) logging.getLogger("royalnet.network.royalnetserver").setLevel(logging.DEBUG) commands = [PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, DebugCreateCommand, SyncCommand, AuthorCommand, DiarioCommand, RageCommand, DateparserCommand, ReminderCommand, KvactiveCommand, KvCommand, - KvrollCommand, VideoinfoCommand, SummonCommand] + KvrollCommand, VideoinfoCommand, SummonCommand, PlayCommand] master = RoyalnetServer("localhost", 1234, "sas") tg_bot = TelegramBot(os.environ["TG_AK"], "ws://localhost:1234", "sas", commands, os.environ["DB_PATH"], Royal, Telegram, "tg_id", error_command=ErrorHandlerCommand) diff --git a/royalnet/audio/__init__.py b/royalnet/audio/__init__.py index 8d5d7366..6f10a592 100644 --- a/royalnet/audio/__init__.py +++ b/royalnet/audio/__init__.py @@ -1,4 +1,5 @@ from .playmodes import PlayMode, Playlist, Pool from .youtubedl import YtdlFile, YtdlInfo +from .royalaudiofile import RoyalAudioFile __all__ = ["PlayMode", "Playlist", "Pool", "YtdlFile", "YtdlInfo"] diff --git a/royalnet/audio/royalaudiofile.py b/royalnet/audio/royalaudiofile.py index 135942f2..806f1ae4 100644 --- a/royalnet/audio/royalaudiofile.py +++ b/royalnet/audio/royalaudiofile.py @@ -13,17 +13,17 @@ log = _logging.getLogger(__name__) class RoyalAudioFile(YtdlFile): ytdl_args = { "logger": log, # Log messages to a logging.Logger instance. - "quiet": True, # Do not print messages to stdout. - "noplaylist": True, # Download single video instead of a playlist if in doubt. - "no_warnings": True, # Do not print out anything for warnings., "format": "bestaudio" # Fetch the best audio format available } def __init__(self, info: "YtdlInfo", **ytdl_args): - super().__init__(info, outtmpl="%(title)s-%(id)s.%(ext)s", **ytdl_args) + # Overwrite the new ytdl_args + self.ytdl_args = {**self.ytdl_args, **ytdl_args} + super().__init__(info, outtmpl="%(title)s-%(id)s.%(ext)s", **self.ytdl_args) # Find the audio_filename with a regex (should be video.opus) - self.audio_filename = re.sub(rf"\.{self.info.ext}$", ".opus", self.video_filename) - # Convert the video to opus + self.audio_filename = re.sub(rf"\.{self.info.ext}$", ".mp3", self.video_filename) + # Convert the video to mp3 + # Actually not needed, but we do this anyways for compression reasons converter = ffmpeg.input(self.video_filename) \ .output(self.audio_filename) converter.run() @@ -40,4 +40,4 @@ class RoyalAudioFile(YtdlFile): os.remove(self.audio_filename) def as_audio_source(self): - return discord.FFmpegPCMAudio(self.audio_filename) + return discord.FFmpegPCMAudio(f"./{self.audio_filename}") diff --git a/royalnet/commands/__init__.py b/royalnet/commands/__init__.py index 86718a1c..0cb6834c 100644 --- a/royalnet/commands/__init__.py +++ b/royalnet/commands/__init__.py @@ -15,8 +15,9 @@ from .kv import KvCommand from .kvroll import KvrollCommand from .videoinfo import VideoinfoCommand from .summon import SummonCommand +from .play import PlayCommand __all__ = ["NullCommand", "PingCommand", "ShipCommand", "SmecdsCommand", "CiaoruoziCommand", "ColorCommand", "SyncCommand", "DiarioCommand", "RageCommand", "DateparserCommand", "AuthorCommand", "ReminderCommand", - "KvactiveCommand", "KvCommand", "KvrollCommand", "VideoinfoCommand", "SummonCommand"] + "KvactiveCommand", "KvCommand", "KvrollCommand", "VideoinfoCommand", "SummonCommand", "PlayCommand"] diff --git a/royalnet/commands/play.py b/royalnet/commands/play.py new file mode 100644 index 00000000..f4d0dd47 --- /dev/null +++ b/royalnet/commands/play.py @@ -0,0 +1,35 @@ +import typing +from ..utils import Command, Call +from ..network import Message + + +class PlayMessage(Message): + def __init__(self, url: str): + self.url: str = url + + +class PlaySuccessful(Message): + pass + + +class PlayError(Message): + def __init__(self, reason: str): + self.reason: str = reason + + +class PlayCommand(Command): + command_name = "play" + command_description = "Riproduce una canzone in chat vocale." + command_syntax = "(url)" + + @classmethod + async def common(cls, call: Call): + url: str = call.args[0] + response: typing.Union[PlaySuccessful, PlayError] = await call.net_request(PlayMessage(url), "discord") + if isinstance(response, PlayError): + await call.reply(f"⚠️ Si è verificato un'errore nella richiesta di riproduzione:\n[c]{response.reason}[/c]") + return + elif isinstance(response, PlaySuccessful): + await call.reply(f"✅ Richiesta la riproduzione di [c]{url}[/c].") + return + raise TypeError(f"Received unexpected response in the PlayCommand: {response.__class__.__name__}")