Skip to content

Commit

Permalink
Rework EditAutoModRule
Browse files Browse the repository at this point in the history
Also changed model methods on `Guild` and `PartialGuild` to take `&self`
instead of `self`.
  • Loading branch information
mkrasnitski committed Jul 24, 2022
1 parent 3af1e35 commit 99c0788
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 83 deletions.
57 changes: 45 additions & 12 deletions src/builder/edit_automod_rule.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
#[cfg(feature = "http")]
use crate::http::Http;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
#[cfg(feature = "http")]
use crate::model::guild::automod::Rule;
use crate::model::guild::automod::{Action, EventType, Trigger};
use crate::model::id::{ChannelId, RoleId};
use crate::model::prelude::*;

#[derive(Clone, Debug, Serialize)]
#[must_use]
/// A builder for creating or editing guild automoderation rules.
///
/// # Examples
///
/// See [`GuildId::create_automod_rule`] for details.
pub struct EditAutoModRule {
event_type: EventType,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -19,45 +31,66 @@ pub struct EditAutoModRule {
}

impl EditAutoModRule {
/// Creates or edits an automoderation [`Rule`] in a guild. Passing `Some(rule_id)` will edit
/// that corresponding rule, otherwise a new rule will be created.
///
/// **Note**: Requires the [Manage Guild] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission, or if invalid data is given.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
#[cfg(feature = "http")]
pub async fn execute(
self,
http: impl AsRef<Http>,
guild_id: GuildId,
rule_id: Option<RuleId>,
) -> Result<Rule> {
let http = http.as_ref();
match rule_id {
Some(rule_id) => http.edit_automod_rule(guild_id.into(), rule_id.into(), &self).await,
None => http.create_automod_rule(guild_id.into(), &self).await,
}
}

/// The display name of the rule.
pub fn name(&mut self, name: impl Into<String>) -> &mut Self {
pub fn name(mut self, name: impl Into<String>) -> 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 {
pub fn event_type(mut self, event_type: EventType) -> Self {
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 {
pub fn trigger(mut self, trigger: Trigger) -> Self {
self.trigger = Some(trigger);
self
}

/// Set the actions which will execute when the rule is triggered.
pub fn actions(&mut self, actions: Vec<Action>) -> &mut Self {
pub fn actions(mut self, actions: Vec<Action>) -> Self {
self.actions = Some(actions);
self
}

/// Set whether the rule is enabled.
pub fn enabled(&mut self, enabled: bool) -> &mut Self {
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = Some(enabled);
self
}

/// Set roles that should not be affected by the rule.
///
/// Maximum of 20.
pub fn exempt_roles(
&mut self,
roles: impl IntoIterator<Item = impl Into<RoleId>>,
) -> &mut Self {
pub fn exempt_roles(mut self, roles: impl IntoIterator<Item = impl Into<RoleId>>) -> Self {
self.exempt_roles = Some(roles.into_iter().map(Into::into).collect());
self
}
Expand All @@ -66,9 +99,9 @@ impl EditAutoModRule {
///
/// Maximum of 50.
pub fn exempt_channels(
&mut self,
mut self,
channels: impl IntoIterator<Item = impl Into<ChannelId>>,
) -> &mut Self {
) -> Self {
self.exempt_channels = Some(channels.into_iter().map(Into::into).collect());
self
}
Expand Down
35 changes: 13 additions & 22 deletions src/model/guild/guild_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,61 +85,52 @@ impl GuildId {
/// ```
/// use std::time::Duration;
///
/// use serenity::builder::EditAutoModRule;
/// use serenity::model::guild::automod::{Action, Trigger};
/// use serenity::model::id::GuildId;
///
/// # async fn run() {
/// # use serenity::http::Http;
/// # let http = Http::new("token");
/// let _rule = GuildId::new(7)
/// .create_automod_rule(&http, |r| {
/// r.name("foobar filter")
/// .trigger(Trigger::Keyword(vec!["foo*".to_string(), "*bar".to_string()]))
/// .actions(vec![Action::BlockMessage, Action::Timeout(Duration::from_secs(60))])
/// })
/// .await;
/// let builder = EditAutoModRule::default()
/// .name("foobar filter")
/// .trigger(Trigger::Keyword(vec!["foo*".to_string(), "*bar".to_string()]))
/// .actions(vec![Action::BlockMessage, Action::Timeout(Duration::from_secs(60))]);
/// let _rule = GuildId::new(7).create_automod_rule(&http, builder).await;
/// # }
/// ```
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission,
/// or if invalid values are set.
/// Returns [`Error::Http`] if the current user lacks permission, or if invalid data is given.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
#[inline]
pub async fn create_automod_rule(
self,
http: impl AsRef<Http>,
f: impl FnOnce(&mut EditAutoModRule) -> &mut EditAutoModRule,
builder: EditAutoModRule,
) -> Result<Rule> {
let mut builder = EditAutoModRule::default();
f(&mut builder);

http.as_ref().create_automod_rule(self.get(), &builder).await
builder.execute(http, self, None).await
}

/// Edit an auto moderation [`Rule`] by its ID.
/// Edit an auto moderation [`Rule`], given its Id.
///
/// **Note**: Requires the [Manage Guild] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission,
/// or if invalid values are set.
/// Returns [`Error::Http`] if the current user lacks permission, or if invalid data is given.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
#[inline]
pub async fn edit_automod_rule(
self,
http: impl AsRef<Http>,
rule_id: impl Into<RuleId>,
f: impl FnOnce(&mut EditAutoModRule) -> &mut EditAutoModRule,
builder: EditAutoModRule,
) -> Result<Rule> {
let mut builder = EditAutoModRule::default();
f(&mut builder);

http.as_ref().edit_automod_rule(self.get(), rule_id.into().0.get(), &builder).await
builder.execute(http, self, Some(rule_id.into())).await
}

/// Deletes an auto moderation [`Rule`] from the guild.
Expand Down
35 changes: 10 additions & 25 deletions src/model/guild/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,54 +299,39 @@ impl Guild {
///
/// # Examples
///
/// Create a custom keyword filter to block the message and timeout the author.
///
/// ```ignore
/// use serenity::model::guild::automod::{Action, Trigger};
/// use serenity::model::id::GuildId;
///
/// let _rule = guild
/// .create_automod_rule(&http, |r| {
/// r.name("foobar filter")
/// .trigger(Trigger::Keyword(vec!["foo*".to_string(), "*bar".to_string()]))
/// .actions(vec![Action::BlockMessage, Action::Timeout(60)])
/// })
/// .await;
/// ```
/// See [`GuildId::create_automod_rule`] for details.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission,
/// or if invalid values are set.
/// Returns [`Error::Http`] if the current user lacks permission, or if invalid data is given.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
#[inline]
pub async fn create_automod_rule(
self,
&self,
http: impl AsRef<Http>,
f: impl FnOnce(&mut EditAutoModRule) -> &mut EditAutoModRule,
builder: EditAutoModRule,
) -> Result<Rule> {
self.id.create_automod_rule(http, f).await
self.id.create_automod_rule(http, builder).await
}

/// Edit an auto moderation [`Rule`] by its ID.
/// Edit an auto moderation [`Rule`], given its Id.
///
/// **Note**: Requires the [Manage Guild] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission,
/// or if invalid values are set.
/// Returns [`Error::Http`] if the current user lacks permission, or if invalid data is given.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
#[inline]
pub async fn edit_automod_rule(
self,
&self,
http: impl AsRef<Http>,
rule_id: impl Into<RuleId>,
f: impl FnOnce(&mut EditAutoModRule) -> &mut EditAutoModRule,
builder: EditAutoModRule,
) -> Result<Rule> {
self.id.edit_automod_rule(http, rule_id, f).await
self.id.edit_automod_rule(http, rule_id, builder).await
}

/// Deletes an auto moderation [`Rule`] from the guild.
Expand Down
34 changes: 10 additions & 24 deletions src/model/guild/partial_guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,53 +191,39 @@ impl PartialGuild {
///
/// # Examples
///
/// Create a custom keyword filter to block the message and timeout the author.
///
/// ```ignore
/// use serenity::model::guild::automod::{Action, Trigger};
///
/// let _rule = guild
/// .create_automod_rule(&http, |r| {
/// r.name("foobar filter")
/// .trigger(Trigger::Keyword(vec!["foo*".to_string(), "*bar".to_string()]))
/// .actions(vec![Action::BlockMessage, Action::Timeout(60)])
/// })
/// .await;
/// ```
/// See [`GuildId::create_automod_rule`] for details.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission,
/// or if invalid values are set.
/// Returns [`Error::Http`] if the current user lacks permission, or if invalid data is given.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
#[inline]
pub async fn create_automod_rule(
self,
&self,
http: impl AsRef<Http>,
f: impl FnOnce(&mut EditAutoModRule) -> &mut EditAutoModRule,
builder: EditAutoModRule,
) -> Result<Rule> {
self.id.create_automod_rule(http, f).await
self.id.create_automod_rule(http, builder).await
}

/// Edit an auto moderation [`Rule`] by its ID.
/// Edit an auto moderation [`Rule`], given its Id.
///
/// **Note**: Requires the [Manage Guild] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission,
/// or if invalid values are set.
/// Returns [`Error::Http`] if the current user lacks permission, or if invalid data is given.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
#[inline]
pub async fn edit_automod_rule(
self,
&self,
http: impl AsRef<Http>,
rule_id: impl Into<RuleId>,
f: impl FnOnce(&mut EditAutoModRule) -> &mut EditAutoModRule,
builder: EditAutoModRule,
) -> Result<Rule> {
self.id.edit_automod_rule(http, rule_id, f).await
self.id.edit_automod_rule(http, rule_id, builder).await
}

/// Deletes an auto moderation [`Rule`] from the guild.
Expand Down

0 comments on commit 99c0788

Please sign in to comment.