New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Auto Moderation
feature
#2035
Merged
Merged
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
502dee7
Add gateway intents for auto moderation
nickelc cd12e52
Add basic rule types
nickelc fd2409c
Change `automod::Action` into enum
nickelc ae61957
Change `Rule::trigger_type` & `Rule::trigger_metadata` into an enum
nickelc d4194f7
Add audit log types for auto moderation
nickelc 477fa9d
Add routing create/edit/delete/list
nickelc a7a0d46
Add gateway event handling for rule create/update/delete
nickelc 83a7d7e
Add gateway event for auto moderation action execution
nickelc 9c00b15
Add http methods get/create/edit/delete/list
nickelc bf43eb6
Add `GuildId`,`Guild` & `PartialGuild` methods for get/create/edit/de…
nickelc ca32a4a
Fix typo
nickelc ff45fc8
Use `std::time::Duration` as timeout value
nickelc 62958f1
fixup! Use `std::time::Duration` as timeout value
nickelc 847aa5e
Document reasoning for manual implementations
nickelc 42a0dac
Fix typos
nickelc 98f5031
Use `#[serde(flatten)]` for `Rule::trigger`
nickelc 087bc6d
Simplify tests with dummy `Rule` structs
nickelc 302304e
There is only one builder
nickelc File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::json::{json, Value}; | ||
use crate::model::guild::automod::{Action, EventType, Trigger}; | ||
use crate::model::id::{ChannelId, RoleId}; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct CreateAutoModRule(pub HashMap<&'static str, Value>); | ||
|
||
impl CreateAutoModRule { | ||
/// The display name of the rule. | ||
pub fn name<D: ToString>(&mut self, name: D) -> &mut Self { | ||
self.0.insert("name", Value::from(name.to_string())); | ||
|
||
self | ||
} | ||
|
||
/// Set the event context the rule should be checked. | ||
pub fn event_type(&mut self, event_type: EventType) -> &mut Self { | ||
self.0.insert("event_type", u8::from(event_type).into()); | ||
|
||
self | ||
} | ||
|
||
/// Set the type of content which can trigger the rule. | ||
pub fn trigger(&mut self, trigger: Trigger) -> &mut Self { | ||
self.0.insert("trigger_type", u8::from(trigger.kind()).into()); | ||
|
||
match trigger { | ||
Trigger::Keyword(keyword_filter) => { | ||
let value = json!({ | ||
"keyword_filter": keyword_filter, | ||
}); | ||
self.0.insert("trigger_metadata", value); | ||
}, | ||
Trigger::KeywordPresent(presets) => { | ||
let value = json!({ | ||
"presets": presets, | ||
}); | ||
self.0.insert("trigger_metadata", value); | ||
}, | ||
_ => {}, | ||
} | ||
|
||
self | ||
} | ||
|
||
/// Set the actions which will execute when the rule is triggered. | ||
pub fn actions<I>(&mut self, actions: I) -> &mut Self | ||
where | ||
I: IntoIterator<Item = Action>, | ||
{ | ||
let actions = actions | ||
.into_iter() | ||
.map(|action| { | ||
let kind = action.kind(); | ||
match action { | ||
Action::Alert(channel_id) => { | ||
json!({ | ||
"type": kind, | ||
"metadata": { | ||
"channel_id": channel_id.0.to_string(), | ||
}, | ||
}) | ||
}, | ||
Action::Timeout(duration) => { | ||
json!({ | ||
"type": kind, | ||
"metadata": { | ||
"duration_seconds": duration, | ||
}, | ||
}) | ||
}, | ||
Action::BlockMessage | Action::Unknown(_) => { | ||
json!({ | ||
"type": kind, | ||
}) | ||
}, | ||
} | ||
}) | ||
.collect(); | ||
|
||
self.0.insert("actions", actions); | ||
|
||
self | ||
} | ||
|
||
/// Set whether the rule is enabled. | ||
pub fn enabled(&mut self, enabled: bool) -> &mut Self { | ||
self.0.insert("enabled", Value::from(enabled)); | ||
|
||
self | ||
} | ||
|
||
/// Set roles that should not be affected by the rule. | ||
/// | ||
/// Maximum of 20. | ||
pub fn exempt_roles<I>(&mut self, roles: I) -> &mut Self | ||
where | ||
I: IntoIterator<Item = RoleId>, | ||
{ | ||
let ids = roles.into_iter().map(|id| id.0.to_string()).collect(); | ||
|
||
self.0.insert("exempt_roles", ids); | ||
|
||
self | ||
} | ||
|
||
/// Set channels that should not be affected by the rule. | ||
/// | ||
/// Maximum of 50. | ||
pub fn exempt_channels<I>(&mut self, channels: I) -> &mut Self | ||
where | ||
I: IntoIterator<Item = ChannelId>, | ||
{ | ||
let ids = channels.into_iter().map(|id| id.0.to_string()).collect(); | ||
|
||
self.0.insert("exempt_channels", ids); | ||
|
||
self | ||
} | ||
} | ||
|
||
impl Default for CreateAutoModRule { | ||
fn default() -> Self { | ||
let mut builder = CreateAutoModRule(HashMap::new()); | ||
builder.0.insert("event_type", u8::from(EventType::MessageSend).into()); | ||
|
||
builder | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::json::{json, Value}; | ||
use crate::model::guild::automod::{Action, EventType, Trigger}; | ||
use crate::model::id::{ChannelId, RoleId}; | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct EditAutoModRule(pub HashMap<&'static str, Value>); | ||
|
||
impl EditAutoModRule { | ||
/// The display name of the rule. | ||
pub fn name<D: ToString>(&mut self, name: D) -> &mut Self { | ||
self.0.insert("name", Value::from(name.to_string())); | ||
|
||
self | ||
} | ||
|
||
/// Set the event context the rule should be checked. | ||
pub fn event_type(&mut self, event_type: EventType) -> &mut Self { | ||
self.0.insert("event_type", u8::from(event_type).into()); | ||
|
||
self | ||
} | ||
|
||
/// Set the type of content which can trigger the rule. | ||
/// | ||
/// **None**: The trigger type can't be edited. Only its values. | ||
pub fn trigger(&mut self, trigger: Trigger) -> &mut Self { | ||
match trigger { | ||
Trigger::Keyword(keyword_filter) => { | ||
let value = json!({ | ||
"keyword_filter": keyword_filter, | ||
}); | ||
self.0.insert("trigger_metadata", value); | ||
}, | ||
Trigger::KeywordPresent(presets) => { | ||
let value = json!({ | ||
"presets": presets, | ||
}); | ||
self.0.insert("trigger_metadata", value); | ||
}, | ||
_ => {}, | ||
} | ||
|
||
self | ||
} | ||
|
||
/// Set the actions which will execute when the rule is triggered. | ||
pub fn actions<I>(&mut self, actions: I) -> &mut Self | ||
where | ||
I: IntoIterator<Item = Action>, | ||
{ | ||
let actions = actions | ||
.into_iter() | ||
.map(|action| { | ||
let kind = action.kind(); | ||
match action { | ||
Action::Alert(channel_id) => { | ||
json!({ | ||
"type": kind, | ||
"metadata": { | ||
"channel_id": channel_id.0.to_string(), | ||
}, | ||
}) | ||
}, | ||
Action::Timeout(duration) => { | ||
json!({ | ||
"type": kind, | ||
"metadata": { | ||
"duration_seconds": duration, | ||
}, | ||
}) | ||
}, | ||
Action::BlockMessage | Action::Unknown(_) => { | ||
json!({ | ||
"type": kind, | ||
}) | ||
}, | ||
} | ||
}) | ||
.collect(); | ||
|
||
self.0.insert("actions", actions); | ||
|
||
self | ||
} | ||
|
||
/// Set whether the rule is enabled. | ||
pub fn enabled(&mut self, enabled: bool) -> &mut Self { | ||
self.0.insert("enabled", Value::from(enabled)); | ||
|
||
self | ||
} | ||
|
||
/// Set roles that should not be affected by the rule. | ||
/// | ||
/// Maximum of 20. | ||
pub fn exempt_roles<I>(&mut self, roles: I) -> &mut Self | ||
where | ||
I: IntoIterator<Item = RoleId>, | ||
{ | ||
let ids = roles.into_iter().map(|id| id.0.to_string()).collect(); | ||
|
||
self.0.insert("exempt_roles", ids); | ||
|
||
self | ||
} | ||
|
||
/// Set channels that should not be affected by the rule. | ||
/// | ||
/// Maximum of 50. | ||
pub fn exempt_channels<I>(&mut self, channels: I) -> &mut Self | ||
where | ||
I: IntoIterator<Item = ChannelId>, | ||
{ | ||
let ids = channels.into_iter().map(|id| id.0.to_string()).collect(); | ||
|
||
self.0.insert("exempt_channels", ids); | ||
|
||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EditAutoModRule is almost a full copy of CreateAutoModRole. Maybe the same type can be used for both? With basically just a small change here: "**Note**: When editing a rule, the trigger type can't be edited. Only its values."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That can probably said about every Create/Edit builder combination in serenity.
EditAutoModRule
doesn't set thetrigger_type
, only the metadata.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright I guess then let's keep the duplication for consistency lol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I had the impression the builders come in pairs (create/edit) but it's actually either
Create*
orEdit*
.I was not looking for a pattern of when to use one or the other.
I rolled a dice and
Edit
stays.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't looked at this closely, but I will say there's a precedent here. Specifically,
CreateInteractionResponseFollowup
is used for both creating and editing followup interaction messages. I don't know if there's any other builders that are used for both.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably rename to
CreateAutoModRole
then for consistencyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found a counter-example in
EditRole
, which is used for both creation and editing of roles. I think this can stay named as is, to not cause further friction.