Skip to content
Merged
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
8 changes: 4 additions & 4 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ impl Application {
set_command!("mod-dash", "moderation", crate::commands::moderation::dash::run),
set_command!("clear", "moderation", crate::commands::moderation::clear::run),

set_command!("top week all", "top", crate::commands::top::all::run),
set_command!("top day all", "top", crate::commands::top::all::run),
set_command!("top week me", "top", crate::commands::top::me::run),
set_command!("top day me", "top", crate::commands::top::me::run),
set_command!("top week all", "activity", crate::commands::top::all::run),
set_command!("top day all", "activity", crate::commands::top::all::run),
set_command!("top week me", "activity", crate::commands::top::me::run),
set_command!("top day me", "activity", crate::commands::top::me::run),

set_command!("setup", "settings", crate::commands::settings::setup::run)
]);
Expand Down
10 changes: 6 additions & 4 deletions src/commands/moderation/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ pub async fn run(
interaction.command_text.as_str(), &config
).ok_or("Cannot find any action type matching command name")?;

let moderation_config = &config.moderation.ok_or("This module is disabled")?;

let target_member = get_target_member(
&discord_http, guild_id, target_id
).await.map_err(Error::from)?;
Expand Down Expand Up @@ -100,7 +102,7 @@ pub async fn run(
let mut roles = target_member
.ok_or("You can mute only user server members (User left or didn't join this server)")?
.roles;
roles.push(config.moderation.mute_role.ok_or("There is no role for muted users set")?);
roles.push(moderation_config.mute_role.ok_or("There is no role for muted users set")?);

discord_http.update_guild_member(config.guild_id, target_id)
.roles(&roles).await.map_err(Error::from)?;
Expand Down Expand Up @@ -149,8 +151,8 @@ pub async fn run(
let result_case = context.mongodb.create_case(
discord_http.to_owned(), &context.redis, case,
case_embed.to_owned(),
if config.moderation.dm_case { Some(target_id) } else { None },
config.moderation.logs_channel
if moderation_config.dm_case { Some(target_id) } else { None },
moderation_config.logs_channel
).await.err();

Ok((InteractionResponseData {
Expand Down Expand Up @@ -179,7 +181,7 @@ fn command_to_action_type(command_name: &str, config: &GuildConfig) -> Option<Ca
let action_type = match command_name {
"warn" => CaseActionType::Warn,
"timeout" | "mute" => {
match config.moderation.mute_mode {
match config.moderation.as_ref()?.mute_mode {
MuteMode::Timeout => CaseActionType::Timeout,
MuteMode::Role => CaseActionType::Mute,
MuteMode::DependOnCommand => {
Expand Down
2 changes: 1 addition & 1 deletion src/events/automod/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async fn send_logs(
guild_config: Arc<GuildConfig>,
reason: String
) -> Result<(), ()> {
let channel = guild_config.moderation.automod.as_ref().ok_or(())?.logs_channel.ok_or(())?;
let channel = guild_config.moderation.as_ref().ok_or(())?.automod.as_ref().ok_or(())?.logs_channel.ok_or(())?;

let avatar = get_avatar_url(message.author.avatar, message.author.id);
let embed = Embed {
Expand Down
2 changes: 1 addition & 1 deletion src/events/automod/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub async fn run(
) -> Result<(), ()> {
let guild_id = message.guild_id.ok_or(())?;
let guild_config = Arc::new(context.mongodb.get_config(guild_id).await.map_err(|_| ())?);
let automod_config = guild_config.moderation.automod.as_ref().ok_or(())?;
let automod_config = guild_config.moderation.as_ref().ok_or(())?.automod.as_ref().ok_or(())?;

if message.content.is_empty() || message.author.bot {
return Ok(())
Expand Down
8 changes: 5 additions & 3 deletions src/events/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ pub async fn run(event: Box<GuildAuditLogEntryCreate>, discord_http: Arc<Client>
let target_id = event.target_id.ok_or(())?;

let guild_config = context.mongodb.get_config(guild_id).await.map_err(|_| ())?;
if !guild_config.moderation.native_support {
let moderation_config = guild_config.moderation.ok_or(())?;

if !moderation_config.native_support {
return Err(())
}

Expand Down Expand Up @@ -62,8 +64,8 @@ pub async fn run(event: Box<GuildAuditLogEntryCreate>, discord_http: Arc<Client>
&context.redis,
case,
embed,
if guild_config.moderation.dm_case { Some(target_id.cast()) } else { None },
guild_config.moderation.logs_channel
if moderation_config.dm_case { Some(target_id.cast()) } else { None },
moderation_config.logs_channel
).await.map_err(|_| ())?;

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/events/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub mod mutes {
context: Arc<Context>
) -> Result<(), ()> {
let config = context.mongodb.get_config(member.guild_id).await.map_err(|_| ())?;
let mute_role = config.moderation.mute_role.ok_or(())?;
let mute_role = config.moderation.ok_or(())?.mute_role.ok_or(())?;

let task = context.mongodb.tasks.find_one(doc! {
"action": { "RemoveMuteRole": member.user.id.to_string() },
Expand Down
5 changes: 3 additions & 2 deletions src/events/top.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ pub async fn run(

let guild_id = message.guild_id.ok_or(())?;
let config = context.mongodb.get_config(guild_id).await.map_err(|_| ())?;
let top_config = config.top.ok_or(())?;
let author_id = message.author.id;

if config.top.week {
if top_config.week {
context.redis
.increase(format!("top_week.{guild_id}"), author_id, 1)
.await
.map_err(|_| ())?;
}

if config.top.day {
if top_config.day {
context.redis
.increase(format!("top_day.{guild_id}"), author_id, 1)
.await
Expand Down
33 changes: 8 additions & 25 deletions src/models/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Serialize, Deserialize};
use twilight_model::id::Id;
use twilight_model::id::marker::{ApplicationMarker, GuildMarker};
use crate::models::config::activity::{Levels, Top};
use crate::models::config::moderation::{Moderation, MuteMode};
use crate::models::config::moderation::Moderation;

use self::automod::actions::BucketAction;

Expand All @@ -15,42 +15,25 @@ pub mod automod;
pub struct GuildConfig {
pub guild_id: Id<GuildMarker>,
pub application_id: Option<Id<ApplicationMarker>>,
pub enabled: HashMap<String, bool>,
pub moderation: Moderation,
pub moderation: Option<Moderation>,
pub premium: bool,
pub levels: Levels,
pub top: Top
pub levels: Option<Levels>,
pub top: Option<Top>
}

impl GuildConfig {
pub fn new(guild_id: Id<GuildMarker>) -> Self {
Self {
guild_id,
application_id: None,
enabled: HashMap::new(),
moderation: Moderation {
mute_mode: MuteMode::Timeout,
mute_role: None,
native_support: false,
logs_channel: None,
dm_case: false,
automod: None
},
moderation: None,
premium: false,
levels: Levels {
xp_timeout: 0,
xp_min: 0,
xp_max: 0
},
top: Top {
week: false,
day: false,
webhook_url: "".to_string()
}
levels: None,
top: None
}
}

pub fn get_bucket_action(&self, key: &str) -> Option<BucketAction> {
self.moderation.automod.as_ref().map(|a| a.bucket_actions.get(key).cloned())?
self.moderation.as_ref()?.automod.as_ref().map(|a| a.bucket_actions.get(key).cloned())?
}
}
3 changes: 0 additions & 3 deletions src/server/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ async fn handle_command(
extract!(interaction_ctx.orginal, guild_id);

let config = context.mongodb.get_config(guild_id).await.map_err(Error::from)?;
if command.module != "settings" {
config.enabled.get(command.module.as_str()).ok_or("This module is disabled")?;
}

let execute_as_slower = interaction_ctx.orginal.target_id().is_none()
&& context.application.is_slower(&command.name).await;
Expand Down
2 changes: 1 addition & 1 deletion src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub async fn run_action(task: Task, config: GuildConfig, discord_http: Arc<Clien
let member = discord_http.guild_member(config.guild_id, member_id)
.await.map_err(|_| ())?.model().await.map_err(|_| ())?;

let mute_role = config.moderation.mute_role.ok_or(())?;
let mute_role = config.moderation.ok_or(())?.mute_role.ok_or(())?;
let roles_without_mute_role = member.roles.iter()
.filter(|role| role != &&mute_role).cloned().collect::<Vec<Id<RoleMarker>>>();

Expand Down
27 changes: 13 additions & 14 deletions src/utils/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ pub fn create_debug_config() -> GuildConfig {
GuildConfig {
guild_id: Id::new(981950094804930581),
application_id: None,
enabled: HashMap::from([("moderation".to_string(), true)]),
moderation: Moderation {
moderation: Some(Moderation {
automod: Some(AutoModeration {
rules: vec![AutoModerationRule {
basic_type: None,
Expand All @@ -21,14 +20,14 @@ pub fn create_debug_config() -> GuildConfig {
checks: vec![],
actions: vec![
ActionMetadata {
action: Action::IncreaseBucket(IncreaseBucket {
key: "mentions".to_owned(),
amount: IncreaseBucketAmount::Mentions,
per_channel: false,
duration: 5
}),
action: Action::IncreaseBucket(IncreaseBucket {
key: "mentions".to_owned(),
amount: IncreaseBucketAmount::Mentions,
per_channel: false,
duration: 5
}),
sync: false
}
}
],
ignore: Some(Ignore {
channels: vec![Id::new(981950096801406979)],
Expand Down Expand Up @@ -62,17 +61,17 @@ pub fn create_debug_config() -> GuildConfig {
native_support: true,
logs_channel: Some(Id::new(981950096801406979)),
dm_case: true,
},
}),
premium: true,
levels: Levels {
levels: Some(Levels {
xp_timeout: 30,
xp_min: 5,
xp_max: 5,
},
top: Top {
}),
top: Some(Top {
week: true,
day: true,
webhook_url: String::new(),
},
}),
}
}