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
use crate::id::{marker::ChannelMarker, Id};
use serde::{Deserialize, Serialize};

/// An action which will execute whenever a rule is triggered.
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct AutoModerationAction {
    /// Type of action.
    #[serde(rename = "type")]
    pub kind: AutoModerationActionType,
    /// Additional metadata needed during execution for this specific action
    /// type.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<AutoModerationActionMetadata>,
}

/// Additional metadata needed during execution for a specific
/// [`AutoModerationActionType`].
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct AutoModerationActionMetadata {
    /// Channel to which user content should be logged.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub channel_id: Option<Id<ChannelMarker>>,
    /// Timeout duration in seconds.
    ///
    /// Maximum value is 2419200 seconds, or 4 weeks.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub duration_seconds: Option<u32>,
}

/// Type of [`AutoModerationAction`].
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(from = "u8", into = "u8")]
pub enum AutoModerationActionType {
    /// Blocks the content of a message according to the rule.
    BlockMessage,
    /// Logs user content to a specified channel.
    SendAlertMessage,
    /// Timeout user for a specified duration.
    ///
    /// A `Timeout` action can only be setup for [`Keyword`] rules.
    /// [`Permissions::MODERATE_MEMBERS`] is required to use the `Timeout` action
    /// type.
    ///
    /// [`Keyword`]: super::AutoModerationTriggerType::Keyword
    /// [`Permissions::MODERATE_MEMBERS`]: crate::guild::Permissions::MODERATE_MEMBERS
    Timeout,
    /// Variant value is unknown to the library.
    Unknown(u8),
}

impl From<u8> for AutoModerationActionType {
    fn from(value: u8) -> Self {
        match value {
            1 => Self::BlockMessage,
            2 => Self::SendAlertMessage,
            3 => Self::Timeout,
            _ => Self::Unknown(value),
        }
    }
}

impl From<AutoModerationActionType> for u8 {
    fn from(value: AutoModerationActionType) -> Self {
        match value {
            AutoModerationActionType::BlockMessage => 1,
            AutoModerationActionType::SendAlertMessage => 2,
            AutoModerationActionType::Timeout => 3,
            AutoModerationActionType::Unknown(unknown) => unknown,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{AutoModerationAction, AutoModerationActionMetadata, AutoModerationActionType};
    use serde::{Deserialize, Serialize};
    use static_assertions::{assert_fields, assert_impl_all};
    use std::{fmt::Debug, hash::Hash};

    assert_fields!(AutoModerationAction: kind, metadata);
    assert_fields!(AutoModerationActionMetadata: channel_id, duration_seconds);
    assert_impl_all!(
        AutoModerationAction: Clone,
        Debug,
        Deserialize<'static>,
        Eq,
        Hash,
        PartialEq,
        Serialize,
        Send,
        Sync
    );
    assert_impl_all!(
        AutoModerationActionMetadata: Clone,
        Debug,
        Deserialize<'static>,
        Eq,
        Hash,
        PartialEq,
        Serialize,
        Send,
        Sync
    );
    assert_impl_all!(
        AutoModerationActionType: Clone,
        Copy,
        Debug,
        Deserialize<'static>,
        Eq,
        Hash,
        PartialEq,
        Send,
        Serialize,
        Sync,
    );

    #[test]
    fn values() {
        assert_eq!(1, u8::from(AutoModerationActionType::BlockMessage));
        assert_eq!(2, u8::from(AutoModerationActionType::SendAlertMessage));
        assert_eq!(3, u8::from(AutoModerationActionType::Timeout));
        assert_eq!(250, u8::from(AutoModerationActionType::Unknown(250)));
    }
}