VoiceClient
Represents a client what is joined to a voice channel.
Attributes
callable
(awaitable
)A coroutine function what is awaited, when the voice clients' current audio finishes playing. By default this attribute is set to the ._play_next
function of the voice client (plays the next audio at the voice clients' .queue
as expected.
This attribute of the client can be modified freely. To it 2
parameters are passed:
Respective name | Type |
---|---|
client | VoiceClient |
last_source | None , AudioSource |
The VoiceClient
also includes some other predefined function for setting as call_after
:
int
The channel's identifier where the voice client currently is.
The voice client's owner client.
The gateway through what the voice client communicates with Discord.
int
The guild's identifier where the voice client is.
Lock
A lock used meanwhile changing the currently playing audio to not modifying it parallelly.
The actual player of the VoiceClient
. If the voice client is not playing nor paused, then set as None
.
list
of AudioSource
A list of the scheduled audios.
None
, AudioReader
Meanwhile the received audio is collected, this attribute is set to a running AudioReader
.
The actual voice region of the voice client.
bool
Whether the voice client plans to reconnect and it's reader and player should not be stopped.
int
Whether the client is showed by Discord as speaking
, then this attribute should is set as 1
. Can be modified, with the .set_speaking
, however it is always adjusted to the voice client's current playing state.
int
An identifier sent by Discord what should be sent back with every voice packet.
dict
of (int
, int
) itemsuser_id
- audio_source
mapping used by AudioStream
-s.
user_id
- AudioStream
(s) mapping for linking AudioStream
to their respective user.
bool
Whether the voice client is connected.
None | set<Future>
Waiter futures waiting for the voice client to be connected again.
Encode not opus encoded audio data.
None
, str
The endpoint, where the voice client sends the audio data.
None
, tuple
of int
The ip version of the ._endpoint
attribute.
int
The port, where the voice client should send the audio data.
Future
Used for awaiting the connecting handshake with Discord.
None | str
The ip to what the voice client's gateway connects.
None
, int
The port to what the voice client's gateway connects.
float
The preferred volume of the voice client. can be between 0.0
and 2.0
.
None
, DatagramMergerReadProtocol
Asynchronous protocol of the voice client to communicate with it's socket.
None
, nacl.secret.SecretBox
Data encoder of the voice client.
int
Counter to define the sent data's sequence for Discord.
None
, Task
Synchronization task for the .set_speaking
coroutine.
None
, socket.socket
The socket through what the VoiceClient
sends the voice data to Discord. Created by the ._create_socket
method, when the client's gateway receives response after connecting. If the client leaves the voice channel, then the socket is closed and set back to None
.
int
A timestamp identifier to tell Discord how much frames we sent to it.
str
Token received by the voice client's owner client's gateway. Used to authorize the voice client.
None
, _SelectorDatagramTransport
Asynchronous transport of the voice client to communicate with it's socket.
int
An identifier sent by Discord what should be sent back with every video packet.
dict
of (int
, int
) itemsuser_id
- video_source
mapping. Not used for now.
Properties
channel
guild
source
voice_state
volume
Get-set property for accessing the voice client's volume.
Can be between 0.0
and 2.0
.
Methods
(client, guild_id, channel_id)
__new__Creates a VoiceClient
. If any of the required libraries are not present, raises RuntimeError
.
If the voice client was successfully created, returns a Future
, what is a waiter for it's ._connect
method. If connecting failed, then the future will raise TimeoutError
.
Parameter | Type | Description |
---|---|---|
client | The parent client. | |
guild_id |
| the guild's identifier, where the the client will connect to. |
channel_id |
| The channel's identifier where the client will connect to. |
Raises
RuntimeError
If PyNaCl
is not loaded. If Opus
is not loaded.
(source)
appendStarts playing the given audio source. If the voice client is already playing, puts it on it's queue instead.
Parameter | Type | Description |
---|---|---|
source | The audio source to put on the queue. |
Returns
started_playing : bool
Whether the source is started playing and not put on queue.
()
clear_connectedSets the voice client's state to be not connected.
()
disconnectDisconnects the voice client.
This method is a coroutine.
()
get_audio_streamsReturns the audio streams of the voice client within a list
.
Returns
streams : list
of tuple
(ClientUserBase
, AudioStream
)
Audio streams as a list
of tuples
of their respective listened user
and stream
.
()
is_connected()
is_pausedReturns whether the voice client is currently paused (or not playing).
Returns
is_paused : bool
()
is_playingReturns whether the voice client is currently playing audio.
Returns
is_playing : bool
()
join_audienceJoins the audience in the voice client's voice channel. Only applicable for stage channels.
This method is a coroutine.
Raises
ConnectionError
No internet connection.
DiscordException
If any exception was received from the Discord API.
(...)
join_speakersRequests to speak at the voice client's voice channel. Only applicable for stage channels.
This method is a coroutine.
Parameter | Type | Optional | Keyword only | Default | Description |
---|---|---|---|---|---|
request |
|
| Whether the client should only request to speak. |
Raises
ConnectionError
No internet connection.
DiscordException
If any exception was received from the Discord API.
(user, ...)
listen_toCreates an audio stream for the given user.
Parameter | Type | Optional | Keyword only | Description |
---|---|---|---|---|
user | The user, who's voice will be captured. | |||
**keyword_parameters | Keyword parameters | Additional keyword parameters. | ||
auto_decode |
| Whether the received packets should be auto decoded. | ||
yield_decoded |
| Whether the audio stream should yield encoded data. |
Returns
audio_stream : AudioStream
(channel)
move_toMove the voice client to an another voice channel.
This method is a coroutine.
Parameter | Type | Description |
---|---|---|
channel |
| The channel where the voice client will move to. |
Returns
moved : bool
Returns False
if the voice client is already in the channel.
Raises
TypeError
If channel
was not given as Channel
not int
.
()
pausePauses the currently played audio if applicable.
()
play_nextSkips the currently playing audio.
Familiar to .skip
, but it return when the operation id done.
This method is a coroutine.
()
resumeResumes the currently stopped audio if applicable.
(packet)
send_packetSends the given packet to Discord with the voice client's socket.
Parameter | Type | Description |
---|---|---|
packet |
| The packet to send. |
()
set_connectedSets the voice client's state to be connected.
(value)
set_speakingA coroutine, what is used when changing the .speaking
state of the voice client. By default when audio is played, the speaking state is changed to True
and meanwhile not, then to False
.
This method is a coroutine.
Parameter | Type |
---|---|
value |
|
Notes
Tinkering with this method is not recommended.
(...)
skipSkips the currently played audio at the given index and returns it.
Skipping nothing yields to returning None
.
Parameter | Type | Optional | Default | Description |
---|---|---|---|---|
index |
|
| The index of the audio to skip. Defaults to |
Returns
source : None
, AudioSource
()
startStarts the voice client and returns a waiter than can be awaited to wait for connection.
Returns
waiter : Future<bool>
()
stopStops the currently playing audio and clears the audio queue.
(timeout)
wait_connectedWaits until the voice client is connected.
Parameter | Type | Description |
---|---|---|
timeout |
| Timeout to apply while waiting for connection. |
Returns
connected : bool
Whether the voice client connected or disconnected before timeout
.
Raises
TimeoutError
()
_close_socketCloses the voice client's socket and transport if they are set.
()
_create_connected_waiterCreates a connected state waiter.
Returns
connected_waiter : Future
(event)
_create_socketCalled when voice server update data is received from Discord.
If full data was received, closes the actual socket if exists and creates a new one connected to the received address.
If the voice client is already connected reconnects it, if not then marks it as connected.
This method is a coroutine.
Parameter | Type | Description |
---|---|---|
event | Voice server update event. |
(...)
_disconnectDisconnects the voice client.
If you want to disconnect a voice client, then you should use .disconnect
. Passing bad parameters to this method the can cause misbehaviour.
This method is a coroutine.
Parameter | Type | Optional | Default | Description |
---|---|---|---|---|
force |
|
| Whether the voice client should disconnect only if it is not connected (for example when it is connecting). | |
terminate |
|
| Whether it is an internal disconnect. If the Disconnect comes from Discord's side, then |
(client, voice_state)
_kill_ghostWhen a client is restarted, it might happen that it will be in still in some voice channels. At this case this function is ensured to kill the ghost connection.
This method is a coroutine.
Parameter | Type | Description |
---|---|---|
client | The owner client of the ghost connection. | |
voice_state | The ghost voice client's voice state. |
(stream)
_link_audio_streamLinks the given AudioStream
to self causing to start receiving audio.
Parameter | Type |
---|---|
stream |
(self, last_source)
_loop_actualRepeats the last played audio if applicable.
Should be used inside of .lock
to ensure that the voice client is not modified parallelly.
This function is a coroutine.
Parameter | Type | Description |
---|---|---|
self | The respective voice client. | |
last_source |
| The audio what was played. |
(self, last_source)
_loop_queuePuts the last played audio back on the voice client's queue.
Should be used inside of .lock
to ensure that the voice client is not modified parallelly.
This function is a coroutine.
Parameter | Type | Description |
---|---|---|
self | The respective voice client. | |
last_source |
| The audio what was played. |
()
_maybe_change_voice_regionResets the voice region of the voice client.
Returns
changed: bool
Whether voice region changed.
(self, last_source)
_play_nextStarts to play the next audio object on .queue
and cancels the actual one if applicable. Should be used inside of .lock
to ensure that the voice client is not modified parallelly.
This function is a coroutine.
Parameter | Type | Description |
---|---|---|
self | The respective voice client. | |
last_source |
| The audio what was played. |
(user_id)
_remove_audio_sourceUn-links the audio streams' source listening to the given user (id), causing the affected audio stream(s) to stop receiving audio data at the meanwhile.
Parameter | Type | Description |
---|---|---|
user_id |
| The respective user's id. |
(connected_waiter)
_remove_connected_waiterRemoves the given connected state waiter.
Parameter | Type | Description |
---|---|---|
connected_waiter |
| The connection waiter to remove. |
(user_id)
_remove_video_sourceUn-links a video stream's source.
Parameter | Type | Description |
---|---|---|
user_id |
| The respective user's id. |
(...)
_runConnects the voice client to Discord and keeps receiving the gateway events.
This method is a coroutine.
Parameter | Type | Optional | Default | Description |
---|---|---|---|---|
waiter |
|
| A Waiter what's result is set (or is raised to), when the voice client connects (or failed to connect). |
(result)
_set_connected_waitersSets the connection waiter's result.
Parameter | Type | Description |
---|---|---|
result |
| The value to set the waiter's result to. |
()
_start_handshakeRequests a gateway handshake from Discord. If we get answer on it, means, we can open the socket to send audio data.
This method is a coroutine.
Raises
TimeoutError
We did not receive answer in time.
()
_stop_player_and_readerStops the voice client's player and reader.
()
_terminate_handshakeCalled when connecting to Discord fails. Ensures, that everything is aborted correctly.
This method is a coroutine.
(audio_stream)
_unlink_audio_streamUn-links the given audio stream from the voice client causing it to stop receiving audio.
Parameter | Type |
---|---|
audio_stream |
(user_id, audio_source)
_update_audio_sourceUpdates (or adds) an user-id
- audio-source
relation to the voice client causing the affected audio streams to listen to their new source.
Parameter | Type | Description |
---|---|---|
user_id |
| The respective user's id. |
audio_source |
| Audio source identifier of the user. |
(user_id, video_source)
_update_video_sourceUpdates (or adds) an user-id
- video-source
relation to the voice client.
Parameter | Type | Description |
---|---|---|
user_id |
| The respective user's id. |
video_source |
| Video source identifier of the user. |
()
__del__Stops and unallocates the resources by the voice client, if was not done already.
()
__getattr__Drops a rich attribute error.
()
__repr__Returns the voice client's representation.