mirror of
https://github.com/revanced/revanced-discord-bot.git
synced 2025-04-29 22:14:28 +02:00
improve code quality
This commit is contained in:
parent
b7269daede
commit
bb72d6252e
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -1356,7 +1356,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "revanced-discord-bot"
|
||||
version = "2.5.1"
|
||||
version = "2.6.1"
|
||||
dependencies = [
|
||||
"base64 0.21.2",
|
||||
"bson",
|
||||
|
@ -1,20 +1,19 @@
|
||||
use tracing::debug;
|
||||
|
||||
use crate::utils::bot::load_configuration;
|
||||
use crate::utils::decancer::reinit_censor;
|
||||
use crate::utils::cure_names::init_censor;
|
||||
use crate::{Context, Error};
|
||||
|
||||
/// Reload the Discord bot.
|
||||
#[poise::command(slash_command)]
|
||||
pub async fn reload(ctx: Context<'_>) -> Result<(), Error> {
|
||||
// Update the configuration
|
||||
let configuration = load_configuration();
|
||||
// Use the embed color from the updated configuration
|
||||
let embed_color = configuration.general.embed_color;
|
||||
// Also save the new configuration to the user data
|
||||
ctx.data().write().await.configuration = configuration;
|
||||
|
||||
reinit_censor(ctx.serenity_context()).await;
|
||||
let embed_color = configuration.general.embed_color;
|
||||
|
||||
init_censor(&configuration.general.censor);
|
||||
|
||||
ctx.data().write().await.configuration = configuration;
|
||||
|
||||
debug!("{} reloaded the configuration.", ctx.author().name);
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
use super::*;
|
||||
use crate::utils::decancer::cure;
|
||||
use crate::utils::cure_names::cure;
|
||||
use crate::utils::moderation::mute_on_join;
|
||||
|
||||
pub async fn guild_member_addition(ctx: &serenity::Context, new_member: &mut serenity::Member) {
|
||||
|
@ -1,5 +1,5 @@
|
||||
use super::*;
|
||||
use crate::utils::decancer::cure;
|
||||
use crate::utils::cure_names::cure;
|
||||
|
||||
pub async fn guild_member_update(
|
||||
ctx: &serenity::Context,
|
||||
|
@ -3,6 +3,8 @@ use std::sync::Arc;
|
||||
use poise::serenity_prelude::{self as serenity, Mutex, RwLock, ShardManager, UserId};
|
||||
use tracing::log::error;
|
||||
|
||||
use crate::utils::bot::get_data_lock;
|
||||
use crate::utils::cure_names::init_censor;
|
||||
use crate::{Data, Error};
|
||||
|
||||
mod guild_member_addition;
|
||||
@ -91,6 +93,16 @@ impl serenity::EventHandler for Handler<Arc<RwLock<Data>>> {
|
||||
async fn ready(&self, ctx: serenity::Context, ready: serenity::Ready) {
|
||||
*self.bot_id.write().await = Some(ready.user.id);
|
||||
|
||||
init_censor(
|
||||
&get_data_lock(&ctx)
|
||||
.await
|
||||
.read()
|
||||
.await
|
||||
.configuration
|
||||
.general
|
||||
.censor,
|
||||
);
|
||||
|
||||
ready::load_muted_members(&ctx, &ready).await;
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,6 @@ use crate::utils::moderation::queue_unmute_member;
|
||||
pub async fn load_muted_members(ctx: &serenity::Context, _: &serenity::Ready) {
|
||||
let data = get_data_lock(ctx).await;
|
||||
let data = &mut *data.write().await;
|
||||
let mute_role_id = data.configuration.general.mute.role;
|
||||
|
||||
let mut cursor = data
|
||||
.database
|
||||
@ -38,7 +37,7 @@ pub async fn load_muted_members(ctx: &serenity::Context, _: &serenity::Ready) {
|
||||
data.database.clone(),
|
||||
serenity::GuildId(guild_id),
|
||||
serenity::UserId(user_id),
|
||||
mute_role_id,
|
||||
data.configuration.general.mute.role,
|
||||
amount_left as u64, // i64 as u64 is handled properly here
|
||||
),
|
||||
);
|
||||
|
@ -3,27 +3,23 @@ use lazy_static::lazy_static;
|
||||
use once_cell::sync::OnceCell;
|
||||
use tracing::{error, info, trace};
|
||||
|
||||
use super::{bot, serenity};
|
||||
use super::serenity;
|
||||
use crate::model::application::Censor as CensorConfiguration;
|
||||
|
||||
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;
|
||||
|
||||
// Initialize censor with the given configuration.
|
||||
pub fn init_censor(configuration: &CensorConfiguration) -> &'static Censor {
|
||||
CENSOR.get_or_init(|| {
|
||||
let mut censor = censor::Standard;
|
||||
|
||||
for addition in additions {
|
||||
for addition in &configuration.additions {
|
||||
censor += addition;
|
||||
}
|
||||
|
||||
for removal in removals {
|
||||
for removal in &configuration.removals {
|
||||
censor -= removal;
|
||||
}
|
||||
|
||||
@ -31,15 +27,6 @@ async fn censor(ctx: &serenity::Context) -> &'static 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,
|
||||
old_if_available: &Option<serenity::Member>,
|
||||
@ -49,8 +36,6 @@ pub async fn cure(
|
||||
trace!("Skipping decancer for bot {}.", member.user.tag());
|
||||
return;
|
||||
}
|
||||
|
||||
let censor = censor(ctx).await;
|
||||
|
||||
let name = member.display_name().to_string();
|
||||
|
||||
@ -71,7 +56,7 @@ pub async fn cure(
|
||||
|
||||
if cured_name.is_empty()
|
||||
|| !cured_name.starts_with(|c: char| c.is_ascii_alphabetic())
|
||||
|| censor.check(&cured_name)
|
||||
|| CENSOR.get().unwrap().check(&cured_name)
|
||||
{
|
||||
cured_name = "ReVanced member".to_string();
|
||||
}
|
@ -3,7 +3,7 @@ use poise::serenity_prelude::{self as serenity, Member, RoleId};
|
||||
|
||||
pub mod bot;
|
||||
pub mod code_embed;
|
||||
pub mod decancer;
|
||||
pub mod cure_names;
|
||||
pub mod macros;
|
||||
pub mod message;
|
||||
pub mod message_response;
|
||||
|
Loading…
x
Reference in New Issue
Block a user