disallow numeric names
This commit is contained in:
parent
302e0d0254
commit
3465d7aab6
127
src/main.rs
127
src/main.rs
@ -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,78 +670,93 @@ 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 {
|
||||||
let pool = ctx.data.read().await
|
|
||||||
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
|
|
||||||
|
|
||||||
// need to check the name is not currently in use by the user
|
if !is_numeric(&new_name) {
|
||||||
let count_name = Sound::count_named_user_sounds(*msg.author.id.as_u64(), &new_name, pool.clone()).await?;
|
let pool = ctx.data.read().await
|
||||||
if count_name > 0 {
|
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
|
||||||
msg.channel_id.say(&ctx, "You are already using that name. Please choose a unique name for your upload.").await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
// need to check the name is not currently in use by the user
|
||||||
// need to check how many sounds user currently has
|
let count_name = Sound::count_named_user_sounds(*msg.author.id.as_u64(), &new_name, pool.clone()).await?;
|
||||||
let count = Sound::count_user_sounds(*msg.author.id.as_u64(), pool.clone()).await?;
|
if count_name > 0 {
|
||||||
let mut permit_upload = true;
|
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
|
// 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 {
|
||||||
|
permit_upload = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
permit_upload = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if permit_upload {
|
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?;
|
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)
|
let reply = msg.channel_id.await_reply(&ctx)
|
||||||
.author_id(msg.author.id)
|
.author_id(msg.author.id)
|
||||||
.timeout(Duration::from_secs(30))
|
.timeout(Duration::from_secs(30))
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
match reply {
|
match reply {
|
||||||
Some(reply_msg) => {
|
Some(reply_msg) => {
|
||||||
if reply_msg.attachments.len() == 1 {
|
if reply_msg.attachments.len() == 1 {
|
||||||
match Sound::create_anon(
|
match Sound::create_anon(
|
||||||
&new_name,
|
&new_name,
|
||||||
&reply_msg.attachments[0].url,
|
&reply_msg.attachments[0].url,
|
||||||
*msg.guild_id.unwrap().as_u64(),
|
*msg.guild_id.unwrap().as_u64(),
|
||||||
*msg.author.id.as_u64(),
|
*msg.author.id.as_u64(),
|
||||||
pool).await {
|
pool).await {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
msg.channel_id.say(&ctx, "Sound has been uploaded").await?;
|
msg.channel_id.say(&ctx, "Sound has been uploaded").await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
msg.channel_id.say(&ctx, "Sound failed to upload.").await?;
|
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?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
None => {
|
msg.channel_id.say(
|
||||||
msg.channel_id.say(&ctx, "Upload timed out. Please redo the command").await?;
|
&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(
|
else {
|
||||||
&ctx,
|
msg.channel_id.say(&ctx, "Please ensure the sound name contains a non-numerical character").await?;
|
||||||
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 {
|
else {
|
||||||
|
Loading…
Reference in New Issue
Block a user