Skip to main content

twilight_http/request/guild/auto_moderation/
update_auto_moderation_rule.rs

1use crate::{
2    client::Client,
3    error::Error,
4    request::{self, AuditLogReason, Request, TryIntoRequest},
5    response::{Response, ResponseFuture},
6    routing::Route,
7};
8use serde::Serialize;
9use std::future::IntoFuture;
10use twilight_model::{
11    guild::auto_moderation::{
12        AutoModerationAction, AutoModerationEventType, AutoModerationRule,
13        AutoModerationTriggerMetadata,
14    },
15    id::{
16        Id,
17        marker::{AutoModerationRuleMarker, ChannelMarker, GuildMarker, RoleMarker},
18    },
19};
20use twilight_validate::request::{ValidationError, audit_reason as validate_audit_reason};
21
22#[derive(Serialize)]
23struct UpdateAutoModerationRuleFields<'a> {
24    #[serde(skip_serializing_if = "Option::is_none")]
25    actions: Option<&'a [AutoModerationAction]>,
26    #[serde(skip_serializing_if = "Option::is_none")]
27    enabled: Option<bool>,
28    #[serde(skip_serializing_if = "Option::is_none")]
29    event_type: Option<AutoModerationEventType>,
30    #[serde(skip_serializing_if = "Option::is_none")]
31    exempt_channels: Option<&'a [Id<ChannelMarker>]>,
32    #[serde(skip_serializing_if = "Option::is_none")]
33    exempt_roles: Option<&'a [Id<RoleMarker>]>,
34    #[serde(skip_serializing_if = "Option::is_none")]
35    name: Option<&'a str>,
36    #[serde(skip_serializing_if = "Option::is_none")]
37    trigger_metadata: Option<&'a AutoModerationTriggerMetadata>,
38}
39
40/// Update an auto moderation rule in a guild.
41///
42/// Requires the [`MANAGE_GUILD`] permission.
43///
44/// [`MANAGE_GUILD`]: twilight_model::guild::Permissions::MANAGE_GUILD
45#[must_use = "requests must be configured and executed"]
46pub struct UpdateAutoModerationRule<'a> {
47    auto_moderation_rule_id: Id<AutoModerationRuleMarker>,
48    fields: UpdateAutoModerationRuleFields<'a>,
49    guild_id: Id<GuildMarker>,
50    http: &'a Client,
51    reason: Result<Option<&'a str>, ValidationError>,
52}
53
54impl<'a> UpdateAutoModerationRule<'a> {
55    pub(crate) const fn new(
56        http: &'a Client,
57        guild_id: Id<GuildMarker>,
58        auto_moderation_rule_id: Id<AutoModerationRuleMarker>,
59    ) -> Self {
60        Self {
61            auto_moderation_rule_id,
62            fields: UpdateAutoModerationRuleFields {
63                actions: None,
64                enabled: None,
65                event_type: None,
66                exempt_channels: None,
67                exempt_roles: None,
68                name: None,
69                trigger_metadata: None,
70            },
71            guild_id,
72            http,
73            reason: Ok(None),
74        }
75    }
76
77    /// Set the list of actions.
78    pub const fn actions(mut self, actions: &'a [AutoModerationAction]) -> Self {
79        self.fields.actions = Some(actions);
80
81        self
82    }
83
84    /// Set whether the rule is enabled.
85    pub const fn enabled(mut self, enabled: bool) -> Self {
86        self.fields.enabled = Some(enabled);
87
88        self
89    }
90
91    /// Set the rule's event type.
92    pub const fn event_type(mut self, event_type: AutoModerationEventType) -> Self {
93        self.fields.event_type = Some(event_type);
94
95        self
96    }
97
98    /// Set the channels where the rule does not apply.
99    pub const fn exempt_channels(mut self, exempt_channels: &'a [Id<ChannelMarker>]) -> Self {
100        self.fields.exempt_channels = Some(exempt_channels);
101
102        self
103    }
104
105    /// Set the roles to which the rule does not apply.
106    pub const fn exempt_roles(mut self, exempt_roles: &'a [Id<RoleMarker>]) -> Self {
107        self.fields.exempt_roles = Some(exempt_roles);
108
109        self
110    }
111
112    /// Set the rule's name.
113    pub const fn name(mut self, name: &'a str) -> Self {
114        self.fields.name = Some(name);
115
116        self
117    }
118
119    /// Set the trigger metadata.
120    ///
121    /// Care must be taken to set the correct metadata based on the rule's type.
122    pub const fn trigger_metadata(
123        mut self,
124        trigger_metadata: &'a AutoModerationTriggerMetadata,
125    ) -> Self {
126        self.fields.trigger_metadata = Some(trigger_metadata);
127
128        self
129    }
130}
131
132impl<'a> AuditLogReason<'a> for UpdateAutoModerationRule<'a> {
133    fn reason(mut self, reason: &'a str) -> Self {
134        self.reason = validate_audit_reason(reason).and(Ok(Some(reason)));
135
136        self
137    }
138}
139
140impl IntoFuture for UpdateAutoModerationRule<'_> {
141    type Output = Result<Response<AutoModerationRule>, Error>;
142
143    type IntoFuture = ResponseFuture<AutoModerationRule>;
144
145    fn into_future(self) -> Self::IntoFuture {
146        let http = self.http;
147
148        match self.try_into_request() {
149            Ok(request) => http.request(request),
150            Err(source) => ResponseFuture::error(source),
151        }
152    }
153}
154
155impl TryIntoRequest for UpdateAutoModerationRule<'_> {
156    fn try_into_request(self) -> Result<Request, Error> {
157        let mut request = Request::builder(&Route::UpdateAutoModerationRule {
158            auto_moderation_rule_id: self.auto_moderation_rule_id.get(),
159            guild_id: self.guild_id.get(),
160        })
161        .json(&self.fields);
162
163        if let Some(reason) = self.reason.map_err(Error::validation)? {
164            request = request.headers(request::audit_header(reason)?);
165        }
166
167        request.build()
168    }
169}