aboutsummaryrefslogtreecommitdiff
path: root/src/commands/playback
diff options
context:
space:
mode:
authorNathan Perry <avaglir@gmail.com>2018-04-05 20:53:37 -0400
committerNathan Perry <avaglir@gmail.com>2018-04-05 20:53:37 -0400
commit1fda857d25c3d33e593951eef3ce713fa69a7025 (patch)
tree23f714c3fc7c7233e498307480109f3ab2f779e3 /src/commands/playback
parent82aefce4f7c5f24730fb5da22ee426988ae4301a (diff)
start to integrate db support with commands
Diffstat (limited to 'src/commands/playback')
-rw-r--r--src/commands/playback/mod.rs51
-rw-r--r--src/commands/playback/types.rs29
2 files changed, 61 insertions, 19 deletions
diff --git a/src/commands/playback/mod.rs b/src/commands/playback/mod.rs
index 1d4ee96..a13ad36 100644
--- a/src/commands/playback/mod.rs
+++ b/src/commands/playback/mod.rs
@@ -1,9 +1,37 @@
-use super::*;
+use either::{Left, Right};
+use serenity::voice::{LockedAudio, ytdl};
+use super::*;
pub use self::types::*;
mod types;
+pub trait CtxExt {
+ fn currently_playing(&self) -> bool;
+ fn users_listening(&self) -> Result<bool>;
+}
+
+impl CtxExt for Context {
+ fn currently_playing(&self) -> bool {
+ let queue_lock = self.data.lock().get::<PlayQueue>().cloned().unwrap();
+ let play_queue = queue_lock.read().unwrap();
+ play_queue.playing.is_none()
+ }
+
+ fn users_listening(&self) -> Result<bool> {
+ let channel_id = ChannelId(must_env_lookup::<u64>("VOICE_CHANNEL"));
+ let channel = channel_id.get()?;
+ let res = channel.guild()
+ .and_then(|ch| ch.read().guild())
+ .map(|g| (&g.read().voice_states)
+ .into_iter()
+ .any(|(_, state)| state.channel_id == Some(channel_id)))
+ .unwrap_or(false);
+
+ Ok(res)
+ }
+}
+
pub fn _play(ctx: &Context, msg: &Message, url: &str) -> Result<()> {
debug!("playing '{}'", url);
if !url.starts_with("http") {
@@ -16,16 +44,12 @@ pub fn _play(ctx: &Context, msg: &Message, url: &str) -> Result<()> {
return Ok(());
}
- trace!("acquiring queue lock");
-
let queue_lock = ctx.data.lock().get::<PlayQueue>().cloned().unwrap();
let mut play_queue = queue_lock.write().unwrap();
- trace!("queue lock acquired");
-
play_queue.queue.push_back(PlayArgs{
initiator: msg.author.name.clone(),
- url: url.to_owned(),
+ data: Left(url.to_owned()),
sender_channel: msg.channel_id,
});
@@ -169,7 +193,13 @@ command!(list(ctx, msg) {
Some(ref info) => {
let audio = info.audio.lock();
let status = if audio.playing { "playing" } else { "paused:" };
- send(msg.channel_id, &format!("Currently {} `{}` ({})", status, info.init_args.url, info.init_args.initiator), msg.tts)?;
+
+ let playing_info = match info.init_args.data {
+ Left(ref url) => format!(" `{}`", url),
+ Right(_) => "memeing".to_owned(),
+ };
+
+ send(msg.channel_id, &format!("Currently {} {} ({})", status, playing_info, info.init_args.initiator), msg.tts)?;
},
None => {
debug!("`list` called with no items in queue");
@@ -179,6 +209,11 @@ command!(list(ctx, msg) {
}
play_queue.queue.iter().for_each(|info| {
- channel.say(&format!("`{}` ({})", info.url, info.initiator)).unwrap();
+ let playing_info = match info.data {
+ Left(ref url) => format!("`{}`", url),
+ Right(_) => "meme".to_owned(),
+ };
+
+ channel.say(&format!("{} ({})", playing_info, info.initiator)).unwrap();
});
});
diff --git a/src/commands/playback/types.rs b/src/commands/playback/types.rs
index 41592ec..b9e1778 100644
--- a/src/commands/playback/types.rs
+++ b/src/commands/playback/types.rs
@@ -2,6 +2,9 @@ use serenity::client::bridge::voice::ClientVoiceManager;
use typemap::Key;
use std::sync::{Arc, RwLock};
use std::collections::VecDeque;
+
+use either::{Either, Left, Right};
+
use super::*;
pub struct VoiceManager;
@@ -19,7 +22,7 @@ impl VoiceManager {
#[derive(Clone, Debug)]
pub struct PlayArgs {
- pub url: String,
+ pub data: Either<String, Vec<u8>>,
pub initiator: String,
pub sender_channel: ChannelId,
}
@@ -90,7 +93,7 @@ impl PlayQueue {
let mut manager = voice_manager.lock();
manager.leave(*TARGET_GUILD_ID);
- debug!("disconnected due to inactivity");
+ debug!("disconnected because playback finished");
}
continue;
}
@@ -98,18 +101,22 @@ impl PlayQueue {
let mut queue = queue_lck.write().unwrap();
let item = queue.queue.pop_front().unwrap();
- trace!("checking ytdl for: {}", item.url);
-
- let src = match ytdl(&item.url) {
- Ok(src) => src,
- Err(e) => {
- error!("bad link: {}; {:?}", &item.url, e);
- let _ = send(item.sender_channel, &format!("what the fuck"), false);
- continue;
+ let src = match item.data {
+ Left(ref url) => {
+ match ytdl(url) {
+ Ok(src) => src,
+ Err(e) => {
+ error!("bad link: {}; {:?}", url, e);
+ let _ = send(item.sender_channel, "what the fuck", false);
+ continue;
+ }
+ }
+ },
+ Right(ref vec) => {
+ ::serenity::voice::opus(true, ::std::io::Cursor::new(vec.clone()))
}
};
- trace!("got ytdl item for {}", item.url);
let mut manager = voice_manager.lock();
let handler = manager.join(*TARGET_GUILD_ID, must_env_lookup::<u64>("VOICE_CHANNEL"));