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]
#[commands(info, help, list_sounds)]
#[commands(info, help, list_sounds, change_public)]
struct AllUsers;
#[group]
@ -305,6 +305,23 @@ SELECT COUNT(1) as count
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 process_src(src_url: &str) -> Option<Vec<u8>> {
let future = Command::new("ffmpeg")
@ -711,12 +728,19 @@ async fn change_prefix(ctx: &mut Context, msg: &Message, mut args: Args) -> Comm
#[command("upload")]
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 {
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
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;
@ -745,7 +769,7 @@ async fn upload_new_sound(ctx: &mut Context, msg: &Message, mut args: Args) -> C
Some(reply_msg) => {
if reply_msg.attachments.len() == 1 {
match Sound::create_anon(
new_name,
&new_name,
&reply_msg.attachments[0].url,
*msg.guild_id.unwrap().as_u64(),
*msg.author.id.as_u64(),
@ -777,6 +801,7 @@ async fn upload_new_sound(ctx: &mut Context, msg: &Message, mut args: Args) -> C
)).await?;
}
}
}
else {
msg.channel_id.say(&ctx, "Usage: `?upload <name>`. Please ensure the name provided is less than 20 characters in length").await?;
}
@ -862,3 +887,46 @@ async fn list_sounds(ctx: &mut Context, msg: &Message, args: Args) -> CommandRes
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(())
}