aboutsummaryrefslogtreecommitdiff
path: root/src/model/voice.rs
diff options
context:
space:
mode:
authorAustin Hellyer <[email protected]>2016-11-29 11:22:33 -0800
committerAustin Hellyer <[email protected]>2016-11-29 11:22:09 -0800
commitb7f70c6252125a1739066b531ea9e5dff07592a1 (patch)
tree9845d27a7c978d5488bf5bd8ffc04cfbcd184448 /src/model/voice.rs
parentRemove duplicated gateway logic (diff)
downloadserenity-b7f70c6252125a1739066b531ea9e5dff07592a1.tar.xz
serenity-b7f70c6252125a1739066b531ea9e5dff07592a1.zip
Add initial audio support
Audio can be played with support by passing one of the following into the `Handler::play` method: `serenity::ext::voice::{ffmpeg, pcm, ytdl}` functions, where - `ffmpeg` accepts a path (such as a `File`); - `pcm` accepts a raw reader source; - `ytdl` accepts a URI, which works with everything youtube-dl supports: <https://github.com/rg3/youtube-dl/blob/master/docs/supportedsites.md> The source can be stopped via [`Handler::stop`]. Receive is supported through [`Handler::listen`], which accepts a `serenity::ext::voice::AudioReceiver` implementation. An example is provided in the form of the file located at `./examples/07_voice.rs`, which can be run by cloning the repo and performing the command `cargo run --example 07_voice`. Prior to running the command, set a bot token as the value of the env variable `DISCORD_TOKEN`. The example supports: - `deafen`: deafens the bot; - `join`: joins a voice channel by ID. The example is a primitive implementation, and requires the ID of the channel to be passed to the bot as a command of `~join 131937933270712320`; - `leave`: leaves the current voice channel, if in one; - `mute`: mutes the bot and will continue to play source audio; - `play`: plays source audio from a URI, through a command like `~play https://www.youtube.com/watch?v=5KJjBRm0ElA`; - `ping`: responds with "Pong!" to ensure the bot is working properly; - `undeafen`: undeafens the bot, if that's actually a word; - `unmute`: unmutes the bot. Documentation for audio can be found at: <https://serenity.zey.moe/serenity/ext/voice/index.html>
Diffstat (limited to 'src/model/voice.rs')
-rw-r--r--src/model/voice.rs23
1 files changed, 9 insertions, 14 deletions
diff --git a/src/model/voice.rs b/src/model/voice.rs
index a888029..61035c6 100644
--- a/src/model/voice.rs
+++ b/src/model/voice.rs
@@ -4,16 +4,7 @@ use ::constants::VoiceOpCode;
use ::internal::prelude::*;
use ::utils::decode_array;
-#[derive(Clone, Debug)]
-pub struct VoiceHandshake {
- pub heartbeat_interval: u64,
- pub ip: Option<String>,
- pub modes: Vec<String>,
- pub port: u16,
- pub ssrc: u32,
-}
-
-#[derive(Clone, Debug)]
+#[derive(Clone, Copy, Debug)]
pub struct VoiceHeartbeat {
pub heartbeat_interval: u64,
}
@@ -21,6 +12,7 @@ pub struct VoiceHeartbeat {
#[derive(Clone, Debug)]
pub struct VoiceHello {
pub heartbeat_interval: u64,
+ pub ip: String,
pub modes: Vec<String>,
pub port: u16,
pub ssrc: u32,
@@ -32,7 +24,7 @@ pub struct VoiceReady {
pub secret_key: Vec<u8>,
}
-#[derive(Clone, Debug)]
+#[derive(Clone, Copy, Debug)]
pub struct VoiceSpeaking {
pub speaking: bool,
pub ssrc: u32,
@@ -41,7 +33,6 @@ pub struct VoiceSpeaking {
#[derive(Clone, Debug)]
pub enum VoiceEvent {
- Handshake(VoiceHandshake),
Heartbeat(VoiceHeartbeat),
Hello(VoiceHello),
Ready(VoiceReady),
@@ -56,16 +47,20 @@ impl VoiceEvent {
let op = req!(try!(remove(&mut value, "op")).as_u64());
let mut map = try!(remove(&mut value, "d").and_then(into_map));
- match try!(VoiceOpCode::from_num(op).ok_or(Error::Client(ClientError::InvalidOpCode))) {
+ let opcode = try!(VoiceOpCode::from_num(op)
+ .ok_or(Error::Client(ClientError::InvalidOpCode)));
+
+ match opcode {
VoiceOpCode::Heartbeat => {
missing!(map, VoiceEvent::Heartbeat(VoiceHeartbeat {
- heartbeat_interval: req!(try!(remove(&mut value, "heartbeat_interval")).as_u64()),
+ heartbeat_interval: req!(try!(remove(&mut map, "heartbeat_interval")).as_u64()),
}))
},
VoiceOpCode::Hello => {
missing!(map, VoiceEvent::Hello(VoiceHello {
heartbeat_interval: req!(try!(remove(&mut map, "heartbeat_interval"))
.as_u64()),
+ ip: try!(remove(&mut map, "ip").and_then(into_string)),
modes: try!(decode_array(try!(remove(&mut map, "modes")),
into_string)),
port: req!(try!(remove(&mut map, "port"))