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

Complete summon command

This commit is contained in:
Steffo 2019-04-15 11:39:34 +02:00
parent 47a45a2552
commit 8cd9254ebe
2 changed files with 48 additions and 9 deletions

View file

@ -58,15 +58,7 @@ class DiscordBot:
elif len(matching_channels) > 1: elif len(matching_channels) > 1:
return SummonError("Multiple channels with a matching name found") return SummonError("Multiple channels with a matching name found")
matching_channel = matching_channels[0] matching_channel = matching_channels[0]
try: await self.bot.vc_connect_or_move(matching_channel)
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)
return SummonSuccessful() return SummonSuccessful()
self.network: RoyalnetLink = RoyalnetLink(master_server_uri, master_server_secret, "discord", network_handler) self.network: RoyalnetLink = RoyalnetLink(master_server_uri, master_server_secret, "discord", network_handler)
@ -113,6 +105,18 @@ class DiscordBot:
# noinspection PyMethodParameters # noinspection PyMethodParameters
class DiscordClient(discord.Client): 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): async def on_message(cli, message: discord.Message):
text = message.content text = message.content
# Skip non-text messages # Skip non-text messages

View file

@ -1,4 +1,5 @@
import typing import typing
import discord
from ..utils import Command, Call from ..utils import Command, Call
from ..network import Message from ..network import Message
@ -34,3 +35,37 @@ class SummonCommand(Command):
await call.reply(f"✅ Mi sono connesso in [c]#{channel_name}[/c].") await call.reply(f"✅ Mi sono connesso in [c]#{channel_name}[/c].")
return return
raise TypeError(f"Received unexpected response type while summoning the bot: {response.__class__.__name__}") 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].")