1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Add play embed on discord

This commit is contained in:
Steffo 2019-05-22 19:39:39 +02:00
parent 0ad680a97e
commit 31ca3e4556
2 changed files with 31 additions and 4 deletions

View file

@ -1,6 +1,8 @@
import typing import typing
import logging as _logging import logging as _logging
import discord
import os import os
import datetime
from youtube_dl import YoutubeDL from youtube_dl import YoutubeDL
log = _logging.getLogger(__name__) log = _logging.getLogger(__name__)
@ -139,6 +141,18 @@ class YtdlInfo:
def download(self, outtmpl="%(title)s-%(id)s.%(ext)s", **ytdl_args) -> YtdlFile: def download(self, outtmpl="%(title)s-%(id)s.%(ext)s", **ytdl_args) -> YtdlFile:
return YtdlFile(self, outtmpl, **ytdl_args) 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): def __repr__(self):
if self.title: if self.title:
return f"<YtdlInfo of {self.title}>" return f"<YtdlInfo of {self.title}>"

View file

@ -2,6 +2,7 @@ import typing
import asyncio import asyncio
import youtube_dl import youtube_dl
import ffmpeg import ffmpeg
import pickle
from ..utils import Command, Call, NetworkHandler, asyncify from ..utils import Command, Call, NetworkHandler, asyncify
from ..network import Request, ResponseSuccess from ..network import Request, ResponseSuccess
from ..error import TooManyFoundError, NoneFoundError from ..error import TooManyFoundError, NoneFoundError
@ -38,7 +39,14 @@ class PlayNH(NetworkHandler):
else: else:
audio_sources = await asyncify(RoyalPCMAudio.create_from_ytsearch, data["url"]) audio_sources = await asyncify(RoyalPCMAudio.create_from_ytsearch, data["url"])
await bot.add_to_music_data(audio_sources, guild) 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): async def notify_on_timeout(call: Call, url: str, time: float, repeat: bool = False):
@ -59,7 +67,7 @@ class PlayCommand(Command):
@classmethod @classmethod
async def common(cls, call: Call): 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")) 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)) notify_task = loop.create_task(notify_on_timeout(call, url, time=30, repeat=True))
try: try:
@ -101,5 +109,10 @@ class PlayCommand(Command):
raise raise
finally: finally:
notify_task.cancel() notify_task.cancel()
for title in data["title_list"]: for video in data["videos"]:
await call.reply(f"⬇️ Download di [i]{title}[/i] completato.") 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.")