Voice

Warning

Voice may not be able to send any data as of now, this is due to a bug with encoding. If you have your own encoder feel free to use it.

VoiceConnection

class acord.VoiceConnection(voice_packet: dict, loop: asyncio.events.AbstractEventLoop, client, channel_id, **kwargs)[source]

Represents a voice connection between client and a discord voice channel

channel_id

ID of channel

Type

Snowflake

guild_id

ID of guild

Type

Snowflake

sequence

Websocket sequence

Type

int

timestamp

Timestamp for next RTC header

Type

int

ssrc

SSRC for the connection

Type

int

mode

The chosen mode for encrypting the packets in

Type

str

disconnected

Whether this connection is disconnected

Type

bool

async change_speaking_state(flags: int = 1, delay: int = 0) None[source]

This function is a coroutine.

Changes the speaking state of the client

Parameters
  • flags (int) – Speaking flag, defaults to 1

  • delay (int) – Delay before client speaks

async client_connect() None[source]

This function is a coroutine.

Sends a OP 12 payload through the ws.

async connect(*, v: int = 6) None[source]

This function is a coroutine.

Creates a websocket connection to the endpoint provided by discord

Parameters

v (int) – Endpoint version

async disconnect(*, message: bytes = b'') None[source]

This function is a coroutine.

Disconnects the voice connection closing both UDP and WS connection, it also terminates the task generated by VoiceConnection.listen()

identity(*, video: bool = False)[source]

Returns the identity packet for voice

async listen(**kwargs) None[source]

Begins to listen for websocket events, to terminate this simply end generated task

async resume() None[source]

This function is a coroutine.

Resumes the websocket connection for the vc, this should rarely be called by the user.

async send_audio_packet(data: bytes, frames: int, *, has_header: bool = False, sock_flags: int = 0) None[source]

This function is a coroutine.

Sends an audio packet to discord

Parameters
  • data (bytes) – Bytes of data to send to discord

  • has_header (bool) – Whether the data has an RTC header attached to it. Defaults to False and should only be True if you know what your doing.

  • frames (int) – Your encoders SAMPLES_PER_FRAME value, used for generating timestamp

  • sock_flags (int) – Additional flags for the UDP socket

async stop_speaking() None[source]

This function is a coroutine.

Shortcut method to VoiceConnection.change_speaking_state(), tells discord that the client has stopped speaking

udp_payload(*, mode: Optional[str] = None)[source]

Returns the payload for the UDP select packet

async upd_connect(addr: str, port: int, **kwargs) None[source]

This function is a coroutine.

Connects to the UDP port, spawns a new UDPConnection, which can be accessed by _sock.

Parameters
  • addr (str) – IP of the UDP port

  • port (int) – Connection port

  • **kwargs – Additional arguments to be passed to the UDPConnection

async wait_until_connected()[source]

This function is a coroutine.

Waits until the voice connection is connected

async wait_until_ready()[source]

This function is a coroutine.

Waits until the voice connection is ready

UDPConnection

Attributes
Methods
class acord.UDPConnection(host, port, loop, client, vc_Ws, conn_id, **kwds)[source]

Represents a UDP connection

host

IP of host

Type

str

port

Port connected to

Type

int

loop

Loop that is being used

Type

py:asyncio.AbstractLoopEvent

vc_Ws

Voice Connection using this UDP connection

Type

VoiceConnection

conn_id

The conn_id assigned to the voice connection

Type

int

\*\*kwds

Any kwargs for connecting to the socket, type and family are overwritten.

async close() None[source]

This function is a coroutine.

Closes the socket connection

async connect() None[source]

This function is a coroutine.

Connects to the UDP port.

Note

It will attempt to connect to the port 5 times before failing, this will only occur when it fails with codes 121 or 10060.

is_connected()[source]

Whether the socket is connected

async read(*, limit: Optional[int] = None, flags: int = 0) bytes[source]

This function is a coroutine.

Reads from the socket stream

Parameters
  • limit (int) – How many bytes to read

  • flags (int) – Any sock flags

async sendto(data: bytes, addr: tuple) None[source]

This function is a coroutine.

Choose to send bytes directly to the provided addr

Parameters
  • data (bytes) – Bytes to send

  • addr (tuple) – A tuple containing the address and port

async wait_until_connected() None[source]

This function is a coroutine.

Wait until the socket has connected

async write(data: bytes, *, flags: int = 0) None[source]

This function is a coroutine.

Writes to the socket stream

Parameters
  • data (bytes) – Bytes to write to stream

  • flags (int) – Any sock flags

Transports

BaseTransport

Methods
class acord.BaseTransport(conn: acord.voice.core.VoiceConnection)[source]

Base transport for working with the UDP connection

Parameters
abstract async cleanup() None[source]

This function is a coroutine.

Cleans up any cache, if any. Should be implemented by recievers which save last recieved packet, and for writers should reset state of the writer

abstract async close() None[source]

This function is a coroutine.

Closes this transport, leaving UDP connection open!

For recievers this should be set to stop recieving, and for writers to close any files + stop writing.

async recieve(*, limit: Optional[int] = None, flags: int = - 1, decode: bool = False) Optional[bytes][source]

This function is a coroutine.

Reads data from stream, recievers will automatically read and dispatch data from streams. Writers should not implement this method but if needed try implement a reciever

Parameters
  • limit (int) – How much data to read, defaults to 64Kb

  • flags (int) – Additional flags to be used when reading stream

  • decode (bool) – Whether to decode data recieved from discord, defaults to False

async send(data: bytes, *, flags: int = 0, c_flags: int = 5, continued: bool = False) None[source]

This function is a coroutine.

Sends data through stream, writers must include this method and should make sure each packet has an RTC header. To simplify this mess you should call VoiceConnection._get_audio_packet.

Note

When implementing writers, big files should be handled by you and for sending “complete” payloads, call VoiceConnection.send_audio_packet.

Parameters
  • data (bytes) – data to send

  • flags (int) – additional flags when sending data, for UDP socket NOT websocket!

  • c_flags (int) – Flags for client when speaking, defaults to 5, requesting priority and microphone.

BasePlayer

Methods
class acord.BasePlayer(conn: acord.voice.core.VoiceConnection, data: Union[io.BufferedIOBase, os.PathLike], encoder: Optional[acord.voice.opus.Encoder] = None, **encoder_kwargs)[source]
async cleanup() None[source]

This function is a coroutine.

Cleans up any cache, if any. Should be implemented by recievers which save last recieved packet, and for writers should reset state of the writer

close() None[source]

This function is a coroutine.

Closes this transport, leaving UDP connection open!

For recievers this should be set to stop recieving, and for writers to close any files + stop writing.

async send(data: bytes, *, flags: int = 0) None[source]

This function is a coroutine.

Sends data through stream, writers must include this method and should make sure each packet has an RTC header. To simplify this mess you should call VoiceConnection._get_audio_packet.

Note

When implementing writers, big files should be handled by you and for sending “complete” payloads, call VoiceConnection.send_audio_packet.

Parameters
  • data (bytes) – data to send

  • flags (int) – additional flags when sending data, for UDP socket NOT websocket!

  • c_flags (int) – Flags for client when speaking, defaults to 5, requesting priority and microphone.

BaserReceiver

Methods
class acord.BasePlayer(conn: acord.voice.core.VoiceConnection, data: Union[io.BufferedIOBase, os.PathLike], encoder: Optional[acord.voice.opus.Encoder] = None, **encoder_kwargs)[source]
async cleanup() None[source]

This function is a coroutine.

Cleans up any cache, if any. Should be implemented by recievers which save last recieved packet, and for writers should reset state of the writer

close() None[source]

This function is a coroutine.

Closes this transport, leaving UDP connection open!

For recievers this should be set to stop recieving, and for writers to close any files + stop writing.

async send(data: bytes, *, flags: int = 0) None[source]

This function is a coroutine.

Sends data through stream, writers must include this method and should make sure each packet has an RTC header. To simplify this mess you should call VoiceConnection._get_audio_packet.

Note

When implementing writers, big files should be handled by you and for sending “complete” payloads, call VoiceConnection.send_audio_packet.

Parameters
  • data (bytes) – data to send

  • flags (int) – additional flags when sending data, for UDP socket NOT websocket!

  • c_flags (int) – Flags for client when speaking, defaults to 5, requesting priority and microphone.

FfmpegPlayer

Methods
class acord.FfmpegPlayer(conn: acord.voice.core.VoiceConnection, source_file: Union[io.BufferedIOBase, os.PathLike], encoder: Optional[acord.voice.opus.Encoder] = None, executable: str = 'ffmpeg', subprocess_args: tuple = (), subprocess_kwds: dict = {}, **encoder_kwds)[source]
async cleanup(*, reset: bool = True) None[source]

This function is a coroutine.

Cleans up any cache, if any. Should be implemented by recievers which save last recieved packet, and for writers should reset state of the writer