From 87301e421264a3afe8d46b10304c67313b0577fd Mon Sep 17 00:00:00 2001 From: jellywx Date: Fri, 25 Jun 2021 18:45:59 +0100 Subject: [PATCH] soundboard working pretty nicely --- regex_command_attr/src/lib.rs | 11 +- regex_command_attr/src/structures.rs | 2 +- src/framework.rs | 11 +- src/main.rs | 184 ++++++++++++++++++++++++++- src/sound.rs | 1 + 5 files changed, 193 insertions(+), 16 deletions(-) diff --git a/regex_command_attr/src/lib.rs b/regex_command_attr/src/lib.rs index f02df84..be33c48 100644 --- a/regex_command_attr/src/lib.rs +++ b/regex_command_attr/src/lib.rs @@ -68,7 +68,7 @@ pub fn command(attr: TokenStream, input: TokenStream) -> TokenStream { _ => { match_options!(name, values, options, span => [ aliases; - usage; + group; required_permissions; allow_slash ]); @@ -79,7 +79,7 @@ pub fn command(attr: TokenStream, input: TokenStream) -> TokenStream { let Options { aliases, description, - usage, + group, examples, required_permissions, allow_slash, @@ -108,7 +108,10 @@ pub fn command(attr: TokenStream, input: TokenStream) -> TokenStream { let arg_idents = cmd_args .iter() - .map(|arg| n.with_suffix(arg.name.as_str()).with_suffix(ARG)) + .map(|arg| { + n.with_suffix(arg.name.replace(" ", "_").replace("-", "_").as_str()) + .with_suffix(ARG) + }) .collect::>(); let mut tokens = cmd_args @@ -146,7 +149,7 @@ pub fn command(attr: TokenStream, input: TokenStream) -> TokenStream { fun: #name, names: &[#_name, #(#aliases),*], desc: #description, - usage: #usage, + group: #group, examples: &[#(#examples),*], required_permissions: #required_permissions, allow_slash: #allow_slash, diff --git a/regex_command_attr/src/structures.rs b/regex_command_attr/src/structures.rs index f552e2b..9ca087f 100644 --- a/regex_command_attr/src/structures.rs +++ b/regex_command_attr/src/structures.rs @@ -290,7 +290,7 @@ impl Default for Arg { pub(crate) struct Options { pub aliases: Vec, pub description: String, - pub usage: AsOption, + pub group: AsOption, pub examples: Vec, pub required_permissions: PermissionLevel, pub allow_slash: bool, diff --git a/src/framework.rs b/src/framework.rs index a177b7c..a4035ee 100644 --- a/src/framework.rs +++ b/src/framework.rs @@ -108,11 +108,9 @@ impl CreateGenericResponse { pub fn embed &mut CreateEmbed>(mut self, f: F) -> Self { let mut embed = CreateEmbed::default(); - f(&mut embed); self.embed = Some(embed); - self } @@ -121,11 +119,9 @@ impl CreateGenericResponse { f: F, ) -> Self { let mut components = CreateComponents::default(); - f(&mut components); self.components = Some(components); - self } } @@ -344,12 +340,15 @@ impl Arg { pub struct Command { pub fun: CommandFn, + pub names: &'static [&'static str], + pub desc: &'static str, - pub usage: Option<&'static str>, pub examples: &'static [&'static str], - pub required_permissions: PermissionLevel, + pub group: Option<&'static str>, + pub allow_slash: bool, + pub required_permissions: PermissionLevel, pub args: &'static [&'static Arg], } diff --git a/src/main.rs b/src/main.rs index 0929b2e..74e6c27 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,6 +48,7 @@ use dashmap::DashMap; use std::{collections::HashMap, convert::TryFrom, env, sync::Arc, time::Duration}; +use serenity::model::prelude::InteractionResponseType; use tokio::sync::{MutexGuard, RwLock}; struct MySQL; @@ -1137,12 +1138,170 @@ async fn list_sounds( #[command("soundboard")] #[aliases("board")] -#[description("Get a menu of sounds in this server with buttons to play them")] +#[description("Get a menu of sounds with buttons to play them")] +#[arg( + name = "1", + description = "Query for sound button 1", + kind = "String", + required = false +)] +#[arg( + name = "2", + description = "Query for sound button 2", + kind = "String", + required = false +)] +#[arg( + name = "3", + description = "Query for sound button 3", + kind = "String", + required = false +)] +#[arg( + name = "4", + description = "Query for sound button 4", + kind = "String", + required = false +)] +#[arg( + name = "5", + description = "Query for sound button 5", + kind = "String", + required = false +)] +#[arg( + name = "6", + description = "Query for sound button 6", + kind = "String", + required = false +)] +#[arg( + name = "7", + description = "Query for sound button 7", + kind = "String", + required = false +)] +#[arg( + name = "8", + description = "Query for sound button 8", + kind = "String", + required = false +)] +#[arg( + name = "9", + description = "Query for sound button 9", + kind = "String", + required = false +)] +#[arg( + name = "10", + description = "Query for sound button 10", + kind = "String", + required = false +)] +#[arg( + name = "11", + description = "Query for sound button 11", + kind = "String", + required = false +)] +#[arg( + name = "12", + description = "Query for sound button 12", + kind = "String", + required = false +)] +#[arg( + name = "13", + description = "Query for sound button 13", + kind = "String", + required = false +)] +#[arg( + name = "14", + description = "Query for sound button 14", + kind = "String", + required = false +)] +#[arg( + name = "15", + description = "Query for sound button 15", + kind = "String", + required = false +)] +#[arg( + name = "16", + description = "Query for sound button 16", + kind = "String", + required = false +)] +#[arg( + name = "17", + description = "Query for sound button 17", + kind = "String", + required = false +)] +#[arg( + name = "18", + description = "Query for sound button 18", + kind = "String", + required = false +)] +#[arg( + name = "19", + description = "Query for sound button 19", + kind = "String", + required = false +)] +#[arg( + name = "20", + description = "Query for sound button 20", + kind = "String", + required = false +)] +#[arg( + name = "21", + description = "Query for sound button 21", + kind = "String", + required = false +)] +#[arg( + name = "22", + description = "Query for sound button 22", + kind = "String", + required = false +)] +#[arg( + name = "23", + description = "Query for sound button 23", + kind = "String", + required = false +)] +#[arg( + name = "24", + description = "Query for sound button 24", + kind = "String", + required = false +)] +#[arg( + name = "25", + description = "Query for sound button 25", + kind = "String", + required = false +)] async fn soundboard( ctx: &Context, invoke: &(dyn CommandInvoke + Sync + Send), - _args: Args, + args: Args, ) -> CommandResult { + if let Some(interaction) = invoke.interaction() { + let _ = interaction + .create_interaction_response(&ctx, |r| { + r.kind(InteractionResponseType::DeferredChannelMessageWithSource) + }) + .await; + } + let pool = ctx .data .read() @@ -1151,13 +1310,28 @@ async fn soundboard( .cloned() .expect("Could not get SQLPool from data"); - let sounds = Sound::get_guild_sounds(invoke.guild_id().unwrap(), pool).await?; + let mut sounds = vec![]; + + for n in 1..25 { + let search = Sound::search_for_sound( + args.named(&n.to_string()).unwrap_or(&"".to_string()), + invoke.guild_id().unwrap(), + invoke.author_id(), + pool.clone(), + true, + ) + .await?; + + if let Some(sound) = search.first() { + sounds.push(sound.clone()); + } + } invoke - .respond( + .followup( ctx.http.clone(), CreateGenericResponse::new() - .content("Select a sound from below:") + .content("**Play a sound:**") .components(|c| { for row in sounds.as_slice().chunks(5) { let mut action_row: CreateActionRow = Default::default(); diff --git a/src/sound.rs b/src/sound.rs index d85a857..6e86c11 100644 --- a/src/sound.rs +++ b/src/sound.rs @@ -111,6 +111,7 @@ WHERE } } +#[derive(Clone)] pub struct Sound { pub name: String, pub id: u32,