From 1cc1068788717998f22345efc30a1ef7f73f5d9f Mon Sep 17 00:00:00 2001 From: jellywx Date: Sat, 18 Sep 2021 13:09:14 +0100 Subject: [PATCH] removed popular command. fix for ambience command --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/cmds/manage.rs | 4 +- src/cmds/play.rs | 94 ++++++++++++++++++++++++++----------------- src/cmds/search.rs | 50 ++--------------------- src/event_handlers.rs | 2 +- src/guild_data.rs | 4 +- src/main.rs | 29 +------------ src/sound.rs | 17 ++++---- 9 files changed, 75 insertions(+), 129 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cd42598..f9baebc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1706,7 +1706,7 @@ dependencies = [ [[package]] name = "soundfx-rs" -version = "1.4.2" +version = "1.4.3" dependencies = [ "dashmap", "dotenv", diff --git a/Cargo.toml b/Cargo.toml index 590126d..ed3dd3b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "soundfx-rs" -version = "1.4.2" +version = "1.4.3" authors = ["jellywx "] edition = "2018" diff --git a/src/cmds/manage.rs b/src/cmds/manage.rs index 95aa409..6406629 100644 --- a/src/cmds/manage.rs +++ b/src/cmds/manage.rs @@ -155,7 +155,7 @@ pub async fn upload_new_sound( invoke.respond( ctx.http.clone(), CreateGenericResponse::new().content(format!( - "You have reached the maximum number of sounds ({}). Either delete some with `?delete` or join our Patreon for unlimited uploads at **https://patreon.com/jellywx**", + "You have reached the maximum number of sounds ({}). Either delete some with `/delete` or join our Patreon for unlimited uploads at **https://patreon.com/jellywx**", *MAX_SOUNDS, ))).await?; } @@ -170,7 +170,7 @@ pub async fn upload_new_sound( .await?; } } else { - invoke.respond(ctx.http.clone(), CreateGenericResponse::new().content("Usage: `?upload `. Please ensure the name provided is less than 20 characters in length")).await?; + invoke.respond(ctx.http.clone(), CreateGenericResponse::new().content("Usage: `/upload `. Please ensure the name provided is less than 20 characters in length")).await?; } Ok(()) diff --git a/src/cmds/play.rs b/src/cmds/play.rs index 5a7a5ca..397c518 100644 --- a/src/cmds/play.rs +++ b/src/cmds/play.rs @@ -108,60 +108,78 @@ pub async fn play_ambience( match channel_to_join { Some(user_channel) => { - let search_name = args.named("name").unwrap().to_lowercase(); let audio_index = ctx.data.read().await.get::().cloned().unwrap(); - if let Some(filename) = audio_index.get(&search_name) { - let (track, track_handler) = create_player( - Input::try_from( - Memory::new(ffmpeg(format!("audio/{}", filename)).await.unwrap()).unwrap(), - ) - .unwrap(), - ); + if let Some(search_name) = args.named("name") { + if let Some(filename) = audio_index.get(search_name) { + let (track, track_handler) = create_player( + Input::try_from( + Memory::new(ffmpeg(format!("audio/{}", filename)).await.unwrap()) + .unwrap(), + ) + .unwrap(), + ); - let (call_handler, _) = join_channel(ctx, guild.clone(), user_channel).await; - let guild_data = ctx.guild_data(guild).await.unwrap(); + let (call_handler, _) = join_channel(ctx, guild.clone(), user_channel).await; + let guild_data = ctx.guild_data(guild).await.unwrap(); - { - let mut lock = call_handler.lock().await; + { + let mut lock = call_handler.lock().await; - lock.play(track); + lock.play(track); + } + + let _ = track_handler.set_volume(guild_data.read().await.volume as f32 / 100.0); + let _ = track_handler.add_event( + Event::Periodic( + track_handler.metadata().duration.unwrap() - Duration::from_millis(200), + None, + ), + RestartTrack {}, + ); + + invoke + .respond( + ctx.http.clone(), + CreateGenericResponse::new() + .content(format!("Playing ambience **{}**", search_name)), + ) + .await?; + } else { + invoke + .respond( + ctx.http.clone(), + CreateGenericResponse::new().embed(|e| { + e.title("Not Found").description(format!( + "Could not find ambience sound by name **{}** + +__Available ambience sounds:__ +{}", + search_name, + audio_index + .keys() + .into_iter() + .map(|i| i.as_str()) + .collect::>() + .join("\n") + )) + }), + ) + .await?; } - - let _ = track_handler.set_volume(guild_data.read().await.volume as f32 / 100.0); - let _ = track_handler.add_event( - Event::Periodic( - track_handler.metadata().duration.unwrap() - Duration::from_millis(200), - None, - ), - RestartTrack {}, - ); - - invoke - .respond( - ctx.http.clone(), - CreateGenericResponse::new() - .content(format!("Playing ambience **{}**", search_name)), - ) - .await?; } else { invoke .respond( ctx.http.clone(), CreateGenericResponse::new().embed(|e| { - e.title("Not Found").description(format!( - "Could not find ambience sound by name **{}** - -__Available ambience sounds:__ -{}", - search_name, + e.title("Available Sounds").description( audio_index .keys() .into_iter() .map(|i| i.as_str()) .collect::>() - .join("\n") - )) + .join("\n"), + ) }), ) .await?; diff --git a/src/cmds/search.rs b/src/cmds/search.rs index 211fc1d..2fc468a 100644 --- a/src/cmds/search.rs +++ b/src/cmds/search.rs @@ -14,13 +14,7 @@ fn format_search_results(search_results: Vec) -> CreateGenericResponse { let field_iter = search_results .iter() .take(25) - .map(|item| { - ( - &item.name, - format!("ID: {}\nPlays: {}", item.id, item.plays), - true, - ) - }) + .map(|item| (&item.name, format!("ID: {}", item.id), true)) .filter(|item| { current_character_count += item.0.len() + item.1.len(); @@ -58,11 +52,11 @@ pub async fn list_sounds( let mut message_buffer; if args.named("me").map(|i| i.to_owned()) == Some("me".to_string()) { - sounds = Sound::get_user_sounds(invoke.author_id(), pool).await?; + sounds = Sound::user_sounds(invoke.author_id(), pool).await?; message_buffer = "All your sounds: ".to_string(); } else { - sounds = Sound::get_guild_sounds(invoke.guild_id().unwrap(), pool).await?; + sounds = Sound::guild_sounds(invoke.guild_id().unwrap(), pool).await?; message_buffer = "All sounds on this server: ".to_string(); } @@ -141,42 +135,6 @@ pub async fn search_sounds( Ok(()) } -#[command("popular")] -#[group("Search")] -#[description("Show popular sounds")] -pub async fn show_popular_sounds( - ctx: &Context, - invoke: &(dyn CommandInvoke + Sync + Send), - _args: Args, -) -> CommandResult { - let pool = ctx - .data - .read() - .await - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - let search_results = sqlx::query_as_unchecked!( - Sound, - " -SELECT name, id, plays, public, server_id, uploader_id - FROM sounds - WHERE public = 1 - ORDER BY plays DESC - LIMIT 25 - " - ) - .fetch_all(&pool) - .await?; - - invoke - .respond(ctx.http.clone(), format_search_results(search_results)) - .await?; - - Ok(()) -} - #[command("random")] #[group("Search")] #[description("Show a page of random sounds")] @@ -196,7 +154,7 @@ pub async fn show_random_sounds( let search_results = sqlx::query_as_unchecked!( Sound, " -SELECT name, id, plays, public, server_id, uploader_id +SELECT name, id, public, server_id, uploader_id FROM sounds WHERE public = 1 ORDER BY rand() diff --git a/src/event_handlers.rs b/src/event_handlers.rs index d4429f2..d39554e 100644 --- a/src/event_handlers.rs +++ b/src/event_handlers.rs @@ -137,7 +137,7 @@ impl EventHandler for Handler { let mut sound = sqlx::query_as_unchecked!( Sound, " -SELECT name, id, plays, public, server_id, uploader_id +SELECT name, id, public, server_id, uploader_id FROM sounds WHERE id = ? ", diff --git a/src/guild_data.rs b/src/guild_data.rs index 1f77924..4dad567 100644 --- a/src/guild_data.rs +++ b/src/guild_data.rs @@ -44,7 +44,7 @@ impl CtxGuildData for Context { } else { let pool = self.data.read().await.get::().cloned().unwrap(); - match GuildData::get_from_id(guild_id, pool).await { + match GuildData::from_id(guild_id, pool).await { Ok(d) => { let lock = Arc::new(RwLock::new(d)); @@ -62,7 +62,7 @@ impl CtxGuildData for Context { } impl GuildData { - pub async fn get_from_id>( + pub async fn from_id>( guild_id: G, db_pool: MySqlPool, ) -> Result { diff --git a/src/main.rs b/src/main.rs index 192665e..f2ca6e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ mod framework; mod guild_data; mod sound; -use std::{collections::HashMap, env, sync::Arc, time::Duration}; +use std::{collections::HashMap, env, sync::Arc}; use dashmap::DashMap; use dotenv::dotenv; @@ -92,9 +92,6 @@ async fn play_audio( call_handler.play(track); - sound.plays += 1; - sound.commit(mysql_pool).await?; - Ok(track_handler) } @@ -262,7 +259,6 @@ async fn main() -> Result<(), Box> { // search commands .add_command(&cmds::search::LIST_SOUNDS_COMMAND) .add_command(&cmds::search::SEARCH_SOUNDS_COMMAND) - .add_command(&cmds::search::SHOW_POPULAR_SOUNDS_COMMAND) .add_command(&cmds::search::SHOW_RANDOM_SOUNDS_COMMAND); if audio_index.is_some() { @@ -310,29 +306,6 @@ async fn main() -> Result<(), Box> { framework_arc.build_slash(&client.cache_and_http.http).await; - let shard_manager = client.shard_manager.clone(); - tokio::spawn(async move { - loop { - { - let mut shards = shard_manager - .lock() - .await - .runners - .lock() - .await - .iter() - .map(|(id, shard)| format!("\t{} {}", id.0, shard.stage)) - .collect::>(); - - shards.sort(); - - info!("Shard Status: [\n{}\n]", shards.join("\n")); - } - - tokio::time::sleep(Duration::from_secs(60)).await; - } - }); - if let Ok((Some(lower), Some(upper))) = env::var("SHARD_RANGE").map(|sr| { let mut split = sr .split(',') diff --git a/src/sound.rs b/src/sound.rs index 04e3e29..d44c426 100644 --- a/src/sound.rs +++ b/src/sound.rs @@ -110,7 +110,6 @@ WHERE pub struct Sound { pub name: String, pub id: u32, - pub plays: u32, pub public: bool, pub server_id: u64, pub uploader_id: Option, @@ -151,7 +150,7 @@ impl Sound { let sound = sqlx::query_as_unchecked!( Self, " -SELECT name, id, plays, public, server_id, uploader_id +SELECT name, id, public, server_id, uploader_id FROM sounds WHERE id = ? AND ( public = 1 OR @@ -175,7 +174,7 @@ SELECT name, id, plays, public, server_id, uploader_id sound = sqlx::query_as_unchecked!( Self, " -SELECT name, id, plays, public, server_id, uploader_id +SELECT name, id, public, server_id, uploader_id FROM sounds WHERE name = ? AND ( public = 1 OR @@ -196,7 +195,7 @@ SELECT name, id, plays, public, server_id, uploader_id sound = sqlx::query_as_unchecked!( Self, " -SELECT name, id, plays, public, server_id, uploader_id +SELECT name, id, public, server_id, uploader_id FROM sounds WHERE name LIKE CONCAT('%', ?, '%') AND ( public = 1 OR @@ -311,12 +310,10 @@ SELECT COUNT(1) as count " UPDATE sounds SET - plays = ?, public = ? WHERE id = ? ", - self.plays, self.public, self.id ) @@ -408,14 +405,14 @@ INSERT INTO sounds (name, server_id, uploader_id, public, src) } } - pub async fn get_user_sounds>( + pub async fn user_sounds>( user_id: U, db_pool: MySqlPool, ) -> Result, Box> { let sounds = sqlx::query_as_unchecked!( Sound, " -SELECT name, id, plays, public, server_id, uploader_id +SELECT name, id, public, server_id, uploader_id FROM sounds WHERE uploader_id = ? ", @@ -427,14 +424,14 @@ SELECT name, id, plays, public, server_id, uploader_id Ok(sounds) } - pub async fn get_guild_sounds>( + pub async fn guild_sounds>( guild_id: G, db_pool: MySqlPool, ) -> Result, Box> { let sounds = sqlx::query_as_unchecked!( Sound, " -SELECT name, id, plays, public, server_id, uploader_id +SELECT name, id, public, server_id, uploader_id FROM sounds WHERE server_id = ? ",