Skip to content

Commit 45b1ce2

Browse files
authored
Remove enabled field from guild configuration (#29)
1 parent 2b53d10 commit 45b1ce2

11 files changed

Lines changed: 43 additions & 59 deletions

File tree

src/application.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ impl Application {
101101
set_command!("mod-dash", "moderation", crate::commands::moderation::dash::run),
102102
set_command!("clear", "moderation", crate::commands::moderation::clear::run),
103103

104-
set_command!("top week all", "top", crate::commands::top::all::run),
105-
set_command!("top day all", "top", crate::commands::top::all::run),
106-
set_command!("top week me", "top", crate::commands::top::me::run),
107-
set_command!("top day me", "top", crate::commands::top::me::run),
104+
set_command!("top week all", "activity", crate::commands::top::all::run),
105+
set_command!("top day all", "activity", crate::commands::top::all::run),
106+
set_command!("top week me", "activity", crate::commands::top::me::run),
107+
set_command!("top day me", "activity", crate::commands::top::me::run),
108108

109109
set_command!("setup", "settings", crate::commands::settings::setup::run)
110110
]);

src/commands/moderation/execute.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ pub async fn run(
5656
interaction.command_text.as_str(), &config
5757
).ok_or("Cannot find any action type matching command name")?;
5858

59+
let moderation_config = &config.moderation.ok_or("This module is disabled")?;
60+
5961
let target_member = get_target_member(
6062
&discord_http, guild_id, target_id
6163
).await.map_err(Error::from)?;
@@ -100,7 +102,7 @@ pub async fn run(
100102
let mut roles = target_member
101103
.ok_or("You can mute only user server members (User left or didn't join this server)")?
102104
.roles;
103-
roles.push(config.moderation.mute_role.ok_or("There is no role for muted users set")?);
105+
roles.push(moderation_config.mute_role.ok_or("There is no role for muted users set")?);
104106

105107
discord_http.update_guild_member(config.guild_id, target_id)
106108
.roles(&roles).await.map_err(Error::from)?;
@@ -149,8 +151,8 @@ pub async fn run(
149151
let result_case = context.mongodb.create_case(
150152
discord_http.to_owned(), &context.redis, case,
151153
case_embed.to_owned(),
152-
if config.moderation.dm_case { Some(target_id) } else { None },
153-
config.moderation.logs_channel
154+
if moderation_config.dm_case { Some(target_id) } else { None },
155+
moderation_config.logs_channel
154156
).await.err();
155157

156158
Ok((InteractionResponseData {
@@ -179,7 +181,7 @@ fn command_to_action_type(command_name: &str, config: &GuildConfig) -> Option<Ca
179181
let action_type = match command_name {
180182
"warn" => CaseActionType::Warn,
181183
"timeout" | "mute" => {
182-
match config.moderation.mute_mode {
184+
match config.moderation.as_ref()?.mute_mode {
183185
MuteMode::Timeout => CaseActionType::Timeout,
184186
MuteMode::Role => CaseActionType::Mute,
185187
MuteMode::DependOnCommand => {

src/events/automod/actions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ async fn send_logs(
7171
guild_config: Arc<GuildConfig>,
7272
reason: String
7373
) -> Result<(), ()> {
74-
let channel = guild_config.moderation.automod.as_ref().ok_or(())?.logs_channel.ok_or(())?;
74+
let channel = guild_config.moderation.as_ref().ok_or(())?.automod.as_ref().ok_or(())?.logs_channel.ok_or(())?;
7575

7676
let avatar = get_avatar_url(message.author.avatar, message.author.id);
7777
let embed = Embed {

src/events/automod/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub async fn run(
4242
) -> Result<(), ()> {
4343
let guild_id = message.guild_id.ok_or(())?;
4444
let guild_config = Arc::new(context.mongodb.get_config(guild_id).await.map_err(|_| ())?);
45-
let automod_config = guild_config.moderation.automod.as_ref().ok_or(())?;
45+
let automod_config = guild_config.moderation.as_ref().ok_or(())?.automod.as_ref().ok_or(())?;
4646

4747
if message.content.is_empty() || message.author.bot {
4848
return Ok(())

src/events/case.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ pub async fn run(event: Box<GuildAuditLogEntryCreate>, discord_http: Arc<Client>
2020
let target_id = event.target_id.ok_or(())?;
2121

2222
let guild_config = context.mongodb.get_config(guild_id).await.map_err(|_| ())?;
23-
if !guild_config.moderation.native_support {
23+
let moderation_config = guild_config.moderation.ok_or(())?;
24+
25+
if !moderation_config.native_support {
2426
return Err(())
2527
}
2628

@@ -62,8 +64,8 @@ pub async fn run(event: Box<GuildAuditLogEntryCreate>, discord_http: Arc<Client>
6264
&context.redis,
6365
case,
6466
embed,
65-
if guild_config.moderation.dm_case { Some(target_id.cast()) } else { None },
66-
guild_config.moderation.logs_channel
67+
if moderation_config.dm_case { Some(target_id.cast()) } else { None },
68+
moderation_config.logs_channel
6769
).await.map_err(|_| ())?;
6870

6971
Ok(())

src/events/restore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub mod mutes {
1111
context: Arc<Context>
1212
) -> Result<(), ()> {
1313
let config = context.mongodb.get_config(member.guild_id).await.map_err(|_| ())?;
14-
let mute_role = config.moderation.mute_role.ok_or(())?;
14+
let mute_role = config.moderation.ok_or(())?.mute_role.ok_or(())?;
1515

1616
let task = context.mongodb.tasks.find_one(doc! {
1717
"action": { "RemoveMuteRole": member.user.id.to_string() },

src/events/top.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ pub async fn run(
1313

1414
let guild_id = message.guild_id.ok_or(())?;
1515
let config = context.mongodb.get_config(guild_id).await.map_err(|_| ())?;
16+
let top_config = config.top.ok_or(())?;
1617
let author_id = message.author.id;
1718

18-
if config.top.week {
19+
if top_config.week {
1920
context.redis
2021
.increase(format!("top_week.{guild_id}"), author_id, 1)
2122
.await
2223
.map_err(|_| ())?;
2324
}
2425

25-
if config.top.day {
26+
if top_config.day {
2627
context.redis
2728
.increase(format!("top_day.{guild_id}"), author_id, 1)
2829
.await

src/models/config/mod.rs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use serde::{Serialize, Deserialize};
33
use twilight_model::id::Id;
44
use twilight_model::id::marker::{ApplicationMarker, GuildMarker};
55
use crate::models::config::activity::{Levels, Top};
6-
use crate::models::config::moderation::{Moderation, MuteMode};
6+
use crate::models::config::moderation::Moderation;
77

88
use self::automod::actions::BucketAction;
99

@@ -15,42 +15,25 @@ pub mod automod;
1515
pub struct GuildConfig {
1616
pub guild_id: Id<GuildMarker>,
1717
pub application_id: Option<Id<ApplicationMarker>>,
18-
pub enabled: HashMap<String, bool>,
19-
pub moderation: Moderation,
18+
pub moderation: Option<Moderation>,
2019
pub premium: bool,
21-
pub levels: Levels,
22-
pub top: Top
20+
pub levels: Option<Levels>,
21+
pub top: Option<Top>
2322
}
2423

2524
impl GuildConfig {
2625
pub fn new(guild_id: Id<GuildMarker>) -> Self {
2726
Self {
2827
guild_id,
2928
application_id: None,
30-
enabled: HashMap::new(),
31-
moderation: Moderation {
32-
mute_mode: MuteMode::Timeout,
33-
mute_role: None,
34-
native_support: false,
35-
logs_channel: None,
36-
dm_case: false,
37-
automod: None
38-
},
29+
moderation: None,
3930
premium: false,
40-
levels: Levels {
41-
xp_timeout: 0,
42-
xp_min: 0,
43-
xp_max: 0
44-
},
45-
top: Top {
46-
week: false,
47-
day: false,
48-
webhook_url: "".to_string()
49-
}
31+
levels: None,
32+
top: None
5033
}
5134
}
5235

5336
pub fn get_bucket_action(&self, key: &str) -> Option<BucketAction> {
54-
self.moderation.automod.as_ref().map(|a| a.bucket_actions.get(key).cloned())?
37+
self.moderation.as_ref()?.automod.as_ref().map(|a| a.bucket_actions.get(key).cloned())?
5538
}
5639
}

src/server/interaction.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ async fn handle_command(
2828
extract!(interaction_ctx.orginal, guild_id);
2929

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

3532
let execute_as_slower = interaction_ctx.orginal.target_id().is_none()
3633
&& context.application.is_slower(&command.name).await;

src/tasks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub async fn run_action(task: Task, config: GuildConfig, discord_http: Arc<Clien
6565
let member = discord_http.guild_member(config.guild_id, member_id)
6666
.await.map_err(|_| ())?.model().await.map_err(|_| ())?;
6767

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

0 commit comments

Comments
 (0)