mirror of
https://github.com/revanced/revanced-discord-bot.git
synced 2025-04-30 14:34:29 +02:00
perf: lazy init censor
This commit is contained in:
parent
0187447d88
commit
36e29abce0
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -1366,7 +1366,9 @@ dependencies = [
|
||||
"dirs",
|
||||
"dotenv",
|
||||
"hmac-sha256",
|
||||
"lazy_static",
|
||||
"mongodb",
|
||||
"once_cell",
|
||||
"parse_duration",
|
||||
"poise",
|
||||
"regex",
|
||||
|
@ -36,3 +36,5 @@ hmac-sha256 = "1.1.6"
|
||||
base64 = "0.21.0"
|
||||
parse_duration = "2.1.1"
|
||||
censor = "0.3.0"
|
||||
lazy_static = "1.4.0"
|
||||
once_cell = "1.17.1"
|
||||
|
@ -1,6 +1,7 @@
|
||||
use tracing::debug;
|
||||
|
||||
use crate::utils::bot::load_configuration;
|
||||
use crate::utils::decancer::reinit_censor;
|
||||
use crate::{Context, Error};
|
||||
|
||||
/// Reload the Discord bot.
|
||||
@ -13,6 +14,8 @@ pub async fn reload(ctx: Context<'_>) -> Result<(), Error> {
|
||||
// Also save the new configuration to the user data
|
||||
ctx.data().write().await.configuration = configuration;
|
||||
|
||||
reinit_censor(ctx.serenity_context()).await;
|
||||
|
||||
debug!("{} reloaded the configuration.", ctx.author().name);
|
||||
|
||||
ctx.send(|f| {
|
||||
|
@ -1,9 +1,44 @@
|
||||
extern crate decancer;
|
||||
|
||||
use censor::*;
|
||||
use censor::Censor;
|
||||
use lazy_static::lazy_static;
|
||||
use once_cell::sync::OnceCell;
|
||||
use tracing::{error, info, trace};
|
||||
|
||||
use super::{*, bot::get_data_lock};
|
||||
use super::{bot, serenity};
|
||||
|
||||
lazy_static! {
|
||||
static ref CENSOR: OnceCell<censor::Censor> = OnceCell::new();
|
||||
}
|
||||
|
||||
// Initialize the censor
|
||||
async fn censor(ctx: &serenity::Context) -> &'static Censor {
|
||||
let data_lock = bot::get_data_lock(ctx).await;
|
||||
let censor_config = &data_lock.read().await.configuration.general.censor;
|
||||
let additions = &censor_config.additions;
|
||||
let removals = &censor_config.removals;
|
||||
|
||||
CENSOR.get_or_init(|| {
|
||||
let mut censor = censor::Standard;
|
||||
|
||||
for addition in additions {
|
||||
censor += addition;
|
||||
}
|
||||
|
||||
for removal in removals {
|
||||
censor -= removal;
|
||||
}
|
||||
|
||||
censor
|
||||
})
|
||||
}
|
||||
|
||||
// Reinitialize the censor when the configuration is reloaded
|
||||
pub async fn reinit_censor(ctx: &serenity::Context) {
|
||||
match CENSOR.set(censor(ctx).await.clone()) {
|
||||
Ok(_) => info!("Reinitialized censor"),
|
||||
Err(_) => error!("Failed to reinitialize censor"),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub async fn cure(
|
||||
ctx: &serenity::Context,
|
||||
@ -15,20 +50,7 @@ pub async fn cure(
|
||||
return;
|
||||
}
|
||||
|
||||
let data_lock = get_data_lock(ctx).await;
|
||||
let censor = &data_lock.read().await.configuration.general.censor;
|
||||
let additions = &censor.additions;
|
||||
let removals = &censor.removals;
|
||||
|
||||
let mut censor = Standard;
|
||||
|
||||
for addition in additions {
|
||||
censor += addition;
|
||||
}
|
||||
|
||||
for removal in removals {
|
||||
censor -= removal;
|
||||
}
|
||||
let censor = censor(ctx).await;
|
||||
|
||||
let name = member.display_name().to_string();
|
||||
|
||||
@ -47,7 +69,10 @@ pub async fn cure(
|
||||
"",
|
||||
);
|
||||
|
||||
if cured_name.is_empty() || !cured_name.starts_with(|c: char| c.is_ascii_alphabetic()) || censor.check(&cured_name) {
|
||||
if cured_name.is_empty()
|
||||
|| !cured_name.starts_with(|c: char| c.is_ascii_alphabetic())
|
||||
|| censor.check(&cured_name)
|
||||
{
|
||||
cured_name = "ReVanced member".to_string();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user