From ffba60b278162707bc4eb004c3bfb6b2e9595213 Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Wed, 8 May 2024 12:55:35 -0400 Subject: rework to use songbird --- src/commands/meme/mod.rs | 63 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 18 deletions(-) (limited to 'src/commands/meme/mod.rs') diff --git a/src/commands/meme/mod.rs b/src/commands/meme/mod.rs index 31d9b78..24fc50d 100644 --- a/src/commands/meme/mod.rs +++ b/src/commands/meme/mod.rs @@ -3,6 +3,7 @@ use log::debug; use rand::random; use serenity::{ all::ReactionType, + async_trait, builder::{ CreateAttachment, CreateMessage, @@ -14,13 +15,21 @@ use serenity::{ model::channel::Message, prelude::*, }; +use songbird::input::{ + core::io::MediaSource, + AudioStream, + AudioStreamError, + Compose, + Input, +}; use crate::{ - audio::{ - PlayArgs, - PlayQueue, + commands::songbird, + db::{ + Audio, + Meme, }, - db::Meme, + CONFIG, }; pub use self::{ @@ -94,27 +103,45 @@ async fn send_meme( }, }; - // note: slight edge-case race condition here: there could have been something queued since we - // checked whether anything was playing. not a significant negative impact and unlikely, so i'm - // not worrying about it if let Some(audio) = audio { let audio = audio?; - { - let queue_lock = ctx.data.write().await.get::().cloned().unwrap(); - let mut play_queue = queue_lock.write().unwrap(); - - play_queue.meme_queue.push_back(PlayArgs { - initiator: msg.author.name.clone(), - data: ::either::Right(audio.data.clone()), - sender_channel: msg.channel_id, - start: None, - end: None, - }); + let (_sb, call) = songbird(ctx, msg).await?; + let mut call = call.lock().await; + + if call.current_channel().is_none() { + call.join(CONFIG.discord.voice_channel()).await?; } + call.enqueue_input(Input::Lazy(Box::new(audio))).await; + msg.react(ctx, ReactionType::Unicode("📣".to_owned())).await?; } Ok(()) } + +#[async_trait] +impl Compose for Audio { + fn create(&mut self) -> Result>, AudioStreamError> { + let ms = std::io::Cursor::new(self.data.clone()); + let ms: Box = Box::new(ms); + + Ok(AudioStream { + input: ms, + hint: None, + }) + } + + #[inline] + async fn create_async( + &mut self, + ) -> Result>, AudioStreamError> { + self.create() + } + + #[inline] + fn should_create_async(&self) -> bool { + false + } +} -- cgit v1.3.1