disallow numeric names

This commit is contained in:
jude 2020-06-23 10:34:03 +01:00
parent 302e0d0254
commit 3465d7aab6

View File

@ -246,7 +246,7 @@ impl EventHandler for Handler {
.send() .send()
.await; .await;
if let Ok(res) = response { if let Err(res) = response {
println!("DiscordBots Response: {:?}", res); println!("DiscordBots Response: {:?}", res);
} }
} }
@ -670,9 +670,24 @@ async fn change_prefix(ctx: &Context, msg: &Message, mut args: Args) -> CommandR
#[command("upload")] #[command("upload")]
async fn upload_new_sound(ctx: &Context, msg: &Message, args: Args) -> CommandResult { 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(); let new_name = args.rest().to_string();
if !new_name.is_empty() && new_name.len() <= 20 { if !new_name.is_empty() && new_name.len() <= 20 {
if !is_numeric(&new_name) {
let pool = ctx.data.read().await let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data"); .get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
@ -680,21 +695,18 @@ async fn upload_new_sound(ctx: &Context, msg: &Message, args: Args) -> CommandRe
let count_name = Sound::count_named_user_sounds(*msg.author.id.as_u64(), &new_name, pool.clone()).await?; let count_name = Sound::count_named_user_sounds(*msg.author.id.as_u64(), &new_name, pool.clone()).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?; msg.channel_id.say(&ctx, "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(*msg.author.id.as_u64(), 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 = GuildId(*PATREON_GUILD).member(ctx, msg.author.id).await?; let patreon_guild_member = GuildId(*PATREON_GUILD).member(ctx, msg.author.id).await;
if patreon_guild_member.roles.contains(&RoleId(*PATREON_ROLE)) { if let Ok(member) = patreon_guild_member {
permit_upload = true; permit_upload = member.roles.contains(&RoleId(*PATREON_ROLE));
} } else {
else {
permit_upload = false; permit_upload = false;
} }
} }
@ -733,8 +745,7 @@ async fn upload_new_sound(ctx: &Context, msg: &Message, args: Args) -> CommandRe
msg.channel_id.say(&ctx, "Upload timed out. Please redo the command").await?; msg.channel_id.say(&ctx, "Upload timed out. Please redo the command").await?;
} }
} }
} } else {
else {
msg.channel_id.say( msg.channel_id.say(
&ctx, &ctx,
format!( format!(
@ -744,6 +755,10 @@ async fn upload_new_sound(ctx: &Context, msg: &Message, args: Args) -> CommandRe
} }
} }
} }
else {
msg.channel_id.say(&ctx, "Please ensure the sound name contains a non-numerical character").await?;
}
}
else { else {
msg.channel_id.say(&ctx, "Usage: `?upload <name>`. Please ensure the name provided is less than 20 characters in length").await?; msg.channel_id.say(&ctx, "Usage: `?upload <name>`. Please ensure the name provided is less than 20 characters in length").await?;
} }