From 3465d7aab6a3bda36228fe7b39ac80b6a4573b31 Mon Sep 17 00:00:00 2001 From: jude Date: Tue, 23 Jun 2020 10:34:03 +0100 Subject: [PATCH] disallow numeric names --- src/main.rs | 127 +++++++++++++++++++++++++++++----------------------- 1 file changed, 71 insertions(+), 56 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3c41a91..2d1ff3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -246,7 +246,7 @@ impl EventHandler for Handler { .send() .await; - if let Ok(res) = response { + if let Err(res) = response { println!("DiscordBots Response: {:?}", res); } } @@ -670,78 +670,93 @@ async fn change_prefix(ctx: &Context, msg: &Message, mut args: Args) -> CommandR #[command("upload")] async fn upload_new_sound(ctx: &Context, msg: &Message, args: Args) -> CommandResult { + + fn is_numeric(s: &String) -> bool { + for char in s.chars() { + if char.is_digit(10) { + continue; + } + else { + return false; + } + } + true + } + let new_name = args.rest().to_string(); if !new_name.is_empty() && new_name.len() <= 20 { - let pool = ctx.data.read().await - .get::().cloned().expect("Could not get SQLPool from data"); - // need to check the name is not currently in use by the user - let count_name = Sound::count_named_user_sounds(*msg.author.id.as_u64(), &new_name, pool.clone()).await?; - if count_name > 0 { - msg.channel_id.say(&ctx, "You are already using that name. Please choose a unique name for your upload.").await?; - } + if !is_numeric(&new_name) { + let pool = ctx.data.read().await + .get::().cloned().expect("Could not get SQLPool from data"); - else { - // need to check how many sounds user currently has - let count = Sound::count_user_sounds(*msg.author.id.as_u64(), pool.clone()).await?; - let mut permit_upload = true; + // need to check the name is not currently in use by the user + let count_name = Sound::count_named_user_sounds(*msg.author.id.as_u64(), &new_name, pool.clone()).await?; + if count_name > 0 { + msg.channel_id.say(&ctx, "You are already using that name. Please choose a unique name for your upload.").await?; + } else { + // need to check how many sounds user currently has + let count = Sound::count_user_sounds(*msg.author.id.as_u64(), pool.clone()).await?; + let mut permit_upload = true; - // need to check if user is patreon or nah - if count >= *MAX_SOUNDS { - let patreon_guild_member = GuildId(*PATREON_GUILD).member(ctx, msg.author.id).await?; + // need to check if user is patreon or nah + if count >= *MAX_SOUNDS { + let patreon_guild_member = GuildId(*PATREON_GUILD).member(ctx, msg.author.id).await; - if patreon_guild_member.roles.contains(&RoleId(*PATREON_ROLE)) { - permit_upload = true; + if let Ok(member) = patreon_guild_member { + permit_upload = member.roles.contains(&RoleId(*PATREON_ROLE)); + } else { + permit_upload = false; + } } - else { - permit_upload = false; - } - } - if permit_upload { - msg.channel_id.say(&ctx, "Please now upload an audio file under 1MB in size (larger files will be automatically trimmed):").await?; + if permit_upload { + msg.channel_id.say(&ctx, "Please now upload an audio file under 1MB in size (larger files will be automatically trimmed):").await?; - let reply = msg.channel_id.await_reply(&ctx) - .author_id(msg.author.id) - .timeout(Duration::from_secs(30)) - .await; + let reply = msg.channel_id.await_reply(&ctx) + .author_id(msg.author.id) + .timeout(Duration::from_secs(30)) + .await; - match reply { - Some(reply_msg) => { - if reply_msg.attachments.len() == 1 { - match Sound::create_anon( - &new_name, - &reply_msg.attachments[0].url, - *msg.guild_id.unwrap().as_u64(), - *msg.author.id.as_u64(), - pool).await { - Ok(_) => { - msg.channel_id.say(&ctx, "Sound has been uploaded").await?; - } - - Err(_) => { - msg.channel_id.say(&ctx, "Sound failed to upload.").await?; + match reply { + Some(reply_msg) => { + if reply_msg.attachments.len() == 1 { + match Sound::create_anon( + &new_name, + &reply_msg.attachments[0].url, + *msg.guild_id.unwrap().as_u64(), + *msg.author.id.as_u64(), + pool).await { + Ok(_) => { + msg.channel_id.say(&ctx, "Sound has been uploaded").await?; + } + + Err(_) => { + msg.channel_id.say(&ctx, "Sound failed to upload.").await?; + } } + } else { + msg.channel_id.say(&ctx, "Please upload 1 attachment following your upload command. Aborted").await?; } - } else { - msg.channel_id.say(&ctx, "Please upload 1 attachment following your upload command. Aborted").await?; + } + + None => { + msg.channel_id.say(&ctx, "Upload timed out. Please redo the command").await?; } } - - None => { - msg.channel_id.say(&ctx, "Upload timed out. Please redo the command").await?; - } + } else { + msg.channel_id.say( + &ctx, + 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**", + *MAX_SOUNDS, + )).await?; } } - else { - msg.channel_id.say( - &ctx, - 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**", - *MAX_SOUNDS, - )).await?; - } + } + else { + msg.channel_id.say(&ctx, "Please ensure the sound name contains a non-numerical character").await?; } } else {