twilight_model/guild/auto_moderation/
action.rs

1use crate::id::{marker::ChannelMarker, Id};
2use serde::{Deserialize, Serialize};
3
4/// An action which will execute whenever a rule is triggered.
5#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
6pub struct AutoModerationAction {
7    /// Type of action.
8    #[serde(rename = "type")]
9    pub kind: AutoModerationActionType,
10    /// Additional metadata needed during execution for this specific action
11    /// type.
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub metadata: Option<AutoModerationActionMetadata>,
14}
15
16/// Additional metadata needed during execution for a specific
17/// [`AutoModerationActionType`].
18#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
19pub struct AutoModerationActionMetadata {
20    /// Channel to which user content should be logged.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub channel_id: Option<Id<ChannelMarker>>,
23    /// Additional explanation that will be shown to members whenever their message is blocked.
24    ///
25    /// Maximum value length is 150 characters.
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub custom_message: Option<String>,
28    /// Timeout duration in seconds.
29    ///
30    /// Maximum value is 2419200 seconds, or 4 weeks.
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub duration_seconds: Option<u32>,
33}
34
35/// Type of [`AutoModerationAction`].
36#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
37#[serde(from = "u8", into = "u8")]
38pub enum AutoModerationActionType {
39    /// Blocks the content of a message according to the rule.
40    BlockMessage,
41    /// Logs user content to a specified channel.
42    SendAlertMessage,
43    /// Timeout user for a specified duration.
44    ///
45    /// A `Timeout` action can only be setup for [`Keyword`] rules.
46    /// [`Permissions::MODERATE_MEMBERS`] is required to use the `Timeout` action
47    /// type.
48    ///
49    /// [`Keyword`]: super::AutoModerationTriggerType::Keyword
50    /// [`Permissions::MODERATE_MEMBERS`]: crate::guild::Permissions::MODERATE_MEMBERS
51    Timeout,
52    /// Prevents a member from using text, voice, or other interactions.
53    BlockMemberInteraction,
54    /// Variant value is unknown to the library.
55    Unknown(u8),
56}
57
58impl From<u8> for AutoModerationActionType {
59    fn from(value: u8) -> Self {
60        match value {
61            1 => Self::BlockMessage,
62            2 => Self::SendAlertMessage,
63            3 => Self::Timeout,
64            4 => Self::BlockMemberInteraction,
65            _ => Self::Unknown(value),
66        }
67    }
68}
69
70impl From<AutoModerationActionType> for u8 {
71    fn from(value: AutoModerationActionType) -> Self {
72        match value {
73            AutoModerationActionType::BlockMessage => 1,
74            AutoModerationActionType::SendAlertMessage => 2,
75            AutoModerationActionType::Timeout => 3,
76            AutoModerationActionType::BlockMemberInteraction => 4,
77            AutoModerationActionType::Unknown(unknown) => unknown,
78        }
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    use super::{AutoModerationAction, AutoModerationActionMetadata, AutoModerationActionType};
85    use serde::{Deserialize, Serialize};
86    use static_assertions::{assert_fields, assert_impl_all};
87    use std::{fmt::Debug, hash::Hash};
88
89    assert_fields!(AutoModerationAction: kind, metadata);
90    assert_fields!(
91        AutoModerationActionMetadata: channel_id,
92        custom_message,
93        duration_seconds
94    );
95    assert_impl_all!(
96        AutoModerationAction: Clone,
97        Debug,
98        Deserialize<'static>,
99        Eq,
100        Hash,
101        PartialEq,
102        Serialize,
103        Send,
104        Sync
105    );
106    assert_impl_all!(
107        AutoModerationActionMetadata: Clone,
108        Debug,
109        Deserialize<'static>,
110        Eq,
111        Hash,
112        PartialEq,
113        Serialize,
114        Send,
115        Sync
116    );
117    assert_impl_all!(
118        AutoModerationActionType: Clone,
119        Copy,
120        Debug,
121        Deserialize<'static>,
122        Eq,
123        Hash,
124        PartialEq,
125        Send,
126        Serialize,
127        Sync,
128    );
129
130    #[test]
131    fn values() {
132        assert_eq!(1, u8::from(AutoModerationActionType::BlockMessage));
133        assert_eq!(2, u8::from(AutoModerationActionType::SendAlertMessage));
134        assert_eq!(3, u8::from(AutoModerationActionType::Timeout));
135        assert_eq!(
136            4,
137            u8::from(AutoModerationActionType::BlockMemberInteraction)
138        );
139        assert_eq!(250, u8::from(AutoModerationActionType::Unknown(250)));
140    }
141}