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
impl Call
Sourcepub fn guild_id(&self) -> GuildId
pub fn guild_id(&self) -> GuildId
The guild of the call.
discord.js counterpart: VoiceConnection#joinConfig.guildId.
Sourcepub fn channel_id(&self) -> Option<ChannelId>
pub fn channel_id(&self) -> Option<ChannelId>
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.
Sourcepub fn play(&self, input: Input) -> TrackHandle
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?);Sourcepub fn enqueue(&self, input: Input) -> TrackHandle
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));
}Sourcepub fn queue(&self) -> TrackQueue
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).
Sourcepub fn subscribe(&self, user_id: UserId) -> VoiceStream
pub fn subscribe(&self, user_id: UserId) -> 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");Sourcepub fn subscribe_with(
&self,
user_id: UserId,
options: ReceiveOptions,
) -> VoiceStream
pub fn subscribe_with( &self, user_id: UserId, 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);Sourcepub fn events(&self) -> CallEvents
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.
Sourcepub async fn leave(&self) -> Result<(), Box<dyn Error + Send + Sync>>
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> Classify for T
impl<T> Classify for T
type Classified = T
fn classify(self) -> T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Declassify for T
impl<T> Declassify for T
type Declassified = T
fn declassify(self) -> T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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