diff --git a/src/db/user.rs b/src/db/user.rs index 8cafddd..f0baa80 100644 --- a/src/db/user.rs +++ b/src/db/user.rs @@ -1,5 +1,5 @@ -use diesel::{prelude::*, insert_into}; -use chrono::NaiveDateTime; +use diesel::{prelude::*, insert_into, update}; +use chrono::{NaiveDateTime, Local}; use crate::{logging::{LogLevel, log}, db::{connect, get_hashed_uid}}; #[derive(Insertable, Selectable, Queryable, Debug, Clone, Default)] @@ -93,4 +93,32 @@ pub fn get_user_by_id(user_id: &u64) -> Option { Ok(u) => return u, Err(_) => return None, } +} + +pub fn regen_fake_id(user_id: &u64, new_fake_id: i64) { + use crate::db::schema::user::dsl::*; + let conn = connect(); + if conn.is_none() { + return + } + let mut conn = conn.unwrap(); + let uid: String = get_hashed_uid(user_id); + let now: NaiveDateTime = Local::now().naive_local(); + + let _ = conn.transaction(|c| { + let sql = update(user) + .filter(discord_id.eq(uid)) + .set((fake_id.eq(new_fake_id), when_generated.eq(now))) + .execute(c); + + match sql { + Err(e) => { + log(LogLevel::Error, "db", "regen_fake_id", format!("SQL Error: {:?}", e).as_str()); + return Err(e); + } + Ok(_) => { + return Ok(()); + } + } + }); } \ No newline at end of file diff --git a/src/handlers.rs b/src/handlers.rs index ffe4bad..abe6d9b 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -3,6 +3,7 @@ use serde::ser::StdError; use poise::serenity_prelude::ChannelId; use poise::serenity_prelude::{self as serenity}; use rand::seq::SliceRandom; +use chrono::{NaiveDateTime, Local}; use crate::{logging::{LogLevel, log}, config::CORE_CFG, db::user::*}; @@ -28,7 +29,20 @@ pub async fn handle_msg(ctx: &serenity::Context, msg: &serenity::Message) -> Res return Ok(()) } } - let usr = uusr.unwrap(); + let mut usr = uusr.unwrap(); + + if usr.is_banned { + let _ = msg.reply(&ctx.http, "You have been banned from using this bot."); + return Ok(()) + } + + let now: NaiveDateTime = Local::now().naive_local(); + if usr.when_generated.timestamp() + 86400 < now.timestamp() { + let fid: i64 = rand::thread_rng().gen_range(10000000..=99999999); + log(LogLevel::Info, "event_handlers", "handle_msg", format!("Regenerate fake ID {}", fid).as_str()); + regen_fake_id(&msg.author.id.0, fid); + usr.fake_id = fid; + } let new_channel = ChannelId::from(cfg.channel); let mut picture = cfg.pictures.choose(&mut rand::thread_rng()).unwrap();