From b31843c47800c60d2aeb66147b054c044f7b7685 Mon Sep 17 00:00:00 2001 From: jellywx Date: Sun, 23 May 2021 10:59:06 +0100 Subject: [PATCH] check time to kill users that get stuck in executing state --- src/commands/moderation_cmds.rs | 21 ++++++++------------- src/framework.rs | 10 +++++++--- src/main.rs | 6 +++--- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index 188c489..88c9725 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -397,9 +397,7 @@ async fn language(ctx: &Context, msg: &Message, args: String) { async fn prefix(ctx: &Context, msg: &Message, args: String) { let (pool, lm) = get_ctx_data(&ctx).await; - let mut guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool) - .await - .unwrap(); + let guild_data = ctx.guild_data(msg.guild_id.unwrap()).await.unwrap(); let language = UserData::language_of(&msg.author, &pool).await; if args.len() > 5 { @@ -413,18 +411,15 @@ async fn prefix(ctx: &Context, msg: &Message, args: String) { .say(&ctx, lm.get(&language, "prefix/no_argument")) .await; } else { - guild_data.prefix = args; + guild_data.write().await.prefix = args; - #[cfg(feature = "prefix-cache")] - let prefix_cache = ctx.data.read().await.get::().cloned().unwrap(); - #[cfg(feature = "prefix-cache")] - prefix_cache.insert(msg.guild_id.unwrap(), guild_data.prefix.clone()); + guild_data.read().await.commit_changes(&pool).await; - guild_data.commit_changes(&pool).await; - - let content = - lm.get(&language, "prefix/success") - .replacen("{prefix}", &guild_data.prefix, 1); + let content = lm.get(&language, "prefix/success").replacen( + "{prefix}", + &guild_data.read().await.prefix, + 1, + ); let _ = msg.channel_id.say(&ctx, content).await; } diff --git a/src/framework.rs b/src/framework.rs index 6c0d92c..44ec7a9 100644 --- a/src/framework.rs +++ b/src/framework.rs @@ -23,7 +23,7 @@ use crate::language_manager::LanguageManager; use crate::models::{CtxGuildData, GuildData, UserData}; use crate::{models::ChannelData, CurrentlyExecuting, SQLPool}; use serenity::model::id::MessageId; -use std::time::Duration; +use std::time::{Duration, Instant}; type CommandFn = for<'fut> fn(&'fut Context, &'fut Message, String) -> BoxFuture<'fut, ()>; @@ -361,8 +361,10 @@ impl Framework for RegexFramework { { let mut lock = currently_executing.lock().unwrap(); - user_is_executing = lock.contains(&msg.author.id); - lock.insert(msg.author.id); + user_is_executing = lock + .get(&msg.author.id) + .map_or(false, |now| now.elapsed().as_secs() < 4); + lock.insert(msg.author.id, Instant::now()); } if !user_is_executing || msg.id == MessageId(0) { @@ -532,6 +534,8 @@ impl Framework for RegexFramework { let mut lock = currently_executing.lock().unwrap(); lock.remove(&msg.author.id); } + } else { + warn!("User {} is still executing!", msg.author.id); } } } diff --git a/src/main.rs b/src/main.rs index 5fa3b83..bee81db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,8 +47,8 @@ use dashmap::DashMap; use tokio::sync::RwLock; use chrono_tz::Tz; -use std::collections::HashSet; use std::sync::Mutex; +use std::time::Instant; struct GuildDataCache; @@ -83,7 +83,7 @@ impl TypeMapKey for PopularTimezones { struct CurrentlyExecuting; impl TypeMapKey for CurrentlyExecuting { - type Value = Arc>>; + type Value = Arc>>; } struct Handler; @@ -317,7 +317,7 @@ async fn main() -> Result<(), Box> { let mut data = client.data.write().await; data.insert::(Arc::new(guild_data_cache)); - data.insert::(Arc::new(Mutex::new(HashSet::new()))); + data.insert::(Arc::new(Mutex::new(HashMap::new()))); data.insert::(pool); data.insert::(Arc::new(popular_timezones)); data.insert::(Arc::new(reqwest::Client::new()));