diff --git a/royalnet/bots/discord.py b/royalnet/bots/discord.py index 776ea881..a053f7a6 100644 --- a/royalnet/bots/discord.py +++ b/royalnet/bots/discord.py @@ -58,7 +58,15 @@ class DiscordBot: elif len(matching_channels) > 1: return SummonError("Multiple channels with a matching name found") matching_channel = matching_channels[0] - await matching_channel.connect() + 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) return SummonSuccessful() self.network: RoyalnetLink = RoyalnetLink(master_server_uri, master_server_secret, "discord", network_handler) diff --git a/royalnet/commands/summon.py b/royalnet/commands/summon.py index e0284dfe..052c0f9d 100644 --- a/royalnet/commands/summon.py +++ b/royalnet/commands/summon.py @@ -1,6 +1,6 @@ import typing from ..utils import Command, Call -from ..network import Message, ErrorMessage +from ..network import Message class SummonMessage(Message): @@ -12,8 +12,9 @@ class SummonSuccessful(Message): pass -class SummonError(ErrorMessage): - pass +class SummonError(Message): + def __init__(self, reason: str): + self.reason: str = reason class SummonCommand(Command): @@ -32,4 +33,4 @@ class SummonCommand(Command): elif isinstance(response, SummonSuccessful): await call.reply(f"✅ Mi sono connesso in [c]#{channel_name}[/c].") return - raise Exception(f"wtf is this: {response}") + raise TypeError(f"Received unexpected response type while summoning the bot: {response.__class__.__name__}") diff --git a/royalnet/network/__init__.py b/royalnet/network/__init__.py index 9015ede6..1f279e15 100644 --- a/royalnet/network/__init__.py +++ b/royalnet/network/__init__.py @@ -1,10 +1,10 @@ -from .messages import Message, ErrorMessage, InvalidSecretEM, InvalidDestinationEM, InvalidPackageEM +from .messages import Message, ServerErrorMessage, InvalidSecretEM, InvalidDestinationEM, InvalidPackageEM from .packages import Package from .royalnetlink import RoyalnetLink, NetworkError, NotConnectedError, NotIdentifiedError from .royalnetserver import RoyalnetServer __all__ = ["Message", - "ErrorMessage", + "ServerErrorMessage", "InvalidSecretEM", "InvalidDestinationEM", "InvalidPackageEM", diff --git a/royalnet/network/messages.py b/royalnet/network/messages.py index 99696f02..97a95563 100644 --- a/royalnet/network/messages.py +++ b/royalnet/network/messages.py @@ -7,17 +7,17 @@ class IdentifySuccessfulMessage(Message): pass -class ErrorMessage(Message): +class ServerErrorMessage(Message): def __init__(self, reason): super().__init__() self.reason = reason -class InvalidSecretEM(ErrorMessage): +class InvalidSecretEM(ServerErrorMessage): pass -class InvalidPackageEM(ErrorMessage): +class InvalidPackageEM(ServerErrorMessage): pass diff --git a/royalnet/network/royalnetlink.py b/royalnet/network/royalnetlink.py index f3531d9a..a2bfb34f 100644 --- a/royalnet/network/royalnetlink.py +++ b/royalnet/network/royalnetlink.py @@ -5,7 +5,7 @@ import functools import typing import pickle import logging as _logging -from .messages import Message, ErrorMessage +from .messages import Message, ServerErrorMessage from .packages import Package loop = asyncio.get_event_loop() @@ -21,9 +21,9 @@ class NotIdentifiedError(Exception): class NetworkError(Exception): - def __init__(self, error_msg: ErrorMessage, *args): + def __init__(self, error_msg: ServerErrorMessage, *args): super().__init__(*args) - self.error_msg: ErrorMessage = error_msg + self.error_msg: ServerErrorMessage = error_msg class PendingRequest: @@ -98,7 +98,7 @@ class RoyalnetLink: await self.websocket.send(f"Identify {self.nid}:{self.link_type}:{self.secret}") response_package = await self.receive() response = response_package.data - if isinstance(response, ErrorMessage): + if isinstance(response, ServerErrorMessage): raise NetworkError(response, "Server returned error while identifying self") self.identify_event.set() log.info(f"Identified successfully!") @@ -119,7 +119,7 @@ class RoyalnetLink: await request.event.wait() result: Message = request.data log.debug(f"Received response: {request} -> {result}") - if isinstance(result, ErrorMessage): + if isinstance(result, ServerErrorMessage): raise NetworkError(result, "Server returned error while requesting something") return result