serenity/builder/
edit_automod_rule.rs1#[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#[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 pub fn new() -> Self {
41 Self::default()
42 }
43
44 pub fn name(mut self, name: impl Into<String>) -> Self {
46 self.name = Some(name.into());
47 self
48 }
49
50 pub fn event_type(mut self, event_type: EventType) -> Self {
52 self.event_type = event_type;
53 self
54 }
55
56 pub fn trigger(mut self, trigger: Trigger) -> Self {
60 self.trigger = Some(trigger);
61 self
62 }
63
64 pub fn actions(mut self, actions: Vec<Action>) -> Self {
66 self.actions = Some(actions);
67 self
68 }
69
70 pub fn enabled(mut self, enabled: bool) -> Self {
72 self.enabled = Some(enabled);
73 self
74 }
75
76 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 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 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 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 None => http.create_automod_rule(ctx.0, &self, self.audit_log_reason).await,
144 }
145 }
146}