forked from serenity-rs/serenity
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix clippy warnings and apply
next
patches to new code (serenity-rs…
…#2061) Co-authored-by: Michael Krasnitski <42564254+mkrasnitski@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
53 additions
and
99 deletions.
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 |
---|---|---|
@@ -1,133 +1,89 @@ | ||
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 EditAutoModRule(pub HashMap<&'static str, Value>); | ||
#[derive(Clone, Debug, Serialize)] | ||
pub struct EditAutoModRule { | ||
event_type: EventType, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
name: Option<String>, | ||
#[serde(flatten, skip_serializing_if = "Option::is_none")] | ||
trigger: Option<Trigger>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
actions: Option<Vec<Action>>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
enabled: Option<bool>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
exempt_roles: Option<Vec<RoleId>>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
exempt_channels: Option<Vec<ChannelId>>, | ||
} | ||
|
||
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())); | ||
|
||
pub fn name(&mut self, name: impl Into<String>) -> &mut Self { | ||
self.name = Some(name.into()); | ||
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.event_type = event_type; | ||
self | ||
} | ||
|
||
/// Set the type of content which can trigger the rule. | ||
/// | ||
/// **None**: The trigger type can't be edited after creation. Only its values. | ||
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::KeywordPreset(presets) => { | ||
let value = json!({ | ||
"presets": presets, | ||
}); | ||
self.0.insert("trigger_metadata", value); | ||
}, | ||
_ => {}, | ||
} | ||
|
||
self.trigger = Some(trigger); | ||
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); | ||
|
||
pub fn actions(&mut self, actions: Vec<Action>) -> &mut Self { | ||
self.actions = Some(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.enabled = Some(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); | ||
|
||
pub fn exempt_roles( | ||
&mut self, | ||
roles: impl IntoIterator<Item = impl Into<RoleId>>, | ||
) -> &mut Self { | ||
self.exempt_roles = Some(roles.into_iter().map(Into::into).collect()); | ||
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); | ||
|
||
pub fn exempt_channels( | ||
&mut self, | ||
channels: impl IntoIterator<Item = impl Into<ChannelId>>, | ||
) -> &mut Self { | ||
self.exempt_channels = Some(channels.into_iter().map(Into::into).collect()); | ||
self | ||
} | ||
} | ||
|
||
impl Default for EditAutoModRule { | ||
fn default() -> Self { | ||
let mut builder = Self(HashMap::new()); | ||
builder.event_type(EventType::MessageSend); | ||
|
||
builder | ||
Self { | ||
name: None, | ||
trigger: None, | ||
actions: None, | ||
enabled: None, | ||
exempt_roles: None, | ||
exempt_channels: None, | ||
event_type: EventType::MessageSend, | ||
} | ||
} | ||
} |
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