1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-12-18 07:34:20 +00:00
royalnet/royalpack/events/discord_summon.py

84 lines
3.7 KiB
Python
Raw Normal View History

2019-11-30 13:49:23 +00:00
from typing import *
from royalnet.commands import *
from royalnet.serf.discord import *
from royalnet.serf.discord.errors import *
2019-12-03 18:00:52 +00:00
from ..utils import RoyalQueue
2019-11-30 13:49:23 +00:00
import discord
class DiscordSummonEvent(Event):
name = "discord_summon"
async def run(self, *,
2019-12-01 22:53:47 +00:00
channel_name: str = "",
2019-11-30 13:49:23 +00:00
channel_id: Optional[int] = None,
guild_id: Optional[int] = None,
user_id: Optional[int] = None,
**kwargs):
if not isinstance(self.serf, DiscordSerf):
raise UnsupportedError()
# Find the guild
if guild_id is not None:
guild: Optional[discord.Guild] = self.serf.client.get_guild(guild_id)
else:
guild = None
# Find the member
if user_id is not None and guild is not None:
member: Optional[Union[discord.User, discord.Member]] = guild.get_member(user_id=user_id)
else:
member = None
# From channel id
2019-12-01 22:53:47 +00:00
if channel_id is not None:
2019-11-30 13:49:23 +00:00
client: discord.Client = self.serf.client
channel = client.get_channel(channel_id)
# Find channel
2019-12-01 22:53:47 +00:00
elif channel_name != "":
2019-11-30 13:49:23 +00:00
# Find accessible_to
accessible_to = [self.serf.client.user]
if member is not None:
accessible_to.append(member)
# Find the channel
channel: Optional["discord.VoiceChannel"] = self.serf.find_channel(channel_type=discord.VoiceChannel,
name=channel_name,
guild=guild,
accessible_to=accessible_to,
required_permissions=["connect", "speak"]
)
elif not (guild_id is None and user_id is None):
if member.voice is not None:
channel = member.voice.channel
else:
channel = None
else:
raise InvalidInputError("Non so a cosa devo connettermi! Specifica il nome di un canale, o entra tu stesso"
" in un canale vocale prima di invocare [c]summon[/c]!")
if channel is None:
raise InvalidInputError("Non ho trovato nessun canale in cui connettermi.")
# Create a new VoicePlayer
vp = VoicePlayer(loop=self.loop)
2019-12-03 18:00:52 +00:00
vp.playing = await RoyalQueue.create()
2019-11-30 13:49:23 +00:00
# Connect to the channel
try:
await vp.connect(channel)
2020-01-29 23:27:27 +00:00
except OpusNotLoadedError:
raise ConfigurationError("libopus non è disponibile sul sistema in cui sta venendo eseguito questo bot,"
" pertanto non è possibile con")
except DiscordTimeoutError:
raise ExternalError("Timeout durante la connessione al canale."
" Forse il bot non ha i permessi per entrarci?")
2019-11-30 13:49:23 +00:00
except GuildAlreadyConnectedError:
raise UserError("Il bot è già connesso in un canale vocale nel Server!\n"
"Spostalo manualmente, o disconnettilo e riinvoca [c]summon[/c]!")
# Add the created VoicePlayer to the list
self.serf.voice_players.append(vp)
# Reply to the request
return {
"channel": {
"id": channel.id,
"name": channel.name,
"guild": {
"name": channel.guild.name,
},
}
}