command to change public status

This commit is contained in:
jude-lafitteIII 2020-05-15 22:04:22 +01:00
parent c84895b722
commit 7683680f54

View File

@ -87,7 +87,7 @@ lazy_static! {
} }
#[group] #[group]
#[commands(info, help, list_sounds)] #[commands(info, help, list_sounds, change_public)]
struct AllUsers; struct AllUsers;
#[group] #[group]
@ -305,6 +305,23 @@ SELECT COUNT(1) as count
Ok(c as u32) Ok(c as u32)
} }
async fn count_named_user_sounds(user_id: u64, name: &String, db_pool: MySqlPool) -> Result<u32, sqlx::error::Error> {
let c = sqlx::query!(
"
SELECT COUNT(1) as count
FROM sounds
WHERE
uploader_id = ? AND
name = ?
",
user_id, name
)
.fetch_one(&db_pool)
.await?.count;
Ok(c as u32)
}
async fn create_anon(name: &str, src_url: &str, server_id: u64, user_id: u64, db_pool: MySqlPool) -> Result<u64, Box<dyn std::error::Error + Send>> { async fn create_anon(name: &str, src_url: &str, server_id: u64, user_id: u64, db_pool: MySqlPool) -> Result<u64, Box<dyn std::error::Error + Send>> {
async fn process_src(src_url: &str) -> Option<Vec<u8>> { async fn process_src(src_url: &str) -> Option<Vec<u8>> {
let future = Command::new("ffmpeg") let future = Command::new("ffmpeg")
@ -711,70 +728,78 @@ async fn change_prefix(ctx: &mut Context, msg: &Message, mut args: Args) -> Comm
#[command("upload")] #[command("upload")]
async fn upload_new_sound(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { async fn upload_new_sound(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
let new_name = args.rest(); 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 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");
// need to check how many sounds user currently has // need to check the name is not currently in use by the user
let count = Sound::count_user_sounds(*msg.author.id.as_u64(), pool.clone()).await?; let count_name = Sound::count_named_user_sounds(*msg.author.id.as_u64(), &new_name, pool.clone()).await?;
let mut permit_upload = true; if count_name > 0 {
msg.channel_id.say(&ctx, "You are already using that name. Please choose a unique name for your upload.").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;
}
else {
permit_upload = false;
}
} }
if permit_upload { else {
msg.channel_id.say(&ctx, "Please now upload an audio file under 1MB in size (larger files will be automatically trimmed):").await?; // 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;
let reply = msg.channel_id.await_reply(&ctx) // need to check if user is patreon or nah
.author_id(msg.author.id) if count >= *MAX_SOUNDS {
.timeout(Duration::from_secs(30)) let patreon_guild_member = GuildId(*PATREON_GUILD).member(&ctx, msg.author.id).await?;
.await;
match reply { if patreon_guild_member.roles.contains(&RoleId(*PATREON_ROLE)) {
Some(reply_msg) => { permit_upload = true;
if reply_msg.attachments.len() == 1 { }
match Sound::create_anon( else {
new_name, permit_upload = false;
&reply_msg.attachments[0].url, }
*msg.guild_id.unwrap().as_u64(), }
*msg.author.id.as_u64(),
pool).await { if permit_upload {
Ok(_) => { 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, "Sound has been uploaded").await?;
} let reply = msg.channel_id.await_reply(&ctx)
.author_id(msg.author.id)
Err(_) => { .timeout(Duration::from_secs(30))
msg.channel_id.say(&ctx, "Sound failed to upload.").await?; .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 {
else { msg.channel_id.say(
msg.channel_id.say( &ctx,
&ctx, format!(
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 {
@ -862,3 +887,46 @@ async fn list_sounds(ctx: &mut Context, msg: &Message, args: Args) -> CommandRes
Ok(()) Ok(())
} }
#[command("public")]
async fn change_public(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
let sound_result;
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
let uid = msg.author.id.as_u64();
{
let name = args.rest();
let gid = *msg.guild_id.unwrap().as_u64();
sound_result = Sound::search_for_sound(name, gid, *uid, pool.clone()).await;
}
match sound_result {
Ok(mut sound) => {
if sound.uploader_id != *uid {
msg.channel_id.say(&ctx, "You can only change the availability of sounds you have uploaded. Use `?list me` to view your sounds").await?;
}
else {
if sound.public {
sound.public = false;
msg.channel_id.say(&ctx, "Sound has been set to private 🔒").await?;
} else {
sound.public = true;
msg.channel_id.say(&ctx, "Sound has been set to public 🔓").await?;
}
sound.commit(pool).await?
}
}
Err(_) => {
msg.channel_id.say(&ctx, "Sound could not be found by that name.").await?;
}
}
Ok(())
}