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

103 lines
4.4 KiB
Python
Raw Normal View History

2019-12-01 22:53:47 +00:00
import datetime
from typing import *
2020-02-03 16:22:44 +00:00
import discord
import royalnet.commands as rc
import royalnet.serf.discord as rsd
import royalnet.bard.discord as rbd
2020-03-29 23:33:07 +00:00
from ..utils import RoyalQueue, RoyalPool
2019-12-01 22:53:47 +00:00
2020-02-03 16:22:44 +00:00
class DiscordPlayEvent(rc.Event):
2019-12-01 22:53:47 +00:00
name = "discord_play"
async def run(self,
2020-03-28 18:38:14 +00:00
urls: List[str],
2019-12-01 22:53:47 +00:00
guild_id: Optional[int] = None,
2020-01-13 13:51:06 +00:00
user: Optional[str] = None,
2020-01-30 19:08:31 +00:00
force_color: Optional[int] = None,
2019-12-01 22:53:47 +00:00
**kwargs) -> dict:
2020-02-03 16:22:44 +00:00
if not isinstance(self.serf, rsd.DiscordSerf):
raise rc.UnsupportedError()
2020-02-03 16:22:44 +00:00
serf: rsd.DiscordSerf = self.serf
2020-01-13 13:51:06 +00:00
client: discord.Client = self.serf.client
2020-03-28 18:38:14 +00:00
guild: discord.Guild = client.get_guild(guild_id) if guild_id is not None else None
candidate_players: List[rsd.VoicePlayer] = serf.find_voice_players(guild)
if len(candidate_players) == 0:
2020-02-03 16:22:44 +00:00
raise rc.UserError("Il bot non è in nessun canale vocale.\n"
2020-02-03 23:54:39 +00:00
"Evocalo prima con [c]summon[/c]!")
elif len(candidate_players) == 1:
voice_player = candidate_players[0]
2019-12-01 22:53:47 +00:00
else:
2020-02-03 16:22:44 +00:00
raise rc.CommandError("Non so in che Server riprodurre questo file...\n"
2020-03-09 20:21:07 +00:00
"Invia il comando su Discord, per favore!")
2020-02-03 16:22:44 +00:00
added: List[rbd.YtdlDiscord] = []
too_long: List[rbd.YtdlDiscord] = []
2020-03-28 18:38:14 +00:00
for url in urls:
ytds = await rbd.YtdlDiscord.from_url(url)
if isinstance(voice_player.playing, RoyalQueue):
for index, ytd in enumerate(ytds):
if ytd.info.duration >= datetime.timedelta(seconds=self.config["Play"]["max_song_duration"]):
too_long.append(ytd)
continue
await ytd.convert_to_pcm()
added.append(ytd)
voice_player.playing.contents.append(ytd)
if not voice_player.voice_client.is_playing():
await voice_player.start()
2020-03-29 23:33:07 +00:00
elif isinstance(voice_player.playing, RoyalPool):
for index, ytd in enumerate(ytds):
if ytd.info.duration >= datetime.timedelta(seconds=self.config["Play"]["max_song_duration"]):
too_long.append(ytd)
continue
await ytd.convert_to_pcm()
added.append(ytd)
voice_player.playing.full_pool.append(ytd)
voice_player.playing.remaining_pool.append(ytd)
if not voice_player.voice_client.is_playing():
await voice_player.start()
2020-03-28 18:38:14 +00:00
else:
raise rc.CommandError(f"Non so come aggiungere musica a [c]{voice_player.playing.__class__.__qualname__}[/c]!")
2020-01-13 13:51:06 +00:00
main_channel: discord.TextChannel = client.get_channel(self.config["Discord"]["main_channel_id"])
if len(added) > 0:
if user:
2020-02-03 16:22:44 +00:00
await main_channel.send(rsd.escape(f"▶️ {user} ha aggiunto {len(added)} file alla coda:"))
2020-01-13 13:51:06 +00:00
else:
2020-02-03 16:22:44 +00:00
await main_channel.send(rsd.escape(f"▶️ Aggiunt{'o' if len(added) == 1 else 'i'} {len(added)} file alla"
2020-02-03 23:54:39 +00:00
f" coda:"))
for ytd in added[:5]:
2020-01-30 19:08:31 +00:00
embed: discord.Embed = ytd.embed()
if force_color:
embed._colour = discord.Colour(force_color)
await main_channel.send(embed=embed)
if len(added) > 5:
await main_channel.send(f"e altri {len(added) - 5}!")
2020-01-13 13:51:06 +00:00
if len(too_long) > 0:
if user:
2020-02-03 16:22:44 +00:00
await main_channel.send(rsd.escape(
2020-01-13 13:51:06 +00:00
f"{len(too_long)} file non {'è' if len(too_long) == 1 else 'sono'}"
f" stat{'o' if len(too_long) == 1 else 'i'} scaricat{'o' if len(too_long) == 1 else 'i'}"
f" perchè durava{'' if len(too_long) == 1 else 'no'}"
f" più di [c]{self.config['Play']['max_song_duration']}[/c] secondi."
))
if len(added) + len(too_long) == 0:
2020-03-28 18:38:14 +00:00
raise rc.InvalidInputError("Non è stato aggiunto nessun file alla coda.")
2020-01-13 13:51:06 +00:00
2019-12-01 22:53:47 +00:00
return {
"added": [{
"title": ytd.info.title,
} for ytd in added],
"too_long": [{
"title": ytd.info.title,
} for ytd in too_long]
}