From 9f2317eb6a5919ed994be7c421ce3467443b541a Mon Sep 17 00:00:00 2001 From: jude-lafitteIII Date: Wed, 29 Apr 2020 23:09:31 +0100 Subject: [PATCH] check for user sound count on upload --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 36 +++++++++++++++++++++++++++++++++--- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 64c9fd3..a33f981 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1338,6 +1338,7 @@ name = "soundfx-rs" version = "0.1.0" dependencies = [ "dotenv", + "lazy_static", "serenity", "sqlx", "tokio", diff --git a/Cargo.toml b/Cargo.toml index 51d6640..34f8efa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ serenity = {path = "/home/jude/serenity", features = ["voice", "collector"]} sqlx = {version = "0.3", default-features = false, features = ["runtime-tokio", "macros", "mysql", "bigdecimal"]} dotenv = "0.15" tokio = {version = "0.2.19", features = ["fs", "sync", "process"]} +lazy_static = "1.4.0" diff --git a/src/main.rs b/src/main.rs index 448f0d7..710f7fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +#[macro_use] +extern crate lazy_static; + use serenity::{ client::{ bridge::{ @@ -62,6 +65,13 @@ impl TypeMapKey for VoiceManager { static THEME_COLOR: u32 = 0x00e0f3; +lazy_static! { + static ref MAX_SOUNDS: u32 = { + dotenv().unwrap(); + env::var("MAX_SOUNDS").unwrap().parse::().unwrap() + }; +} + #[group] #[commands(play, info, help, change_volume, change_prefix, upload_new_sound)] struct General; @@ -184,6 +194,21 @@ WHERE Ok(()) } + async fn count_user_sounds(user_id: u64, db_pool: MySqlPool) -> Result { + let c = sqlx::query!( + " +SELECT COUNT(1) as count + FROM sounds + WHERE uploader_id = ? + ", + user_id + ) + .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> { async fn process_src(src_url: &str) -> Option> { let future = Command::new("ffmpeg") @@ -561,9 +586,16 @@ async fn upload_new_sound(ctx: &mut Context, msg: &Message, mut args: Args) -> C let new_name = args.rest(); 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 how many sounds user currently has + let count = Sound::count_user_sounds(*msg.author.id.as_u64(), pool.clone()).await?; // need to check if user is patreon or nah + if count >= *MAX_SOUNDS { + + } msg.channel_id.say(&ctx, "Please now upload an audio file under 1MB in size:").await?; @@ -574,9 +606,6 @@ async fn upload_new_sound(ctx: &mut Context, msg: &Message, mut args: Args) -> C match reply { Some(reply_msg) => { - let pool = ctx.data.read().await - .get::().cloned().expect("Could not get SQLPool from data"); - if reply_msg.attachments.len() == 1 { match Sound::create_anon( new_name, @@ -609,3 +638,4 @@ async fn upload_new_sound(ctx: &mut Context, msg: &Message, mut args: Args) -> C Ok(()) } +