mirror of
https://github.com/revanced/revanced-discord-bot.git
synced 2025-05-01 23:14:25 +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]]
|
[[package]]
|
||||||
name = "revanced-discord-bot"
|
name = "revanced-discord-bot"
|
||||||
version = "2.5.1"
|
version = "2.6.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64 0.21.2",
|
"base64 0.21.2",
|
||||||
"bson",
|
"bson",
|
||||||
|
@ -1,20 +1,19 @@
|
|||||||
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::utils::cure_names::init_censor;
|
||||||
use crate::{Context, Error};
|
use crate::{Context, Error};
|
||||||
|
|
||||||
/// Reload the Discord bot.
|
/// Reload the Discord bot.
|
||||||
#[poise::command(slash_command)]
|
#[poise::command(slash_command)]
|
||||||
pub async fn reload(ctx: Context<'_>) -> Result<(), Error> {
|
pub async fn reload(ctx: Context<'_>) -> Result<(), Error> {
|
||||||
// Update the configuration
|
|
||||||
let configuration = load_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);
|
debug!("{} reloaded the configuration.", ctx.author().name);
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::utils::decancer::cure;
|
use crate::utils::cure_names::cure;
|
||||||
use crate::utils::moderation::mute_on_join;
|
use crate::utils::moderation::mute_on_join;
|
||||||
|
|
||||||
pub async fn guild_member_addition(ctx: &serenity::Context, new_member: &mut serenity::Member) {
|
pub async fn guild_member_addition(ctx: &serenity::Context, new_member: &mut serenity::Member) {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::utils::decancer::cure;
|
use crate::utils::cure_names::cure;
|
||||||
|
|
||||||
pub async fn guild_member_update(
|
pub async fn guild_member_update(
|
||||||
ctx: &serenity::Context,
|
ctx: &serenity::Context,
|
||||||
|
@ -3,6 +3,8 @@ use std::sync::Arc;
|
|||||||
use poise::serenity_prelude::{self as serenity, Mutex, RwLock, ShardManager, UserId};
|
use poise::serenity_prelude::{self as serenity, Mutex, RwLock, ShardManager, UserId};
|
||||||
use tracing::log::error;
|
use tracing::log::error;
|
||||||
|
|
||||||
|
use crate::utils::bot::get_data_lock;
|
||||||
|
use crate::utils::cure_names::init_censor;
|
||||||
use crate::{Data, Error};
|
use crate::{Data, Error};
|
||||||
|
|
||||||
mod guild_member_addition;
|
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) {
|
async fn ready(&self, ctx: serenity::Context, ready: serenity::Ready) {
|
||||||
*self.bot_id.write().await = Some(ready.user.id);
|
*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;
|
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) {
|
pub async fn load_muted_members(ctx: &serenity::Context, _: &serenity::Ready) {
|
||||||
let data = get_data_lock(ctx).await;
|
let data = get_data_lock(ctx).await;
|
||||||
let data = &mut *data.write().await;
|
let data = &mut *data.write().await;
|
||||||
let mute_role_id = data.configuration.general.mute.role;
|
|
||||||
|
|
||||||
let mut cursor = data
|
let mut cursor = data
|
||||||
.database
|
.database
|
||||||
@ -38,7 +37,7 @@ pub async fn load_muted_members(ctx: &serenity::Context, _: &serenity::Ready) {
|
|||||||
data.database.clone(),
|
data.database.clone(),
|
||||||
serenity::GuildId(guild_id),
|
serenity::GuildId(guild_id),
|
||||||
serenity::UserId(user_id),
|
serenity::UserId(user_id),
|
||||||
mute_role_id,
|
data.configuration.general.mute.role,
|
||||||
amount_left as u64, // i64 as u64 is handled properly here
|
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 once_cell::sync::OnceCell;
|
||||||
use tracing::{error, info, trace};
|
use tracing::{error, info, trace};
|
||||||
|
|
||||||
use super::{bot, serenity};
|
use super::serenity;
|
||||||
|
use crate::model::application::Censor as CensorConfiguration;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref CENSOR: OnceCell<censor::Censor> = OnceCell::new();
|
static ref CENSOR: OnceCell<censor::Censor> = OnceCell::new();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize the censor
|
// Initialize censor with the given configuration.
|
||||||
async fn censor(ctx: &serenity::Context) -> &'static Censor {
|
pub fn init_censor(configuration: &CensorConfiguration) -> &'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(|| {
|
CENSOR.get_or_init(|| {
|
||||||
let mut censor = censor::Standard;
|
let mut censor = censor::Standard;
|
||||||
|
|
||||||
for addition in additions {
|
for addition in &configuration.additions {
|
||||||
censor += addition;
|
censor += addition;
|
||||||
}
|
}
|
||||||
|
|
||||||
for removal in removals {
|
for removal in &configuration.removals {
|
||||||
censor -= removal;
|
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(
|
pub async fn cure(
|
||||||
ctx: &serenity::Context,
|
ctx: &serenity::Context,
|
||||||
old_if_available: &Option<serenity::Member>,
|
old_if_available: &Option<serenity::Member>,
|
||||||
@ -49,8 +36,6 @@ pub async fn cure(
|
|||||||
trace!("Skipping decancer for bot {}.", member.user.tag());
|
trace!("Skipping decancer for bot {}.", member.user.tag());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let censor = censor(ctx).await;
|
|
||||||
|
|
||||||
let name = member.display_name().to_string();
|
let name = member.display_name().to_string();
|
||||||
|
|
||||||
@ -71,7 +56,7 @@ pub async fn cure(
|
|||||||
|
|
||||||
if cured_name.is_empty()
|
if cured_name.is_empty()
|
||||||
|| !cured_name.starts_with(|c: char| c.is_ascii_alphabetic())
|
|| !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();
|
cured_name = "ReVanced member".to_string();
|
||||||
}
|
}
|
@ -3,7 +3,7 @@ use poise::serenity_prelude::{self as serenity, Member, RoleId};
|
|||||||
|
|
||||||
pub mod bot;
|
pub mod bot;
|
||||||
pub mod code_embed;
|
pub mod code_embed;
|
||||||
pub mod decancer;
|
pub mod cure_names;
|
||||||
pub mod macros;
|
pub mod macros;
|
||||||
pub mod message;
|
pub mod message;
|
||||||
pub mod message_response;
|
pub mod message_response;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user