Skip to main content

Module voice

Module voice 

Source
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.
CallEvents
The stream of a Call’s events. Created by Call::events.
Input
What a track plays, given to Call::play or Call::enqueue.
JoinOptions
How a VoiceManager joins 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’s joinVoiceChannel does (a bot that only plays gets nobody’s audio), at 128 kb/s.
ReceiveOptions
How Call::subscribe_with receives 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’s VoiceReceiver#subscribe.
TrackEvents
The stream of a track’s events. Created by TrackHandle::events.
TrackHandle
Controls a track that Call::play or Call::enqueue gave the call: pause it, play it again, stop it, change its volume, loop it, seek in it, read its state, follow its events.
TrackInfo
A track’s state, from TrackHandle::info (D-356).
TrackQueue
The queue of a Call, from Call::queue: skip the current track, clear the waiting ones, see what plays (D-352).
VoiceManager
The bot’s voice calls, one per guild: joins voice channels and keeps the calls.
VoicePacket
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, by sequence.
VoiceStream
The packets of one user’s audio, from Call::subscribe (D-371). It ends with its call, or as its EndBehavior says; dropping it unsubscribes.

Enums§

CallEvent
An event of a Call, from Call::events (D-321, D-337).
DisconnectReason
Why a call ended (D-344), in CallEvent::Disconnected.
EndBehavior
When a VoiceStream ends by itself (D-365); it always ends with its call.
InputError
Why an Input cannot be played.
JoinError
Why VoiceManager::join failed.
Loop
Whether a track plays again at its end (D-358), for TrackHandle::set_loop.
PlayState
Where a track is (D-361), in TrackInfo::state.
TrackEnd
Why a track ended (D-355), in TrackEvent::Ended.
TrackError
Why a TrackHandle control did nothing (D-339, D-360).
TrackEvent
An event of a track, from TrackHandle::events (D-355).
VoiceData
The audio of a VoicePacket (D-368).

Traits§

VoiceGateway
Sends Opcode 4 (Update Voice State) through a shard of the main gateway: how a VoiceManager joins, moves between and leaves voice channels.