twilight_model/guild/auto_moderation/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//! Types for Auto Moderation.
//!
//! Auto Moderation is a feature which allows each guild to set up rules that
//! trigger based on some criteria. For example, a rule can trigger whenever a
//! message contains a specific keyword.
//!
//! Rules can be configured to automatically execute actions whenever they
//! trigger. For example, if a user tries to send a message which contains a
//! certain keyword, a rule can trigger and block the message before it is sent.

#![warn(missing_docs)]

mod action;
mod event_type;
mod preset_type;
mod trigger_metadata;
mod trigger_type;

pub use self::{
    action::{AutoModerationAction, AutoModerationActionMetadata, AutoModerationActionType},
    event_type::AutoModerationEventType,
    preset_type::AutoModerationKeywordPresetType,
    trigger_metadata::AutoModerationTriggerMetadata,
    trigger_type::AutoModerationTriggerType,
};

use crate::id::{
    marker::{AutoModerationRuleMarker, ChannelMarker, GuildMarker, RoleMarker, UserMarker},
    Id,
};
use serde::{Deserialize, Serialize};

/// Configured auto moderation rule.
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct AutoModerationRule {
    /// Actions which will execute when the rule is triggered.
    pub actions: Vec<AutoModerationAction>,
    /// User which created the rule.
    pub creator_id: Id<UserMarker>,
    /// Whether the rule is enabled.
    pub enabled: bool,
    /// Rule event type.
    pub event_type: AutoModerationEventType,
    /// Channels that should not be affected by the rule.
    ///
    /// Maximum of 50.
    pub exempt_channels: Vec<Id<ChannelMarker>>,
    /// Roles that should not be affected by the rule.
    ///
    /// Maximum of 20.
    pub exempt_roles: Vec<Id<RoleMarker>>,
    /// ID of the guild the rule belongs to.
    pub guild_id: Id<GuildMarker>,
    /// ID of the rule.
    pub id: Id<AutoModerationRuleMarker>,
    /// Name of the rule.
    pub name: String,
    /// Rule trigger metadata.
    pub trigger_metadata: AutoModerationTriggerMetadata,
    /// Rule trigger type.
    pub trigger_type: AutoModerationTriggerType,
}

#[cfg(test)]
mod tests {
    use super::{
        AutoModerationAction, AutoModerationActionMetadata, AutoModerationActionType,
        AutoModerationEventType, AutoModerationRule, AutoModerationTriggerMetadata,
        AutoModerationTriggerType,
    };
    use crate::id::{
        marker::{AutoModerationRuleMarker, ChannelMarker, GuildMarker, RoleMarker, UserMarker},
        Id,
    };
    use serde::{Deserialize, Serialize};
    use serde_test::Token;
    use static_assertions::{assert_fields, assert_impl_all};
    use std::{fmt::Debug, hash::Hash};

    assert_fields!(
        AutoModerationRule: actions,
        creator_id,
        enabled,
        event_type,
        exempt_channels,
        exempt_roles,
        guild_id,
        id,
        name,
        trigger_metadata,
        trigger_type
    );
    assert_impl_all!(
        AutoModerationRule: Clone,
        Debug,
        Deserialize<'static>,
        Eq,
        Hash,
        PartialEq,
        Serialize,
        Send,
        Sync,
    );

    #[allow(clippy::too_many_lines)]
    #[test]
    fn rule() {
        const ACTION_CHANNEL_ID: Id<ChannelMarker> = Id::new(1);
        const AUTO_MODERATION_RULE_ID: Id<AutoModerationRuleMarker> = Id::new(2);
        const CREATOR_ID: Id<UserMarker> = Id::new(3);
        const EXEMPT_CHANNEL_ID: Id<ChannelMarker> = Id::new(4);
        const EXEMPT_ROLE_ID: Id<RoleMarker> = Id::new(5);
        const GUILD_ID: Id<GuildMarker> = Id::new(6);

        let value = AutoModerationRule {
            actions: Vec::from([
                AutoModerationAction {
                    kind: AutoModerationActionType::BlockMessage,
                    metadata: None,
                },
                AutoModerationAction {
                    kind: AutoModerationActionType::SendAlertMessage,
                    metadata: Some(AutoModerationActionMetadata {
                        channel_id: Some(ACTION_CHANNEL_ID),
                        custom_message: None,
                        duration_seconds: None,
                    }),
                },
                AutoModerationAction {
                    kind: AutoModerationActionType::Timeout,
                    metadata: Some(AutoModerationActionMetadata {
                        channel_id: None,
                        custom_message: None,
                        duration_seconds: Some(120),
                    }),
                },
            ]),
            creator_id: CREATOR_ID,
            enabled: true,
            event_type: AutoModerationEventType::MessageSend,
            exempt_channels: Vec::from([EXEMPT_CHANNEL_ID]),
            exempt_roles: Vec::from([EXEMPT_ROLE_ID]),
            guild_id: GUILD_ID,
            id: AUTO_MODERATION_RULE_ID,
            name: "rule".into(),
            trigger_metadata: AutoModerationTriggerMetadata {
                allow_list: None,
                keyword_filter: Some(Vec::from(["shoot".into(), "darn".into()])),
                presets: None,
                mention_raid_protection_enabled: Some(true),
                mention_total_limit: None,
                regex_patterns: Some(Vec::from(["[a-z]+".into(), "[0-9]+".into()])),
            },
            trigger_type: AutoModerationTriggerType::Keyword,
        };

        serde_test::assert_tokens(
            &value,
            &[
                Token::Struct {
                    name: "AutoModerationRule",
                    len: 11,
                },
                Token::Str("actions"),
                Token::Seq { len: Some(3) },
                Token::Struct {
                    name: "AutoModerationAction",
                    len: 1,
                },
                Token::Str("type"),
                Token::U8(u8::from(AutoModerationActionType::BlockMessage)),
                Token::StructEnd,
                Token::Struct {
                    name: "AutoModerationAction",
                    len: 2,
                },
                Token::Str("type"),
                Token::U8(u8::from(AutoModerationActionType::SendAlertMessage)),
                Token::Str("metadata"),
                Token::Some,
                Token::Struct {
                    name: "AutoModerationActionMetadata",
                    len: 1,
                },
                Token::Str("channel_id"),
                Token::Some,
                Token::NewtypeStruct { name: "Id" },
                Token::Str("1"),
                Token::StructEnd,
                Token::StructEnd,
                Token::Struct {
                    name: "AutoModerationAction",
                    len: 2,
                },
                Token::Str("type"),
                Token::U8(u8::from(AutoModerationActionType::Timeout)),
                Token::Str("metadata"),
                Token::Some,
                Token::Struct {
                    name: "AutoModerationActionMetadata",
                    len: 1,
                },
                Token::Str("duration_seconds"),
                Token::Some,
                Token::U32(120),
                Token::StructEnd,
                Token::StructEnd,
                Token::SeqEnd,
                Token::Str("creator_id"),
                Token::NewtypeStruct { name: "Id" },
                Token::Str("3"),
                Token::Str("enabled"),
                Token::Bool(true),
                Token::Str("event_type"),
                Token::U8(u8::from(AutoModerationEventType::MessageSend)),
                Token::Str("exempt_channels"),
                Token::Seq { len: Some(1) },
                Token::NewtypeStruct { name: "Id" },
                Token::Str("4"),
                Token::SeqEnd,
                Token::Str("exempt_roles"),
                Token::Seq { len: Some(1) },
                Token::NewtypeStruct { name: "Id" },
                Token::Str("5"),
                Token::SeqEnd,
                Token::Str("guild_id"),
                Token::NewtypeStruct { name: "Id" },
                Token::Str("6"),
                Token::Str("id"),
                Token::NewtypeStruct { name: "Id" },
                Token::Str("2"),
                Token::Str("name"),
                Token::Str("rule"),
                Token::Str("trigger_metadata"),
                Token::Struct {
                    name: "AutoModerationTriggerMetadata",
                    len: 3,
                },
                Token::Str("keyword_filter"),
                Token::Some,
                Token::Seq { len: Some(2) },
                Token::Str("shoot"),
                Token::Str("darn"),
                Token::SeqEnd,
                Token::Str("mention_raid_protection_enabled"),
                Token::Some,
                Token::Bool(true),
                Token::Str("regex_patterns"),
                Token::Some,
                Token::Seq { len: Some(2) },
                Token::Str("[a-z]+"),
                Token::Str("[0-9]+"),
                Token::SeqEnd,
                Token::StructEnd,
                Token::Str("trigger_type"),
                Token::U8(u8::from(AutoModerationTriggerType::Keyword)),
                Token::StructEnd,
            ],
        );
    }
}