serenity/builder/
edit_automod_rule.rs

1#[cfg(feature = "http")]
2use super::Builder;
3#[cfg(feature = "http")]
4use crate::http::CacheHttp;
5#[cfg(feature = "http")]
6use crate::internal::prelude::*;
7use crate::model::guild::automod::EventType;
8use crate::model::prelude::*;
9
10/// A builder for creating or editing guild AutoMod rules.
11///
12/// # Examples
13///
14/// See [`GuildId::create_automod_rule`] for details.
15///
16/// [Discord docs](https://discord.com/developers/docs/resources/auto-moderation#modify-auto-moderation-rule)
17#[derive(Clone, Debug, Serialize)]
18#[must_use]
19pub struct EditAutoModRule<'a> {
20    #[serde(skip_serializing_if = "Option::is_none")]
21    name: Option<String>,
22    event_type: EventType,
23    #[serde(flatten, skip_serializing_if = "Option::is_none")]
24    trigger: Option<Trigger>,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    actions: Option<Vec<Action>>,
27    #[serde(skip_serializing_if = "Option::is_none")]
28    enabled: Option<bool>,
29    #[serde(skip_serializing_if = "Option::is_none")]
30    exempt_roles: Option<Vec<RoleId>>,
31    #[serde(skip_serializing_if = "Option::is_none")]
32    exempt_channels: Option<Vec<ChannelId>>,
33
34    #[serde(skip)]
35    audit_log_reason: Option<&'a str>,
36}
37
38impl<'a> EditAutoModRule<'a> {
39    /// Equivalent to [`Self::default`].
40    pub fn new() -> Self {
41        Self::default()
42    }
43
44    /// The display name of the rule.
45    pub fn name(mut self, name: impl Into<String>) -> Self {
46        self.name = Some(name.into());
47        self
48    }
49
50    /// Set the event context the rule should be checked.
51    pub fn event_type(mut self, event_type: EventType) -> Self {
52        self.event_type = event_type;
53        self
54    }
55
56    /// Set the type of content which can trigger the rule.
57    ///
58    /// **None**: The trigger type can't be edited after creation. Only its values.
59    pub fn trigger(mut self, trigger: Trigger) -> Self {
60        self.trigger = Some(trigger);
61        self
62    }
63
64    /// Set the actions which will execute when the rule is triggered.
65    pub fn actions(mut self, actions: Vec<Action>) -> Self {
66        self.actions = Some(actions);
67        self
68    }
69
70    /// Set whether the rule is enabled.
71    pub fn enabled(mut self, enabled: bool) -> Self {
72        self.enabled = Some(enabled);
73        self
74    }
75
76    /// Set roles that should not be affected by the rule.
77    ///
78    /// Maximum of 20.
79    pub fn exempt_roles(mut self, roles: impl IntoIterator<Item = impl Into<RoleId>>) -> Self {
80        self.exempt_roles = Some(roles.into_iter().map(Into::into).collect());
81        self
82    }
83
84    /// Set channels that should not be affected by the rule.
85    ///
86    /// Maximum of 50.
87    pub fn exempt_channels(
88        mut self,
89        channels: impl IntoIterator<Item = impl Into<ChannelId>>,
90    ) -> Self {
91        self.exempt_channels = Some(channels.into_iter().map(Into::into).collect());
92        self
93    }
94
95    /// Sets the request's audit log reason.
96    pub fn audit_log_reason(mut self, reason: &'a str) -> Self {
97        self.audit_log_reason = Some(reason);
98        self
99    }
100}
101
102impl Default for EditAutoModRule<'_> {
103    fn default() -> Self {
104        Self {
105            name: None,
106            trigger: None,
107            actions: None,
108            enabled: None,
109            exempt_roles: None,
110            exempt_channels: None,
111            event_type: EventType::MessageSend,
112            audit_log_reason: None,
113        }
114    }
115}
116
117#[cfg(feature = "http")]
118#[async_trait::async_trait]
119impl Builder for EditAutoModRule<'_> {
120    type Context<'ctx> = (GuildId, Option<RuleId>);
121    type Built = Rule;
122
123    /// Creates or edits an AutoMod [`Rule`] in a guild. Providing a [`RuleId`] will edit that
124    /// corresponding rule, otherwise a new rule will be created.
125    ///
126    /// **Note**: Requires the [Manage Guild] permission.
127    ///
128    /// # Errors
129    ///
130    /// Returns [`Error::Http`] if the current user lacks permission, or if invalid data is given.
131    ///
132    /// [Manage Guild]: Permissions::MANAGE_GUILD
133    async fn execute(
134        self,
135        cache_http: impl CacheHttp,
136        ctx: Self::Context<'_>,
137    ) -> Result<Self::Built> {
138        let http = cache_http.http();
139        match ctx.1 {
140            Some(id) => http.edit_automod_rule(ctx.0, id, &self, self.audit_log_reason).await,
141            // Automod Rule creation has required fields, whereas modifying a rule does not.
142            // TODO: Enforce these fields (maybe with a separate CreateAutoModRule builder).
143            None => http.create_automod_rule(ctx.0, &self, self.audit_log_reason).await,
144        }
145    }
146}