twilight_model/guild/auto_moderation/
trigger_metadata.rs

1use super::AutoModerationKeywordPresetType;
2use serde::{Deserialize, Serialize};
3
4/// Additional data used to determine whether a rule should be triggered.
5///
6/// Different fields are relevant based on the value of [`AutoModerationRule::trigger_type`].
7///
8/// [`AutoModerationRule::trigger_type`]: super::AutoModerationRule::trigger_type
9#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
10pub struct AutoModerationTriggerMetadata {
11    /// Substrings that will be exempt from triggering the preset type.
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub allow_list: Option<Vec<String>>,
14    /// Substrings which will be searched for in content.
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub keyword_filter: Option<Vec<String>>,
17    /// Internally pre-defined wordsets which will be searched for in content.
18    ///
19    /// A keyword can be a phrase which contains multiple words. Wildcard
20    /// symbols can be used to customize how each keyword will be matched. See
21    /// [Discord Docs/Keyword Matching Strategies].
22    ///
23    /// [Discord Docs/Keyword Matching Strategies]: https://discord.com/developers/docs/resources/auto-moderation#auto-moderation-rule-object-keyword-matching-strategies
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub presets: Option<Vec<AutoModerationKeywordPresetType>>,
26    /// Whether to automatically detect mention raids.
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub mention_raid_protection_enabled: Option<bool>,
29    /// Total number of unique role and user mentions allowed per message (Maximum of 50).
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub mention_total_limit: Option<u8>,
32    /// Regular expression patterns which will be matched against content (Maximum of 10).
33    /// Only Rust flavored regex is supported. Each regex pattern must be 260 characters or less.
34    #[serde(skip_serializing_if = "Option::is_none")]
35    pub regex_patterns: Option<Vec<String>>,
36}
37
38#[cfg(test)]
39mod tests {
40    use super::{AutoModerationKeywordPresetType, AutoModerationTriggerMetadata};
41    use serde::{Deserialize, Serialize};
42    use serde_test::Token;
43    use static_assertions::{assert_fields, assert_impl_all};
44    use std::{fmt::Debug, hash::Hash};
45
46    assert_fields!(AutoModerationTriggerMetadata: keyword_filter, presets);
47    assert_impl_all!(
48        AutoModerationTriggerMetadata: Clone,
49        Debug,
50        Deserialize<'static>,
51        Eq,
52        Hash,
53        PartialEq,
54        Serialize,
55        Send,
56        Sync
57    );
58
59    #[test]
60    fn trigger_metadata() {
61        let value = AutoModerationTriggerMetadata {
62            allow_list: Some(Vec::from(["heck".into()])),
63            keyword_filter: Some(Vec::from(["shoot".into(), "darn".into()])),
64            presets: Some(Vec::from([
65                AutoModerationKeywordPresetType::Profanity,
66                AutoModerationKeywordPresetType::SexualContent,
67                AutoModerationKeywordPresetType::Slurs,
68            ])),
69            mention_raid_protection_enabled: Some(true),
70            mention_total_limit: Some(5),
71            regex_patterns: Some(Vec::from(["^\\d+$".into()])),
72        };
73
74        serde_test::assert_ser_tokens(
75            &value,
76            &[
77                Token::Struct {
78                    name: "AutoModerationTriggerMetadata",
79                    len: 6,
80                },
81                Token::Str("allow_list"),
82                Token::Some,
83                Token::Seq { len: Some(1) },
84                Token::Str("heck"),
85                Token::SeqEnd,
86                Token::Str("keyword_filter"),
87                Token::Some,
88                Token::Seq { len: Some(2) },
89                Token::Str("shoot"),
90                Token::Str("darn"),
91                Token::SeqEnd,
92                Token::Str("presets"),
93                Token::Some,
94                Token::Seq { len: Some(3) },
95                Token::U8(u8::from(AutoModerationKeywordPresetType::Profanity)),
96                Token::U8(u8::from(AutoModerationKeywordPresetType::SexualContent)),
97                Token::U8(u8::from(AutoModerationKeywordPresetType::Slurs)),
98                Token::SeqEnd,
99                Token::Str("mention_raid_protection_enabled"),
100                Token::Some,
101                Token::Bool(true),
102                Token::Str("mention_total_limit"),
103                Token::Some,
104                Token::U8(5),
105                Token::Str("regex_patterns"),
106                Token::Some,
107                Token::Seq { len: Some(1) },
108                Token::Str("^\\d+$"),
109                Token::SeqEnd,
110                Token::StructEnd,
111            ],
112        );
113    }
114}