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