From 31ca3e4556182d4636b40b23992cf47525afa3d3 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 22 May 2019 19:39:39 +0200 Subject: [PATCH] Add play embed on discord --- royalnet/audio/youtubedl.py | 14 ++++++++++++++ royalnet/commands/play.py | 21 +++++++++++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/royalnet/audio/youtubedl.py b/royalnet/audio/youtubedl.py index f43f232b..b0c4f47d 100644 --- a/royalnet/audio/youtubedl.py +++ b/royalnet/audio/youtubedl.py @@ -1,6 +1,8 @@ import typing import logging as _logging +import discord import os +import datetime from youtube_dl import YoutubeDL log = _logging.getLogger(__name__) @@ -139,6 +141,18 @@ class YtdlInfo: def download(self, outtmpl="%(title)s-%(id)s.%(ext)s", **ytdl_args) -> YtdlFile: return YtdlFile(self, outtmpl, **ytdl_args) + def to_discord_embed(self) -> discord.Embed: + embed = discord.Embed(title=self.title, + colour=discord.Colour(0xcc0000), + url=self.webpage_url) + embed.set_thumbnail( + url=self.thumbnail) + embed.set_author(name=self.uploader, url=self.uploader_url) + embed.set_footer(text="Fonte: youtube-dl", icon_url="https://i.imgur.com/TSvSRYn.png") + embed.add_field(name="Duration", value=str(self.duration), inline=True) + embed.add_field(name="Published on", value=self.upload_date, inline=True) + return embed + def __repr__(self): if self.title: return f"" diff --git a/royalnet/commands/play.py b/royalnet/commands/play.py index 3fa0073d..ee4f94d9 100644 --- a/royalnet/commands/play.py +++ b/royalnet/commands/play.py @@ -2,6 +2,7 @@ import typing import asyncio import youtube_dl import ffmpeg +import pickle from ..utils import Command, Call, NetworkHandler, asyncify from ..network import Request, ResponseSuccess from ..error import TooManyFoundError, NoneFoundError @@ -38,7 +39,14 @@ class PlayNH(NetworkHandler): else: audio_sources = await asyncify(RoyalPCMAudio.create_from_ytsearch, data["url"]) await bot.add_to_music_data(audio_sources, guild) - return ResponseSuccess({"title_list": [source.rpf.info.title for source in audio_sources]}) + # Create response dictionary + response = { + "videos": [{ + "title": source.rpf.info.title, + "discord_embed_pickle": str(pickle.dumps(source.rpf.info.to_discord_embed())) + } for source in audio_sources] + } + return ResponseSuccess(response) async def notify_on_timeout(call: Call, url: str, time: float, repeat: bool = False): @@ -59,7 +67,7 @@ class PlayCommand(Command): @classmethod async def common(cls, call: Call): - guild_name, url = call.args.match(r"(?:\[(.+)])?\s*(.+)") + guild_name, url = call.args.match(r"(?:\[(.+)])?\s*?") download_task = loop.create_task(call.net_request(Request("music_play", {"url": url, "guild_name": guild_name}), "discord")) notify_task = loop.create_task(notify_on_timeout(call, url, time=30, repeat=True)) try: @@ -101,5 +109,10 @@ class PlayCommand(Command): raise finally: notify_task.cancel() - for title in data["title_list"]: - await call.reply(f"⬇️ Download di [i]{title}[/i] completato.") + for video in data["videos"]: + if call.interface_name == "discord": + # This is one of the unsafest things ever + embed = pickle.loads(eval(video["discord_embed_pickle"])) + await call.channel.send(content="✅ Aggiunto alla coda:", embed=embed) + else: + await call.reply(f"✅ [i]{video['title']}[/i] scaricato e aggiunto alla coda.")