diff --git a/royalnet/bots/discord.py b/royalnet/bots/discord.py index a053f7a6..bbbdc1fb 100644 --- a/royalnet/bots/discord.py +++ b/royalnet/bots/discord.py @@ -58,15 +58,7 @@ class DiscordBot: elif len(matching_channels) > 1: return SummonError("Multiple channels with a matching name found") matching_channel = matching_channels[0] - try: - await matching_channel.connect() - except discord.errors.ClientException: - # Move to the selected channel, instead of connecting - for voice_client in self.bot.voice_clients: - voice_client: discord.VoiceClient - if voice_client.guild != matching_channel.guild: - continue - await voice_client.move_to(matching_channel) + await self.bot.vc_connect_or_move(matching_channel) return SummonSuccessful() self.network: RoyalnetLink = RoyalnetLink(master_server_uri, master_server_secret, "discord", network_handler) @@ -113,6 +105,18 @@ class DiscordBot: # noinspection PyMethodParameters class DiscordClient(discord.Client): + @staticmethod + async def vc_connect_or_move(channel: discord.VoiceChannel): + try: + await channel.connect() + except discord.errors.ClientException: + # Move to the selected channel, instead of connecting + for voice_client in self.bot.voice_clients: + voice_client: discord.VoiceClient + if voice_client.guild != channel.guild: + continue + await voice_client.move_to(channel) + async def on_message(cli, message: discord.Message): text = message.content # Skip non-text messages diff --git a/royalnet/commands/summon.py b/royalnet/commands/summon.py index 052c0f9d..5b43bf0c 100644 --- a/royalnet/commands/summon.py +++ b/royalnet/commands/summon.py @@ -1,4 +1,5 @@ import typing +import discord from ..utils import Command, Call from ..network import Message @@ -34,3 +35,37 @@ class SummonCommand(Command): await call.reply(f"✅ Mi sono connesso in [c]#{channel_name}[/c].") return raise TypeError(f"Received unexpected response type while summoning the bot: {response.__class__.__name__}") + + @classmethod + async def discord(cls, call: Call): + bot = call.interface_obj.bot + message: discord.Message = call.kwargs["message"] + channel_name: str = call.args.optional(0) + if channel_name: + guild: typing.Optional[discord.Guild] = message.guild + channels: typing.List[discord.abc.GuildChannel] + if guild is not None: + channels = guild.channels + else: + channels = bot.get_all_channels() + matching_channels: typing.List[discord.VoiceChannel] = [] + for channel in channels: + if isinstance(channel, discord.VoiceChannel): + if channel.name == channel_name: + matching_channels.append(channel) + if len(matching_channels) == 0: + await call.reply("⚠️ Non esiste alcun canale vocale con il nome specificato.") + return + elif len(matching_channels) > 1: + await call.reply("⚠️ Esiste più di un canale vocale con il nome specificato.") + return + channel = matching_channels[0] + else: + author: discord.Member = message.author + voice: typing.Optional[discord.VoiceState] = author.voice + if voice is None: + await call.reply("⚠️ Non sei connesso a nessun canale vocale!") + return + channel = voice.channel + await bot.vc_connect_or_move(channel) + await call.reply(f"✅ Mi sono connesso in [c]#{channel.name}[/c].")