check time to kill users that get stuck in executing state

This commit is contained in:
jellywx 2021-05-23 10:59:06 +01:00
parent 9109250fe8
commit b31843c478
3 changed files with 18 additions and 19 deletions

View File

@ -397,9 +397,7 @@ async fn language(ctx: &Context, msg: &Message, args: String) {
async fn prefix(ctx: &Context, msg: &Message, args: String) { async fn prefix(ctx: &Context, msg: &Message, args: String) {
let (pool, lm) = get_ctx_data(&ctx).await; let (pool, lm) = get_ctx_data(&ctx).await;
let mut guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool) let guild_data = ctx.guild_data(msg.guild_id.unwrap()).await.unwrap();
.await
.unwrap();
let language = UserData::language_of(&msg.author, &pool).await; let language = UserData::language_of(&msg.author, &pool).await;
if args.len() > 5 { 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")) .say(&ctx, lm.get(&language, "prefix/no_argument"))
.await; .await;
} else { } else {
guild_data.prefix = args; guild_data.write().await.prefix = args;
#[cfg(feature = "prefix-cache")] guild_data.read().await.commit_changes(&pool).await;
let prefix_cache = ctx.data.read().await.get::<PrefixCache>().cloned().unwrap();
#[cfg(feature = "prefix-cache")]
prefix_cache.insert(msg.guild_id.unwrap(), guild_data.prefix.clone());
guild_data.commit_changes(&pool).await; let content = lm.get(&language, "prefix/success").replacen(
"{prefix}",
let content = &guild_data.read().await.prefix,
lm.get(&language, "prefix/success") 1,
.replacen("{prefix}", &guild_data.prefix, 1); );
let _ = msg.channel_id.say(&ctx, content).await; let _ = msg.channel_id.say(&ctx, content).await;
} }

View File

@ -23,7 +23,7 @@ use crate::language_manager::LanguageManager;
use crate::models::{CtxGuildData, GuildData, UserData}; use crate::models::{CtxGuildData, GuildData, UserData};
use crate::{models::ChannelData, CurrentlyExecuting, SQLPool}; use crate::{models::ChannelData, CurrentlyExecuting, SQLPool};
use serenity::model::id::MessageId; 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, ()>; 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(); let mut lock = currently_executing.lock().unwrap();
user_is_executing = lock.contains(&msg.author.id); user_is_executing = lock
lock.insert(msg.author.id); .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) { if !user_is_executing || msg.id == MessageId(0) {
@ -532,6 +534,8 @@ impl Framework for RegexFramework {
let mut lock = currently_executing.lock().unwrap(); let mut lock = currently_executing.lock().unwrap();
lock.remove(&msg.author.id); lock.remove(&msg.author.id);
} }
} else {
warn!("User {} is still executing!", msg.author.id);
} }
} }
} }

View File

@ -47,8 +47,8 @@ use dashmap::DashMap;
use tokio::sync::RwLock; use tokio::sync::RwLock;
use chrono_tz::Tz; use chrono_tz::Tz;
use std::collections::HashSet;
use std::sync::Mutex; use std::sync::Mutex;
use std::time::Instant;
struct GuildDataCache; struct GuildDataCache;
@ -83,7 +83,7 @@ impl TypeMapKey for PopularTimezones {
struct CurrentlyExecuting; struct CurrentlyExecuting;
impl TypeMapKey for CurrentlyExecuting { impl TypeMapKey for CurrentlyExecuting {
type Value = Arc<Mutex<HashSet<UserId>>>; type Value = Arc<Mutex<HashMap<UserId, Instant>>>;
} }
struct Handler; struct Handler;
@ -317,7 +317,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let mut data = client.data.write().await; let mut data = client.data.write().await;
data.insert::<GuildDataCache>(Arc::new(guild_data_cache)); data.insert::<GuildDataCache>(Arc::new(guild_data_cache));
data.insert::<CurrentlyExecuting>(Arc::new(Mutex::new(HashSet::new()))); data.insert::<CurrentlyExecuting>(Arc::new(Mutex::new(HashMap::new())));
data.insert::<SQLPool>(pool); data.insert::<SQLPool>(pool);
data.insert::<PopularTimezones>(Arc::new(popular_timezones)); data.insert::<PopularTimezones>(Arc::new(popular_timezones));
data.insert::<ReqwestClient>(Arc::new(reqwest::Client::new())); data.insert::<ReqwestClient>(Arc::new(reqwest::Client::new()));