Skip to main content

Call

Struct Call 

Source
pub struct Call { /* private fields */ }
Expand description

The bot’s voice connection in one guild, from VoiceManager::join: it plays tracks, reports its events, and leaves.

A cheap-clone handle (D-320): clones are the same call, and every method takes &self. A lost connection is resumed or made again in the background (D-331), and a move to another channel (by join or by a moderator) connects it again; events reports both. The call ends when the bot leaves, is disconnected, or its connection cannot be repaired (CallEvent::Disconnected).

Discord documents voice connections at https://discord.com/developers/docs/topics/voice-connections.

discord.js counterpart: VoiceConnection with a subscribed AudioPlayer of @discordjs/voice; songbird: Call.

§Examples

use djs_voice::{CallEvent, Input};

let call = manager.join(guild, channel).await?;
let mut events = call.events();
call.play(Input::ogg_file("intro.opus").await?);
while let Some(event) = events.next().await {
    if let CallEvent::Disconnected(reason) = event {
        println!("the call ended: {reason}");
    }
}

Implementations§

Source§

impl Call

Source

pub fn guild_id(&self) -> Id<GuildMarker>

The guild of the call.

discord.js counterpart: VoiceConnection#joinConfig.guildId.

Source

pub fn channel_id(&self) -> Option<Id<ChannelMarker>>

The voice channel the call is connected to; None while it is not (connecting again after a move, or ended).

discord.js counterpart: VoiceConnection#joinConfig.channelId.

Source

pub fn play(&self, input: Input) -> TrackHandle

Plays input now, replacing the current track (D-335, D-352): it ends (TrackEnd::Replaced), and the tracks waiting in the queue play after this one. Returns at once; the input is read and sent in the background, paced in real time.

While the call is connecting again (after a move or a lost connection) the track keeps its pace and its audio is not sent; with end-to-end encryption (DAVE), audio is sent once the bot is in the call’s group (D-349).

discord.js counterpart: AudioPlayer#play of a player subscribed to the connection; songbird: Call::play_only_input.

§Examples
let track = call.play(djs_voice::Input::ogg_file("song.opus").await?);
Source

pub fn enqueue(&self, input: Input) -> TrackHandle

Adds input to the end of the call’s queue (D-352): it plays when the tracks before it have ended, at once when nothing plays. Returns its handle at once; until its turn it is PlayState::Queued, and FFmpeg is not run for it yet.

discord.js counterpart: none (a bot keeps its own queue); songbird: Call::enqueue_input.

§Examples
use djs_voice::Input;

for song in ["one.mp3", "two.mp3", "three.mp3"] {
    call.enqueue(Input::ffmpeg(song));
}
Source

pub fn queue(&self) -> TrackQueue

The call’s queue of tracks (D-352): skip the current one, clear the waiting ones, see what plays.

songbird counterpart: Call::queue.

discord.js counterpart: none (@discordjs/voice has no queue).

Source

pub fn subscribe(&self, user_id: Id<UserMarker>) -> VoiceStream

Receives the audio of the user user_id (D-363, D-367): a stream of their packets, as Opus, in order, ended manually (subscribe_with chooses).

Discord sends a bot nobody’s audio while it is deafened, which is how VoiceManager::join joins: join with JoinOptions::self_deaf(false) to receive. The stream may be made before the user speaks or even joins; packets come once the voice gateway has said which source is theirs (the first time they speak). Each stream is its own, with a copy of the packets (D-373); dropping it unsubscribes. It ends with the call.

discord.js counterpart: VoiceReceiver#subscribe; songbird: the VoiceTick event.

§Examples
use djs_voice::{JoinOptions, VoiceData};

let options = JoinOptions::new().self_deaf(false);
let call = manager.join_with(guild_id, channel_id, options).await?;
let mut voice = call.subscribe(user_id);
let mut bytes = 0;
while let Some(packet) = voice.next().await {
    if let VoiceData::Opus(opus) = packet.audio {
        bytes += opus.len();
    }
}
println!("{bytes} bytes of Opus received");
Source

pub fn subscribe_with( &self, user_id: Id<UserMarker>, options: ReceiveOptions, ) -> VoiceStream

Receives the audio of the user user_id with options (D-367, D-372): decoded to PCM or not, ended by itself or not. Otherwise as subscribe.

discord.js counterpart: VoiceReceiver#subscribe with its options.

§Examples
use std::time::Duration;

use djs_voice::{EndBehavior, ReceiveOptions, VoiceData};

// One utterance, decoded: it ends after a second of silence.
let options = ReceiveOptions::new()
    .decode(true)
    .end(EndBehavior::AfterSilence(Duration::from_secs(1)));
let mut voice = call.subscribe_with(user_id, options);
let mut samples: Vec<i16> = Vec::new();
while let Some(packet) = voice.next().await {
    if let VoiceData::Pcm(pcm) = packet.audio {
        samples.extend(pcm);
    }
}
println!("{} s of audio", samples.len() / 2 / 48_000);
Source

pub fn events(&self) -> CallEvents

Returns a stream of the call’s events: connections resumed and made again, users joining, leaving and speaking, and the end of the call. Each stream gets every event from its creation on, and ends with the call.

discord.js counterpart: VoiceConnection#on('stateChange'); songbird: Call::add_global_event.

Source

pub async fn leave(&self) -> Result<(), Box<dyn Error + Send + Sync>>

Leaves the voice channel: sends Opcode 4 without a channel, closes the connection and ends the call (DisconnectReason::Left); returns once it has ended.

discord.js counterpart: VoiceConnection#destroy; songbird: Call::leave.

§Errors

When the VoiceGateway could not send Opcode 4: the call still ends, but Discord may show the bot in the channel until its gateway session ends.

Trait Implementations§

Source§

impl Clone for Call

Source§

fn clone(&self) -> Call

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Call

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Call

§

impl !UnwindSafe for Call

§

impl Freeze for Call

§

impl Send for Call

§

impl Sync for Call

§

impl Unpin for Call

§

impl UnsafeUnpin for Call

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Classify for T

§

type Classified = T

§

fn classify(self) -> T

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> Declassify for T

§

type Declassified = T

§

fn declassify(self) -> T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more