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",
|
"dirs",
|
||||||
"dotenv",
|
"dotenv",
|
||||||
"hmac-sha256",
|
"hmac-sha256",
|
||||||
|
"lazy_static",
|
||||||
"mongodb",
|
"mongodb",
|
||||||
|
"once_cell",
|
||||||
"parse_duration",
|
"parse_duration",
|
||||||
"poise",
|
"poise",
|
||||||
"regex",
|
"regex",
|
||||||
|
@ -36,3 +36,5 @@ hmac-sha256 = "1.1.6"
|
|||||||
base64 = "0.21.0"
|
base64 = "0.21.0"
|
||||||
parse_duration = "2.1.1"
|
parse_duration = "2.1.1"
|
||||||
censor = "0.3.0"
|
censor = "0.3.0"
|
||||||
|
lazy_static = "1.4.0"
|
||||||
|
once_cell = "1.17.1"
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
use crate::utils::bot::load_configuration;
|
use crate::utils::bot::load_configuration;
|
||||||
|
use crate::utils::decancer::reinit_censor;
|
||||||
use crate::{Context, Error};
|
use crate::{Context, Error};
|
||||||
|
|
||||||
/// Reload the Discord bot.
|
/// 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
|
// Also save the new configuration to the user data
|
||||||
ctx.data().write().await.configuration = configuration;
|
ctx.data().write().await.configuration = configuration;
|
||||||
|
|
||||||
|
reinit_censor(ctx.serenity_context()).await;
|
||||||
|
|
||||||
debug!("{} reloaded the configuration.", ctx.author().name);
|
debug!("{} reloaded the configuration.", ctx.author().name);
|
||||||
|
|
||||||
ctx.send(|f| {
|
ctx.send(|f| {
|
||||||
|
@ -1,9 +1,44 @@
|
|||||||
extern crate decancer;
|
use censor::Censor;
|
||||||
|
use lazy_static::lazy_static;
|
||||||
use censor::*;
|
use once_cell::sync::OnceCell;
|
||||||
use tracing::{error, info, trace};
|
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(
|
pub async fn cure(
|
||||||
ctx: &serenity::Context,
|
ctx: &serenity::Context,
|
||||||
@ -14,21 +49,8 @@ pub async fn cure(
|
|||||||
trace!("Skipping decancer for bot {}.", member.user.tag());
|
trace!("Skipping decancer for bot {}.", member.user.tag());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let data_lock = get_data_lock(ctx).await;
|
let censor = censor(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 name = member.display_name().to_string();
|
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();
|
cured_name = "ReVanced member".to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user