djs_rs/manager/auto_moderation.rs
1//! [`AutoModerationRuleManager`].
2
3use std::future::Future;
4
5use djs_cache::Backend;
6use djs_collection::Collection;
7use djs_model::{
8 auto_moderation::Rule,
9 id::{AutoModerationRuleId, GuildId},
10 rest::{CreateAutoModerationRule, ModifyAutoModerationRule},
11};
12use djs_rest::Error;
13
14use super::Fetch;
15use crate::Context;
16
17/// The auto moderation rules of one guild. Returned by [`Context::auto_moderation_rules`].
18///
19/// The cache does not keep rules, so the [`djs-model`](djs_model) [`Rule`] is returned. Every
20/// method needs `MANAGE_GUILD`.
21///
22/// discord.js counterpart: `AutoModerationRuleManager` (`guild.autoModerationRules`).
23#[derive(Debug)]
24pub struct AutoModerationRuleManager<'a, B: Backend> {
25 ctx: &'a Context<B>,
26 guild_id: GuildId,
27}
28
29impl<B: Backend> Clone for AutoModerationRuleManager<'_, B> {
30 fn clone(&self) -> Self {
31 *self
32 }
33}
34
35impl<B: Backend> Copy for AutoModerationRuleManager<'_, B> {}
36
37impl<'a, B: Backend> AutoModerationRuleManager<'a, B> {
38 pub(crate) const fn new(ctx: &'a Context<B>, guild_id: GuildId) -> Self {
39 Self { ctx, guild_id }
40 }
41
42 /// The guild these rules belong to.
43 ///
44 /// discord.js counterpart: `AutoModerationRuleManager#guild`.
45 #[must_use]
46 pub const fn guild_id(self) -> GuildId {
47 self.guild_id
48 }
49
50 /// A rule ([Get Auto Moderation Rule](djs_rest::Rest::get_auto_moderation_rule)).
51 ///
52 /// discord.js counterpart: `AutoModerationRuleManager#fetch(id)`.
53 ///
54 /// # Errors
55 ///
56 /// Returns the REST error if the request failed.
57 pub fn fetch(
58 self,
59 rule_id: AutoModerationRuleId,
60 ) -> Fetch<Rule, impl Future<Output = Result<Rule, Error>> + Send + 'a> {
61 let Self { ctx, guild_id } = self;
62 Fetch::new(None, async move {
63 ctx.rest().get_auto_moderation_rule(guild_id, rule_id).await
64 })
65 }
66
67 /// Every rule of the guild ([List Auto Moderation Rules for
68 /// Guild](djs_rest::Rest::list_auto_moderation_rules_for_guild)).
69 ///
70 /// discord.js counterpart: `AutoModerationRuleManager#fetch()`.
71 ///
72 /// # Errors
73 ///
74 /// Returns the REST error if the request failed.
75 pub async fn list(self) -> Result<Collection<AutoModerationRuleId, Rule>, Error> {
76 let rules = self
77 .ctx
78 .rest()
79 .list_auto_moderation_rules_for_guild(self.guild_id)
80 .await?;
81 Ok(rules.into_iter().map(|rule| (rule.id, rule)).collect())
82 }
83
84 /// Creates a rule ([Create Auto Moderation
85 /// Rule](djs_rest::Rest::create_auto_moderation_rule)).
86 ///
87 /// discord.js counterpart: `AutoModerationRuleManager#create`.
88 ///
89 /// # Errors
90 ///
91 /// Returns the REST error if the request failed.
92 pub async fn create(
93 self,
94 body: &CreateAutoModerationRule,
95 reason: Option<&str>,
96 ) -> Result<Rule, Error> {
97 self.ctx
98 .rest()
99 .create_auto_moderation_rule(self.guild_id, body, reason)
100 .await
101 }
102
103 /// Edits a rule ([Modify Auto Moderation
104 /// Rule](djs_rest::Rest::modify_auto_moderation_rule)).
105 ///
106 /// discord.js counterpart: `AutoModerationRuleManager#edit`.
107 ///
108 /// # Errors
109 ///
110 /// Returns the REST error if the request failed.
111 pub async fn edit(
112 self,
113 rule_id: AutoModerationRuleId,
114 body: &ModifyAutoModerationRule,
115 reason: Option<&str>,
116 ) -> Result<Rule, Error> {
117 self.ctx
118 .rest()
119 .modify_auto_moderation_rule(self.guild_id, rule_id, body, reason)
120 .await
121 }
122
123 /// Deletes a rule ([Delete Auto Moderation
124 /// Rule](djs_rest::Rest::delete_auto_moderation_rule)).
125 ///
126 /// discord.js counterpart: `AutoModerationRuleManager#delete`.
127 ///
128 /// # Errors
129 ///
130 /// Returns the REST error if the request failed.
131 pub async fn delete(
132 self,
133 rule_id: AutoModerationRuleId,
134 reason: Option<&str>,
135 ) -> Result<(), Error> {
136 self.ctx
137 .rest()
138 .delete_auto_moderation_rule(self.guild_id, rule_id, reason)
139 .await
140 }
141}