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

str

get_discord_websocket(client, guild_id)[source]

Utility method to get access to discord.py’s gateway websocket.

Parameters
  • client (Union[Client, ConnectionState]) – discord.py client.

  • guild_id (int) – Guild id whose websocket to get.

Return type

DiscordWebSocket

async update_voice_state(client, guild_id, channel_id)[source]

Update the voice state.

Parameters
  • client (Client) – discord.py client.

  • guild_id (int) – Guild id to target

  • channel_id (Optional[int]) – Channel id to connect to. If None, disconnect from the current channel.

Return type

None

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

AsyncMethodGroup

wrap_client_listener(client, func, *, name=None)[source]

Add a listener method to the discord client.

Parameters
  • client (Client) – Client to add the listener to

  • func (Callable) – Listener function to add

  • name (Optional[str]) – Custom name to use. Defaults to the name of the function. Be sure to include “on_” if you set this.

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.

Parameters
  • client (Client) – Client to remove listener from

  • func (Callable) – Listener function to remove

  • name (Optional[str]) – Custom name to use. Defaults to the name of the function. Be sure to include “on_” if you set this.

Return type

None

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

discord.Client

andesite_client

Andesite client to send the voice server update.

Type

WebSocketInterface

discord_client
andesite_client
add_listener()[source]

Add the on_socket_response listener.

If the handler is attached to a Bot, it uses the listener framework, otherwise it safely wraps the client handler.

Return type

None

remove_listener()[source]

Remove the on_socket_response listener.

Return type

None

async on_socket_response(data)[source]

Intercept voice server updates and send them to Andesite.

Return type

None

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

Dict[WebSocketInterface, SocketResponseHandler]

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
  • a_region (str) – First region name

  • b_region (str) – Second region name

Return type

int

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
  • discord_client (Client) – Client to use to determine the guild region.

  • region_comp (Optional[Callable[[str, str], int]]) – Specify the actual function which compares the guild region and the Andesite node region. Defaults to compare_regions.

Return type

Callable[[int, Optional[str]], int]

Returns

Region comparator which uses region_comp to compare guild region with node region.