check for user sound count on upload

This commit is contained in:
jude-lafitteIII 2020-04-29 23:09:31 +01:00
parent d30f875ea8
commit 9f2317eb6a
3 changed files with 35 additions and 3 deletions

1
Cargo.lock generated
View File

@ -1338,6 +1338,7 @@ name = "soundfx-rs"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"dotenv", "dotenv",
"lazy_static",
"serenity", "serenity",
"sqlx", "sqlx",
"tokio", "tokio",

View File

@ -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"]} sqlx = {version = "0.3", default-features = false, features = ["runtime-tokio", "macros", "mysql", "bigdecimal"]}
dotenv = "0.15" dotenv = "0.15"
tokio = {version = "0.2.19", features = ["fs", "sync", "process"]} tokio = {version = "0.2.19", features = ["fs", "sync", "process"]}
lazy_static = "1.4.0"

View File

@ -1,3 +1,6 @@
#[macro_use]
extern crate lazy_static;
use serenity::{ use serenity::{
client::{ client::{
bridge::{ bridge::{
@ -62,6 +65,13 @@ impl TypeMapKey for VoiceManager {
static THEME_COLOR: u32 = 0x00e0f3; static THEME_COLOR: u32 = 0x00e0f3;
lazy_static! {
static ref MAX_SOUNDS: u32 = {
dotenv().unwrap();
env::var("MAX_SOUNDS").unwrap().parse::<u32>().unwrap()
};
}
#[group] #[group]
#[commands(play, info, help, change_volume, change_prefix, upload_new_sound)] #[commands(play, info, help, change_volume, change_prefix, upload_new_sound)]
struct General; struct General;
@ -184,6 +194,21 @@ WHERE
Ok(()) Ok(())
} }
async fn count_user_sounds(user_id: u64, db_pool: MySqlPool) -> Result<u32, sqlx::error::Error> {
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<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")
@ -561,9 +586,16 @@ async fn upload_new_sound(ctx: &mut Context, msg: &Message, mut args: Args) -> C
let new_name = args.rest(); let new_name = args.rest();
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 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?;
// need to check if user is patreon or nah // 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?; 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 { match reply {
Some(reply_msg) => { Some(reply_msg) => {
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
if reply_msg.attachments.len() == 1 { if reply_msg.attachments.len() == 1 {
match Sound::create_anon( match Sound::create_anon(
new_name, new_name,
@ -609,3 +638,4 @@ async fn upload_new_sound(ctx: &mut Context, msg: &Message, mut args: Args) -> C
Ok(()) Ok(())
} }