added some missing args back. added upload to slash commands

This commit is contained in:
jellywx 2021-06-19 12:46:30 +01:00
parent 8d9458ff49
commit 1aed25a74c
2 changed files with 122 additions and 33 deletions

View File

@ -128,6 +128,11 @@ pub trait CommandInvoke {
http: Arc<Http>, http: Arc<Http>,
generic_response: CreateGenericResponse, generic_response: CreateGenericResponse,
) -> SerenityResult<()>; ) -> SerenityResult<()>;
async fn followup(
&self,
http: Arc<Http>,
generic_response: CreateGenericResponse,
) -> SerenityResult<()>;
} }
#[async_trait] #[async_trait]
@ -178,6 +183,25 @@ impl CommandInvoke for Message {
.await .await
.map(|_| ()) .map(|_| ())
} }
async fn followup(
&self,
http: Arc<Http>,
generic_response: CreateGenericResponse,
) -> SerenityResult<()> {
self.channel_id
.send_message(http, |m| {
m.content(generic_response.content);
if let Some(embed) = generic_response.embed {
m.set_embed(embed.clone());
}
m
})
.await
.map(|_| ())
}
} }
#[async_trait] #[async_trait]
@ -234,6 +258,24 @@ impl CommandInvoke for Interaction {
.await .await
.map(|_| ()) .map(|_| ())
} }
async fn followup(
&self,
http: Arc<Http>,
generic_response: CreateGenericResponse,
) -> SerenityResult<()> {
self.create_followup_message(http, |d| {
d.content(generic_response.content);
if let Some(embed) = generic_response.embed {
d.set_embed(embed.clone());
}
d
})
.await
.map(|_| ())
}
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]

View File

@ -787,6 +787,12 @@ async fn change_volume(
#[command("prefix")] #[command("prefix")]
#[required_permissions(Restricted)] #[required_permissions(Restricted)]
#[allow_slash(false)] #[allow_slash(false)]
#[arg(
name = "prefix",
kind = "String",
description = "The new prefix to use for the bot",
required = true
)]
async fn change_prefix( async fn change_prefix(
ctx: &Context, ctx: &Context,
invoke: &(dyn CommandInvoke + Sync + Send), invoke: &(dyn CommandInvoke + Sync + Send),
@ -853,7 +859,7 @@ async fn change_prefix(
} }
#[command("upload")] #[command("upload")]
#[allow_slash(false)] #[description("Upload a new sound to the bot")]
#[arg( #[arg(
name = "name", name = "name",
description = "Name to upload sound to", description = "Name to upload sound to",
@ -865,8 +871,6 @@ async fn upload_new_sound(
invoke: &(dyn CommandInvoke + Sync + Send), invoke: &(dyn CommandInvoke + Sync + Send),
args: Args, args: Args,
) -> CommandResult { ) -> CommandResult {
let msg = invoke.msg().unwrap();
fn is_numeric(s: &String) -> bool { fn is_numeric(s: &String) -> bool {
for char in s.chars() { for char in s.chars() {
if char.is_digit(10) { if char.is_digit(10) {
@ -895,19 +899,20 @@ async fn upload_new_sound(
// need to check the name is not currently in use by the user // need to check the name is not currently in use by the user
let count_name = let count_name =
Sound::count_named_user_sounds(*msg.author.id.as_u64(), &new_name, pool.clone()) Sound::count_named_user_sounds(invoke.author_id().0, &new_name, pool.clone())
.await?; .await?;
if count_name > 0 { if count_name > 0 {
msg.channel_id.say(&ctx, "You are already using that name. Please choose a unique name for your upload.").await?; invoke.respond(ctx.http.clone(), CreateGenericResponse::new().content("You are already using that name. Please choose a unique name for your upload.")).await?;
} else { } else {
// need to check how many sounds user currently has // need to check how many sounds user currently has
let count = Sound::count_user_sounds(*msg.author.id.as_u64(), pool.clone()).await?; let count = Sound::count_user_sounds(invoke.author_id().0, pool.clone()).await?;
let mut permit_upload = true; let mut permit_upload = true;
// need to check if user is patreon or nah // need to check if user is patreon or nah
if count >= *MAX_SOUNDS { if count >= *MAX_SOUNDS {
let patreon_guild_member = let patreon_guild_member = GuildId(*PATREON_GUILD)
GuildId(*PATREON_GUILD).member(ctx, msg.author.id).await; .member(ctx, invoke.author_id())
.await;
if let Ok(member) = patreon_guild_member { if let Ok(member) = patreon_guild_member {
permit_upload = member.roles.contains(&RoleId(*PATREON_ROLE)); permit_upload = member.roles.contains(&RoleId(*PATREON_ROLE));
@ -917,16 +922,20 @@ async fn upload_new_sound(
} }
if permit_upload { if permit_upload {
let attachment = if let Some(attachment) = msg.attachments.get(0) { let attachment = if let Some(attachment) = invoke
Some(attachment.url.clone()) .msg()
.map(|m| m.attachments.get(0).map(|a| a.url.clone()))
.flatten()
{
Some(attachment)
} else { } else {
msg.channel_id.say(&ctx, "Please now upload an audio file under 1MB in size (larger files will be automatically trimmed):").await?; invoke.respond(ctx.http.clone(), CreateGenericResponse::new().content("Please now upload an audio file under 1MB in size (larger files will be automatically trimmed):")).await?;
let reply = msg let reply = invoke
.channel_id .channel_id()
.await_reply(&ctx) .await_reply(&ctx)
.author_id(msg.author.id) .author_id(invoke.author_id())
.timeout(Duration::from_secs(30)) .timeout(Duration::from_secs(120))
.await; .await;
match reply { match reply {
@ -934,15 +943,19 @@ async fn upload_new_sound(
if let Some(attachment) = reply_msg.attachments.get(0) { if let Some(attachment) = reply_msg.attachments.get(0) {
Some(attachment.url.clone()) Some(attachment.url.clone())
} else { } else {
msg.channel_id.say(&ctx, "Please upload 1 attachment following your upload command. Aborted").await?; invoke.followup(ctx.http.clone(), CreateGenericResponse::new().content("Please upload 1 attachment following your upload command. Aborted")).await?;
None None
} }
} }
None => { None => {
msg.channel_id invoke
.say(&ctx, "Upload timed out. Please redo the command") .followup(
ctx.http.clone(),
CreateGenericResponse::new()
.content("Upload timed out. Please redo the command"),
)
.await?; .await?;
None None
@ -954,41 +967,54 @@ async fn upload_new_sound(
match Sound::create_anon( match Sound::create_anon(
&new_name, &new_name,
url.as_str(), url.as_str(),
*msg.guild_id.unwrap().as_u64(), invoke.guild_id().unwrap().0,
*msg.author.id.as_u64(), invoke.author_id().0,
pool, pool,
) )
.await .await
{ {
Ok(_) => { Ok(_) => {
msg.channel_id.say(&ctx, "Sound has been uploaded").await?; invoke
.followup(
ctx.http.clone(),
CreateGenericResponse::new()
.content("Sound has been uploaded"),
)
.await?;
} }
Err(e) => { Err(e) => {
println!("Error occurred during upload: {:?}", e); println!("Error occurred during upload: {:?}", e);
msg.channel_id.say(&ctx, "Sound failed to upload.").await?; invoke
.followup(
ctx.http.clone(),
CreateGenericResponse::new()
.content("Sound failed to upload."),
)
.await?;
} }
} }
} }
} else { } else {
msg.channel_id.say( invoke.respond(
&ctx, ctx.http.clone(),
format!( 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, *MAX_SOUNDS,
)).await?; ))).await?;
} }
} }
} else { } else {
msg.channel_id invoke
.say( .respond(
&ctx, ctx.http.clone(),
"Please ensure the sound name contains a non-numerical character", CreateGenericResponse::new()
.content("Please ensure the sound name contains a non-numerical character"),
) )
.await?; .await?;
} }
} else { } else {
msg.channel_id.say(&ctx, "Usage: `?upload <name>`. Please ensure the name provided is less than 20 characters in length").await?; invoke.respond(ctx.http.clone(), CreateGenericResponse::new().content("Usage: `?upload <name>`. Please ensure the name provided is less than 20 characters in length")).await?;
} }
Ok(()) Ok(())
@ -1153,6 +1179,12 @@ async fn list_sounds(
#[command("public")] #[command("public")]
#[description("Change a sound between public and private")] #[description("Change a sound between public and private")]
#[arg(
name = "query",
kind = "String",
description = "Sound name or ID to change the privacy setting of",
required = true
)]
async fn change_public( async fn change_public(
ctx: &Context, ctx: &Context,
invoke: &(dyn CommandInvoke + Sync + Send), invoke: &(dyn CommandInvoke + Sync + Send),
@ -1333,6 +1365,12 @@ fn format_search_results(search_results: Vec<Sound>) -> CreateGenericResponse {
#[command("search")] #[command("search")]
#[description("Search for sounds")] #[description("Search for sounds")]
#[arg(
name = "query",
kind = "String",
description = "Sound name to search for",
required = true
)]
async fn search_sounds( async fn search_sounds(
ctx: &Context, ctx: &Context,
invoke: &(dyn CommandInvoke + Sync + Send), invoke: &(dyn CommandInvoke + Sync + Send),
@ -1437,6 +1475,12 @@ SELECT name, id, plays, public, server_id, uploader_id
#[command("greet")] #[command("greet")]
#[description("Set a join sound")] #[description("Set a join sound")]
#[arg(
name = "query",
kind = "String",
description = "Name or ID of sound to set as your greet sound",
required = false
)]
async fn set_greet_sound( async fn set_greet_sound(
ctx: &Context, ctx: &Context,
invoke: &(dyn CommandInvoke + Sync + Send), invoke: &(dyn CommandInvoke + Sync + Send),
@ -1450,7 +1494,10 @@ async fn set_greet_sound(
.cloned() .cloned()
.expect("Could not get SQLPool from data"); .expect("Could not get SQLPool from data");
let query = args.named("query").unwrap(); let query = args
.named("query")
.map(|s| s.to_owned())
.unwrap_or(String::new());
let user_id = invoke.author_id(); let user_id = invoke.author_id();
if query.len() == 0 { if query.len() == 0 {
@ -1464,7 +1511,7 @@ async fn set_greet_sound(
.await?; .await?;
} else { } else {
let sound_vec = Sound::search_for_sound( let sound_vec = Sound::search_for_sound(
query, &query,
invoke.guild_id().unwrap(), invoke.guild_id().unwrap(),
user_id, user_id,
pool.clone(), pool.clone(),