From 32aa189511c695293b27afa6cfd8b01abd5572b1 Mon Sep 17 00:00:00 2001 From: jude-lafitteIII Date: Tue, 19 May 2020 16:29:57 +0100 Subject: [PATCH] cargo config. extracted play functionality. volume can be modified hopefully --- .cargo/config | 2 ++ src/main.rs | 69 +++++++++++++++++++++++++++++++-------------------- 2 files changed, 44 insertions(+), 27 deletions(-) create mode 100644 .cargo/config diff --git a/.cargo/config b/.cargo/config new file mode 100644 index 0000000..fb7281e --- /dev/null +++ b/.cargo/config @@ -0,0 +1,2 @@ +[build] +target-dir = "/home/jude/.rust_build/soundfx-rs" diff --git a/src/main.rs b/src/main.rs index 006913c..ab2782c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,8 +28,10 @@ use serenity::{ * }, voice::{ + AudioSource, ffmpeg, - pcm + Handler as VoiceHandler, + pcm, }, }; @@ -47,7 +49,10 @@ use tokio::{ fs::File, io::empty, process::Command, - sync::Mutex, + sync::{ + Mutex, + MutexGuard + }, time, }; @@ -287,7 +292,7 @@ SELECT * } } - async fn store_sound_source(&self) -> Result> { + async fn store_sound_source(&self) -> Result, Box> { let caching_location = env::var("CACHING_LOCATION").unwrap_or(String::from("/tmp")); let path_name = format!("{}/sound-{}", caching_location, self.id); @@ -301,10 +306,10 @@ SELECT * file.write_all(self.src.as_ref()).await?; } - Ok(path_name) + Ok(ffmpeg(path_name).await?) } - async fn count_user_sounds(user_id: u64, db_pool: MySqlPool) -> Result { + async fn count_user_sounds(user_id: u64, db_pool: MySqlPool) -> Result { let c = sqlx::query!( " SELECT COUNT(1) as count @@ -425,7 +430,7 @@ DELETE match sqlx::query!( " INSERT INTO sounds (name, server_id, uploader_id, public, src) -VALUES (?, ?, ?, 1, ?) + VALUES (?, ?, ?, 1, ?) ", name, server_id, user_id, data ) @@ -471,7 +476,7 @@ SELECT * } struct GuildData { - id: u64, + pub id: u64, pub name: Option, pub prefix: String, pub volume: u8, @@ -483,8 +488,8 @@ impl GuildData { GuildData, " SELECT * -FROM servers -WHERE id = ? + FROM servers + WHERE id = ? ", guild_id ) .fetch_one(&db_pool) @@ -501,7 +506,7 @@ WHERE id = ? sqlx::query!( " INSERT INTO servers (id, name) -VALUES (?, ?) + VALUES (?, ?) ", guild.id.as_u64(), guild.name ) .execute(&db_pool) @@ -629,6 +634,26 @@ async fn disconnect_from_inactive(voice_manager_mutex: Arc CommandResult { + + async fn play_audio(sound: &mut Sound, guild: GuildData, handler: &mut VoiceHandler, mut voice_guilds: MutexGuard<'_, HashSet>, pool: MySqlPool) + -> Result<(), Box> { + + let audio = handler.play_only(sound.store_sound_source().await?); + + { + let mut locked = audio.lock().await; + + locked.volume(guild.volume as f32 / 100.0); + } + + sound.plays += 1; + sound.commit(pool).await?; + + voice_guilds.insert(GuildId(guild.id)); + + Ok(()) + } + let guild = match msg.guild(&ctx.cache).await { Some(guild) => guild, @@ -661,8 +686,6 @@ async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult { match sound_res { Some(sound) => { - let fp = sound.store_sound_source().await?; - let voice_manager_lock = ctx.data.read().await .get::().cloned().expect("Could not get VoiceManager from data"); @@ -671,29 +694,21 @@ async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult { let voice_guilds_lock = ctx.data.read().await .get::().cloned().expect("Could not get VoiceGuilds from data"); - let mut voice_guilds = voice_guilds_lock.lock().await; + let voice_guilds = voice_guilds_lock.lock().await; + + let guild_data = GuildData::get_from_id(*guild_id.as_u64(), pool.clone()).await.unwrap(); match voice_manager.get_mut(guild_id) { Some(handler) => { // play sound - handler.play_only(ffmpeg(fp).await?); - - sound.plays += 1; - sound.commit(pool).await?; - - voice_guilds.insert(guild_id); + play_audio(sound, guild_data, handler, voice_guilds, pool).await?; } None => { // try & join a voice channel match voice_manager.join(guild_id, user_channel) { Some(handler) => { - handler.play_only(ffmpeg(fp).await?); - - sound.plays += 1; - sound.commit(pool).await?; - - voice_guilds.insert(guild_id); + play_audio(sound, guild_data, handler, voice_guilds, pool).await?; } None => { @@ -1135,7 +1150,7 @@ async fn show_popular_sounds(ctx: &Context, msg: &Message, _args: Args) -> Comma Sound, " SELECT * FROM sounds -ORDER BY plays + ORDER BY plays " ) .fetch_all(&pool) @@ -1155,7 +1170,7 @@ async fn show_random_sounds(ctx: &Context, msg: &Message, _args: Args) -> Comman Sound, " SELECT * FROM sounds -ORDER BY rand() + ORDER BY rand() " ) .fetch_all(&pool)