ratelimit commands
This commit is contained in:
parent
0a9624d12d
commit
a0da4dcf00
@ -21,7 +21,8 @@ use std::{collections::HashMap, fmt};
|
|||||||
|
|
||||||
use crate::language_manager::LanguageManager;
|
use crate::language_manager::LanguageManager;
|
||||||
use crate::models::{CtxGuildData, GuildData, UserData};
|
use crate::models::{CtxGuildData, GuildData, UserData};
|
||||||
use crate::{models::ChannelData, SQLPool};
|
use crate::{models::ChannelData, CurrentlyExecuting, SQLPool};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
type CommandFn = for<'fut> fn(&'fut Context, &'fut Message, String) -> BoxFuture<'fut, ()>;
|
type CommandFn = for<'fut> fn(&'fut Context, &'fut Message, String) -> BoxFuture<'fut, ()>;
|
||||||
|
|
||||||
@ -345,9 +346,27 @@ impl Framework for RegexFramework {
|
|||||||
|
|
||||||
// gate to prevent analysing messages unnecessarily
|
// gate to prevent analysing messages unnecessarily
|
||||||
if (msg.author.bot && self.ignore_bots) || msg.content.is_empty() {
|
if (msg.author.bot && self.ignore_bots) || msg.content.is_empty() {
|
||||||
|
} else {
|
||||||
|
let currently_executing = ctx
|
||||||
|
.data
|
||||||
|
.read()
|
||||||
|
.await
|
||||||
|
.get::<CurrentlyExecuting>()
|
||||||
|
.cloned()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let user_is_executing;
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut lock = currently_executing.lock().unwrap();
|
||||||
|
|
||||||
|
user_is_executing = lock.contains(&msg.author.id);
|
||||||
|
lock.insert(msg.author.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !user_is_executing {
|
||||||
// Guild Command
|
// Guild Command
|
||||||
else if let (Some(guild), Some(Channel::Guild(channel))) =
|
if let (Some(guild), Some(Channel::Guild(channel))) =
|
||||||
(msg.guild(&ctx).await, msg.channel(&ctx).await)
|
(msg.guild(&ctx).await, msg.channel(&ctx).await)
|
||||||
{
|
{
|
||||||
let data = ctx.data.read().await;
|
let data = ctx.data.read().await;
|
||||||
@ -368,7 +387,13 @@ impl Framework for RegexFramework {
|
|||||||
PermissionCheck::All => {
|
PermissionCheck::All => {
|
||||||
let command = self
|
let command = self
|
||||||
.commands
|
.commands
|
||||||
.get(&full_match.name("cmd").unwrap().as_str().to_lowercase())
|
.get(
|
||||||
|
&full_match
|
||||||
|
.name("cmd")
|
||||||
|
.unwrap()
|
||||||
|
.as_str()
|
||||||
|
.to_lowercase(),
|
||||||
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let channel_data = ChannelData::from_channel(
|
let channel_data = ChannelData::from_channel(
|
||||||
@ -385,33 +410,43 @@ impl Framework for RegexFramework {
|
|||||||
.unwrap_or("")
|
.unwrap_or("")
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
let member = guild.member(&ctx, &msg.author).await.unwrap();
|
let member =
|
||||||
|
guild.member(&ctx, &msg.author).await.unwrap();
|
||||||
|
|
||||||
if command.check_permissions(&ctx, &guild, &member).await {
|
if command
|
||||||
|
.check_permissions(&ctx, &guild, &member)
|
||||||
|
.await
|
||||||
|
{
|
||||||
dbg!(command.name);
|
dbg!(command.name);
|
||||||
|
|
||||||
{
|
{
|
||||||
let guild_id = guild.id.as_u64().to_owned();
|
let guild_id = guild.id.as_u64().to_owned();
|
||||||
|
|
||||||
GuildData::from_guild(guild, &pool).await.expect(
|
GuildData::from_guild(guild, &pool)
|
||||||
&format!(
|
.await
|
||||||
|
.expect(&format!(
|
||||||
"Failed to create new guild object for {}",
|
"Failed to create new guild object for {}",
|
||||||
guild_id
|
guild_id
|
||||||
),
|
));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
(command.func)(&ctx, &msg, args).await;
|
(command.func)(&ctx, &msg, args).await;
|
||||||
} else if command.required_perms == PermissionLevel::Restricted
|
} else if command.required_perms
|
||||||
|
== PermissionLevel::Restricted
|
||||||
{
|
{
|
||||||
let _ = msg
|
let _ = msg
|
||||||
.channel_id
|
.channel_id
|
||||||
.say(
|
.say(
|
||||||
&ctx,
|
&ctx,
|
||||||
lm.get(&language.await, "no_perms_restricted"),
|
lm.get(
|
||||||
|
&language.await,
|
||||||
|
"no_perms_restricted",
|
||||||
|
),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
} else if command.required_perms == PermissionLevel::Managed {
|
} else if command.required_perms
|
||||||
|
== PermissionLevel::Managed
|
||||||
|
{
|
||||||
let _ = msg
|
let _ = msg
|
||||||
.channel_id
|
.channel_id
|
||||||
.say(
|
.say(
|
||||||
@ -439,7 +474,10 @@ impl Framework for RegexFramework {
|
|||||||
"{manage_webhooks}",
|
"{manage_webhooks}",
|
||||||
if manage_webhooks { "✅" } else { "❌" },
|
if manage_webhooks { "✅" } else { "❌" },
|
||||||
)
|
)
|
||||||
.replace("{embed_links}", if embed_links { "✅" } else { "❌" })
|
.replace(
|
||||||
|
"{embed_links}",
|
||||||
|
if embed_links { "✅" } else { "❌" },
|
||||||
|
)
|
||||||
.replace(
|
.replace(
|
||||||
"{add_reactions}",
|
"{add_reactions}",
|
||||||
if add_reactions { "✅" } else { "❌" },
|
if add_reactions { "✅" } else { "❌" },
|
||||||
@ -485,5 +523,15 @@ impl Framework for RegexFramework {
|
|||||||
(command.func)(&ctx, &msg, args).await;
|
(command.func)(&ctx, &msg, args).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// wait 500 ms before allowing the user to execute a command again
|
||||||
|
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||||
|
|
||||||
|
let mut lock = currently_executing.lock().unwrap();
|
||||||
|
lock.remove(&msg.author.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
10
src/main.rs
10
src/main.rs
@ -47,6 +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;
|
||||||
|
|
||||||
struct GuildDataCache;
|
struct GuildDataCache;
|
||||||
|
|
||||||
@ -78,6 +80,12 @@ impl TypeMapKey for PopularTimezones {
|
|||||||
type Value = Arc<Vec<Tz>>;
|
type Value = Arc<Vec<Tz>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct CurrentlyExecuting;
|
||||||
|
|
||||||
|
impl TypeMapKey for CurrentlyExecuting {
|
||||||
|
type Value = Arc<Mutex<HashSet<UserId>>>;
|
||||||
|
}
|
||||||
|
|
||||||
struct Handler;
|
struct Handler;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
@ -309,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::<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()));
|
||||||
|
Loading…
Reference in New Issue
Block a user