Skip to main content

djs_rest/endpoints/
auto_moderation.rs

1//! [Auto Moderation endpoints](https://docs.discord.com/developers/resources/auto-moderation):
2//! rules that block, log or time out content automatically. Every endpoint needs
3//! `MANAGE_GUILD`.
4
5use djs_model::{
6    auto_moderation::Rule,
7    id::{AutoModerationRuleId, GuildId},
8    rest::{CreateAutoModerationRule, ModifyAutoModerationRule},
9};
10
11use crate::client::boxed;
12use crate::{Error, Request, Rest, Route};
13
14impl Rest {
15    /// [List Auto Moderation Rules for Guild](https://docs.discord.com/developers/resources/auto-moderation#list-auto-moderation-rules-for-guild):
16    /// returns every rule of the guild.
17    ///
18    /// discord.js counterpart: `AutoModerationRuleManager#fetch()`.
19    ///
20    /// # Errors
21    ///
22    /// See [`Rest::request`].
23    pub fn list_auto_moderation_rules_for_guild(
24        &self,
25        guild_id: GuildId,
26    ) -> impl Future<Output = Result<Vec<Rule>, Error>> + Send + '_ {
27        boxed(async move {
28            self.call(Request::new(Route::ListAutoModerationRulesForGuild {
29                guild_id,
30            }))
31            .await
32        })
33    }
34
35    /// [Get Auto Moderation Rule](https://docs.discord.com/developers/resources/auto-moderation#get-auto-moderation-rule):
36    /// returns one rule.
37    ///
38    /// discord.js counterpart: `AutoModerationRuleManager#fetch(id)`.
39    ///
40    /// # Errors
41    ///
42    /// See [`Rest::request`].
43    pub fn get_auto_moderation_rule(
44        &self,
45        guild_id: GuildId,
46        rule_id: AutoModerationRuleId,
47    ) -> impl Future<Output = Result<Rule, Error>> + Send + '_ {
48        boxed(async move {
49            self.call(Request::new(Route::GetAutoModerationRule {
50                guild_id,
51                rule_id,
52            }))
53            .await
54        })
55    }
56
57    /// [Create Auto Moderation Rule](https://docs.discord.com/developers/resources/auto-moderation#create-auto-moderation-rule):
58    /// adds a rule. A `Timeout` action also needs `MODERATE_MEMBERS`.
59    ///
60    /// `reason` is shown in the audit log (D-086).
61    ///
62    /// discord.js counterpart: `AutoModerationRuleManager#create`.
63    ///
64    /// # Examples
65    ///
66    /// ```no_run
67    /// # async fn example(rest: djs_rest::Rest, guild_id: djs_model::id::GuildId, log: djs_model::id::ChannelId) -> Result<(), djs_rest::Error> {
68    /// use djs_model::{auto_moderation::Action, rest::CreateAutoModerationRule};
69    ///
70    /// let mut body = CreateAutoModerationRule::mention_spam(
71    ///     "Mention spam",
72    ///     10,
73    ///     [Action::block_message(), Action::send_alert_message(log)],
74    /// );
75    /// body.enabled = Some(true);
76    /// let rule = rest
77    ///     .create_auto_moderation_rule(guild_id, &body, Some("Raid protection"))
78    ///     .await?;
79    /// println!("Created rule {}", rule.id);
80    /// # Ok(())
81    /// # }
82    /// ```
83    ///
84    /// # Errors
85    ///
86    /// See [`Rest::request`]; `400` when the guild already has the most rules of that trigger
87    /// type, or for metadata or actions the trigger type does not accept.
88    pub fn create_auto_moderation_rule<'a>(
89        &'a self,
90        guild_id: GuildId,
91        body: &'a CreateAutoModerationRule,
92        reason: Option<&'a str>,
93    ) -> impl Future<Output = Result<Rule, Error>> + Send + 'a {
94        boxed(async move {
95            self.call(
96                Request::new(Route::CreateAutoModerationRule { guild_id })
97                    .json(body)?
98                    .reason(reason),
99            )
100            .await
101        })
102    }
103
104    /// [Modify Auto Moderation Rule](https://docs.discord.com/developers/resources/auto-moderation#modify-auto-moderation-rule):
105    /// changes a rule and returns it.
106    ///
107    /// `reason` is shown in the audit log (D-086).
108    ///
109    /// discord.js counterpart: `AutoModerationRule#edit` and its setters,
110    /// `AutoModerationRuleManager#edit`.
111    ///
112    /// # Examples
113    ///
114    /// ```no_run
115    /// # async fn example(rest: djs_rest::Rest, guild_id: djs_model::id::GuildId, rule_id: djs_model::id::AutoModerationRuleId) -> Result<(), djs_rest::Error> {
116    /// use djs_model::rest::ModifyAutoModerationRule;
117    ///
118    /// let mut body = ModifyAutoModerationRule::default();
119    /// body.enabled = Some(false);
120    /// rest.modify_auto_moderation_rule(guild_id, rule_id, &body, None)
121    ///     .await?;
122    /// # Ok(())
123    /// # }
124    /// ```
125    ///
126    /// # Errors
127    ///
128    /// See [`Rest::request`].
129    pub fn modify_auto_moderation_rule<'a>(
130        &'a self,
131        guild_id: GuildId,
132        rule_id: AutoModerationRuleId,
133        body: &'a ModifyAutoModerationRule,
134        reason: Option<&'a str>,
135    ) -> impl Future<Output = Result<Rule, Error>> + Send + 'a {
136        boxed(async move {
137            self.call(
138                Request::new(Route::ModifyAutoModerationRule { guild_id, rule_id })
139                    .json(body)?
140                    .reason(reason),
141            )
142            .await
143        })
144    }
145
146    /// [Delete Auto Moderation Rule](https://docs.discord.com/developers/resources/auto-moderation#delete-auto-moderation-rule):
147    /// deletes a rule.
148    ///
149    /// `reason` is shown in the audit log (D-086).
150    ///
151    /// discord.js counterpart: `AutoModerationRule#delete`,
152    /// `AutoModerationRuleManager#delete`.
153    ///
154    /// # Errors
155    ///
156    /// See [`Rest::request`].
157    pub fn delete_auto_moderation_rule<'a>(
158        &'a self,
159        guild_id: GuildId,
160        rule_id: AutoModerationRuleId,
161        reason: Option<&'a str>,
162    ) -> impl Future<Output = Result<(), Error>> + Send + 'a {
163        boxed(async move {
164            self.call(
165                Request::new(Route::DeleteAutoModerationRule { guild_id, rule_id }).reason(reason),
166            )
167            .await
168        })
169    }
170}