Skip to content
This repository was archived by the owner on Aug 7, 2024. It is now read-only.

feat: use censor to cure names with slurs #65

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ homepage = "https://revanced.app"
license = "GPL-3.0"
name = "revanced-discord-bot"
repository = "https://github.com/revanced/revanced-discord-bot"
version = "2.5.1"
version = "2.6.1"
edition = "2021"

[profile.release]
Expand Down Expand Up @@ -35,3 +35,6 @@ tracing-subscriber = "0.3.16"
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"
8 changes: 6 additions & 2 deletions configuration.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
0
]
},
"logging_channel": 0
"logging_channel": 0,
"censor": {
"additions": ["word", "another word"],
"removals": ["word", "another word"]
}
},
"administrators": {
"roles": [
Expand Down Expand Up @@ -41,7 +45,7 @@
},
"thread_options": {
"lock_on_response": false,
"close_on_response": false,
"close_on_response": false
}
}
]
Expand Down
12 changes: 10 additions & 2 deletions configuration.revanced.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@
]
},
"media_channels": [],
"logging_channel": 1027892160797872179
"logging_channel": 1027892160797872179,
"censor": {
"additions": [
"nigga"
],
"removals": [
"ass"
]
}
},
"administrators": {
"roles": [
Expand Down Expand Up @@ -364,4 +372,4 @@
}
}
]
}
}
8 changes: 5 additions & 3 deletions src/commands/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use tracing::debug;

use crate::utils::bot::load_configuration;
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

init_censor(&configuration.general.censor);

ctx.data().write().await.configuration = configuration;

debug!("{} reloaded the configuration.", ctx.author().name);
Expand Down
2 changes: 1 addition & 1 deletion src/events/guild_member_addition.rs
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/events/guild_member_update.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
12 changes: 12 additions & 0 deletions src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 1 addition & 2 deletions src/events/ready.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
),
);
Expand Down
6 changes: 6 additions & 0 deletions src/model/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub struct General {
pub embed_color: i32,
pub mute: Mute,
pub logging_channel: u64,
pub censor: Censor
}

#[derive(Default, Serialize, Deserialize)]
Expand All @@ -74,6 +75,11 @@ pub struct Mute {
pub take: Vec<u64>,
}
#[derive(Default, Serialize, Deserialize)]
pub struct Censor {
pub additions: Vec<String>,
pub removals: Vec<String>,
}
#[derive(Default, Serialize, Deserialize)]
pub struct Administrators {
pub roles: Vec<u64>,
pub users: Vec<u64>,
Expand Down
34 changes: 30 additions & 4 deletions src/utils/decancer.rs → src/utils/cure_names.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
extern crate decancer;

use censor::Censor;
use lazy_static::lazy_static;
use once_cell::sync::OnceCell;
use tracing::{error, info, trace};

use super::*;
use super::serenity;
use crate::model::application::Censor as CensorConfiguration;

lazy_static! {
static ref CENSOR: OnceCell<censor::Censor> = OnceCell::new();
}

// 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 &configuration.additions {
censor += addition;
}

for removal in &configuration.removals {
censor -= removal;
}

censor
})
}

pub async fn cure(
ctx: &serenity::Context,
Expand Down Expand Up @@ -31,7 +54,10 @@ pub async fn cure(
"",
);

if cured_name.is_empty() || !cured_name.starts_with(|c: char| c.is_ascii_alphabetic()) {
if cured_name.is_empty()
|| !cured_name.starts_with(|c: char| c.is_ascii_alphabetic())
|| CENSOR.get().unwrap().check(&cured_name)
{
cured_name = "ReVanced member".to_string();
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down