Discord¶
Utilities for the discord.py library.
If you’re using the discord.py library, you can use the functions provided by
this module to your advantage. Instead of having to manually send the voice
server updates, you can call add_voice_server_update_handler
once and be done
with it. All events received by discord.py will then automatically be forwarded
to the Andesite client.
Even further, you can use connect_voice_channel
and disconnect_voice_channel
to easily connect to and disconnect from voice channels.
This works for both discord.ext.commands.Bot
and normal discord.Client
instances without interfering.
-
SOCKET_RESPONSE_HANDLERS_ATTR
¶ Name of the attribute used to store the
SocketResponseHandler
instances in discord clients.- Type
-
get_discord_websocket
(client, guild_id)[source]¶ Utility method to get access to discord.py’s gateway websocket.
-
async
connect_voice_channel
(client, *args, **kwargs)[source]¶ Connect to a voice channel.
This function has two signatures, you can either call it with a
VoiceChannel
, or provide a guild / guild id and the voice channel id.- Return type
None
-
async
disconnect_voice_channel
(client, guild)[source]¶ Disconnect from the current voice channel.
- Return type
None
-
class
AsyncMethodGroup
(methods)[source]¶ Group of async functions which act as a method.
When called the instance calls all its functions. It doesn’t pass the “self” parameter.
-
methods
¶
-
-
get_async_method_group
(obj, name)[source]¶ Get a method group for a method.
If the method exists and is not already an
AsyncMethodGroup
, a new group containing the previous method takes its place.- Return type
-
wrap_client_listener
(client, func, *, name=None)[source]¶ Add a listener method to the discord client.
- Parameters
This makes it possible to add a listener if the client isn’t a
discord.ext.commands.Bot
. This is achieved by adding the listener as a method to the client. If there already is a listener it is still called!See also
unwrap_client_listener
to remove it again.- Return type
None
-
unwrap_client_listener
(client, func, *, name=None)[source]¶ Remove a listener method from a discord client.
-
class
SocketResponseHandler
(discord_client, andesite_client)[source]¶ Socket response listener.
An interface between discord.py clients and andesite clients to automatically send voice server updates.
-
discord_client
¶ discord.py client that is listened to.
- Type
-
andesite_client
¶ Andesite client to send the voice server update.
- Type
-
discord_client
-
andesite_client
-
-
get_andesite_socket_response_handlers
(obj)[source]¶ Get the socket response handlers added to the discord client.
If it doesn’t exist, a new one is created and added to the object.
- Return type
-
add_voice_server_update_handler
(discord_client, andesite_client)[source]¶ Add a voice server update listener to the discord client.
- Parameters
discord_client (
Client
) – Discord client to add the listener to.andesite_client (
WebSocketInterface
) – Andesite web socket client to use to send the voice server update.
This will listen to socket responses using the discord client and trigger
WebSocketInterface.voice_server_update
.- Return type
None
-
remove_voice_server_update_handler
(discord_client, andesite_client)[source]¶ Remove the socket response handler added by
add_voice_server_update_handler
.- Parameters
discord_client (
Client
) – Discord client to remove listener from.andesite_client (
WebSocketInterface
) – Andesite web socket client to use to send the voice server update.
- Return type
None
-
compare_regions
(a_region, b_region)[source]¶ Compare two region names.
The order of the provided regions is irrelevant.
- Parameters
- Return type
- Returns
0 if the regions can’t be properly compared (ex: Node region unknown or guild id unknown). If the regions can be compared the result is the sum of the following points:
2 points if both regions are in the same country
1 point if both regions are in the same part of a country (ex: us_west) (This point is also awarded if both regions don’t specify a country part)
-
create_region_comparator
(discord_client, *, region_comp=None)[source]¶ Create a region comparator which compares the guild region with the node region.
You can use the created comparator for the
WebSocketPool
.- Parameters
- Return type
- Returns
Region comparator which uses region_comp to compare guild region with node region.