Expand description
Voice: djs-voice plugged into the Client (D-315,
D-345–D-348), behind the voice feature.
Client::voice gives the client’s VoiceManager: the client
sends its Opcode 4 through the guild’s shard and passes it the voice events, so a bot
only joins and plays. Everything here is djs-voice’s API, re-exported.
The client needs the GUILD_VOICE_STATES intent: without it the bot never learns its
voice state, and joins time out (the first client.voice() warns about it, D-346).
discord.js counterpart: @discordjs/voice with guild.voiceAdapterCreator.
§Examples
use djs_rs::{model::gateway::Intents, prelude::*, voice::Input};
let client = Client::builder("token")
.intents(Intents::GUILDS | Intents::GUILD_VOICE_STATES)
.build();
let voice = client.voice().clone();
client.on(Events::MessageCreate, move |_ctx, msg| {
let voice = voice.clone();
async move {
if msg.content == "!play"
&& let Some(guild_id) = msg.guild_id
{
let call = voice.join(guild_id, channel_id).await?;
call.play(Input::ogg_file("song.opus").await?);
}
Ok(())
}
});
client.login().await?;Structs§
- Call
- The bot’s voice connection in one guild, from
VoiceManager::join: it plays tracks, reports its events, and leaves. - Call
Events - The stream of a
Call’s events. Created byCall::events. - Input
- What a track plays, given to
Call::playorCall::enqueue. - Join
Options - How a
VoiceManagerjoins a channel: the bot’s own mute and deafen flags (D-341, D-343), and the bitrate of the audio it encodes (D-357). The default is deafened and not muted, as discord.js’sjoinVoiceChanneldoes (a bot that only plays gets nobody’s audio), at 128 kb/s. - Receive
Options - How
Call::subscribe_withreceives a user’s audio (D-372): the packets as Opus or decoded to PCM (D-364), and when the stream ends (D-365). The default is Opus, ended manually, as discord.js’sVoiceReceiver#subscribe. - Track
Events - The stream of a track’s events. Created by
TrackHandle::events. - Track
Handle - Controls a track that
Call::playorCall::enqueuegave the call: pause it, play it again, stop it, change its volume, loop it, seek in it, read its state, follow its events. - Track
Info - A track’s state, from
TrackHandle::info(D-356). - Track
Queue - The queue of a
Call, fromCall::queue: skip the current track, clear the waiting ones, see what plays (D-352). - Voice
Manager - The bot’s voice calls, one per guild: joins voice channels and keeps the calls.
- Voice
Packet - A packet of a user’s audio, from a
VoiceStream(D-368), in the order the user sent them: each one follows the one before, bysequence. - Voice
Stream - The packets of one user’s audio, from
Call::subscribe(D-371). It ends with its call, or as itsEndBehaviorsays; dropping it unsubscribes.
Enums§
- Call
Event - An event of a
Call, fromCall::events(D-321, D-337). - Disconnect
Reason - Why a call ended (D-344), in
CallEvent::Disconnected. - EndBehavior
- When a
VoiceStreamends by itself (D-365); it always ends with its call. - Input
Error - Why an
Inputcannot be played. - Join
Error - Why
VoiceManager::joinfailed. - Loop
- Whether a track plays again at its end (D-358), for
TrackHandle::set_loop. - Play
State - Where a track is (D-361), in
TrackInfo::state. - Track
End - Why a track ended (D-355), in
TrackEvent::Ended. - Track
Error - Why a
TrackHandlecontrol did nothing (D-339, D-360). - Track
Event - An event of a track, from
TrackHandle::events(D-355). - Voice
Data - The audio of a
VoicePacket(D-368).
Traits§
- Voice
Gateway - Sends Opcode 4 (Update Voice State) through a shard of the main gateway: how a
VoiceManagerjoins, moves between and leaves voice channels.