twilight_model/channel/message/
kind.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use crate::guild::Permissions;
use serde::{Deserialize, Serialize};

/// Type of a [`Message`].
///
/// Refer to [Discord Docs/Message Types] for more information.
///
/// [Discord Docs/Message Types]: https://discord.com/developers/docs/resources/channel#message-object-message-types
/// [`Message`]: super::Message
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(from = "u8", into = "u8")]
pub enum MessageType {
    /// Regular message.
    Regular,
    /// System message denoting a recipient has been added to a group.
    RecipientAdd,
    /// System message denoting a recipient has been removed from a group.
    RecipientRemove,
    /// System message denoting a call state, e.g. missed, started.
    Call,
    /// System message denoting a channel's name has been changed.
    ChannelNameChange,
    /// System message denoting a channel's icon has been changed.
    ChannelIconChange,
    /// System message denoting a message has been pinned.
    ChannelMessagePinned,
    /// System message denoting a member has joined a guild.
    UserJoin,
    /// System message denoting a user nitro boosted a guild.
    GuildBoost,
    /// System message denoting a user nitro boosted a guild to level 1.
    GuildBoostTier1,
    /// System message denoting a user nitro boosted a guild to level 2.
    GuildBoostTier2,
    /// System message denoting a user nitro boosted a guild to level 3.
    GuildBoostTier3,
    /// System message denoting a channel has been followed.
    ChannelFollowAdd,
    /// System message denoting a guild has been disqualified for Server Discovery.
    GuildDiscoveryDisqualified,
    /// System message denoting a guild has been redisqualified for Server Discovery.
    GuildDiscoveryRequalified,
    /// System message denoting an initial warning for Server Discovery disqualification.
    GuildDiscoveryGracePeriodInitialWarning,
    /// System message denoting a final warning for Server Discovery disqualification.
    GuildDiscoveryGracePeriodFinalWarning,
    ThreadCreated,
    /// Message is an inline reply.
    Reply,
    /// Message is a chat input command.
    ChatInputCommand,
    ThreadStarterMessage,
    GuildInviteReminder,
    ContextMenuCommand,
    /// Message is an auto moderation action.
    AutoModerationAction,
    /// System message denoting a user subscribed to a role.
    RoleSubscriptionPurchase,
    /// System message denoting a interaction premium upsell.
    InteractionPremiumUpsell,
    /// System message denoting a stage instance has started.
    StageStart,
    /// System message denoting a stage instance has ended.
    StageEnd,
    /// System message for stage instance speakers.
    StageSpeaker,
    /// System message denoting the topic for a stage instance.
    StageTopic,
    /// System message denoting a guild application premium subscription.
    GuildApplicationPremiumSubscription,
    /// System message denoting a guild auto moderation incident alerts are enabled.
    GuildIncidentAlertModeEnabled,
    /// System message denoting a guild auto moderation incident alerts are disabled.
    GuildIncidentAlertModeDisabled,
    /// System message denoting a guild raid incident report.
    GuildIncidentReportRaid,
    /// System message denoting a false positive guild raid incident report.
    GuildIncidentReportRaidFalseAlarm,
    /// Variant value is unknown to the library.
    Unknown(u8),
}

impl MessageType {
    /// Whether the message can be deleted, not taking permissions into account.
    /// Some message types can't be deleted, even by server administrators.
    ///
    /// Some message types can only be deleted with certain permissions. For
    /// example, [`AutoModerationAction`][`Self::AutoModerationAction`] can only
    /// be deleted if the user has the
    /// [Manage Messages] permission.
    ///
    /// To check whether a message can be deleted while taking permissions into
    /// account, use
    /// [`deletable_with_permissions`][`Self::deletable_with_permissions`].
    ///
    /// [Manage Messages]: Permissions::MANAGE_MESSAGES
    pub const fn deletable(self) -> bool {
        matches!(
            self,
            Self::Regular
                | Self::ChannelMessagePinned
                | Self::UserJoin
                | Self::GuildBoost
                | Self::GuildBoostTier1
                | Self::GuildBoostTier2
                | Self::GuildBoostTier3
                | Self::ChannelFollowAdd
                | Self::ThreadCreated
                | Self::Reply
                | Self::ChatInputCommand
                | Self::GuildInviteReminder
                | Self::ContextMenuCommand
                | Self::AutoModerationAction
                | Self::RoleSubscriptionPurchase
                | Self::InteractionPremiumUpsell
                | Self::StageStart
                | Self::StageEnd
                | Self::StageSpeaker
                | Self::StageTopic
                | Self::GuildApplicationPremiumSubscription
                | Self::GuildIncidentAlertModeEnabled
                | Self::GuildIncidentAlertModeDisabled
                | Self::GuildIncidentReportRaid
                | Self::GuildIncidentReportRaidFalseAlarm
        )
    }

    /// Whether the message can be deleted, taking permissions into account.
    /// Some message types can't be deleted, even by server administrators.
    ///
    /// Some message types can only be deleted with certain permissions. For
    /// example, [`AutoModerationAction`][`Self::AutoModerationAction`] can only
    /// be deleted if the user has the [Manage Messages] permission.
    ///
    /// To check whether a message can be deleted *without* taking permissions
    /// into account, use [`deletable`][`Self::deletable`].
    ///
    /// [Manage Messages]: Permissions::MANAGE_MESSAGES
    pub const fn deletable_with_permissions(self, permissions: Permissions) -> bool {
        let required_permissions = match self {
            Self::AutoModerationAction => Permissions::MANAGE_MESSAGES,
            _ => Permissions::empty(),
        };

        if !permissions.contains(required_permissions) {
            return false;
        }

        self.deletable()
    }
}

impl From<u8> for MessageType {
    fn from(value: u8) -> Self {
        match value {
            0 => Self::Regular,
            1 => Self::RecipientAdd,
            2 => Self::RecipientRemove,
            3 => Self::Call,
            4 => Self::ChannelNameChange,
            5 => Self::ChannelIconChange,
            6 => Self::ChannelMessagePinned,
            7 => Self::UserJoin,
            8 => Self::GuildBoost,
            9 => Self::GuildBoostTier1,
            10 => Self::GuildBoostTier2,
            11 => Self::GuildBoostTier3,
            12 => Self::ChannelFollowAdd,
            14 => Self::GuildDiscoveryDisqualified,
            15 => Self::GuildDiscoveryRequalified,
            16 => Self::GuildDiscoveryGracePeriodInitialWarning,
            17 => Self::GuildDiscoveryGracePeriodFinalWarning,
            18 => Self::ThreadCreated,
            19 => Self::Reply,
            20 => Self::ChatInputCommand,
            21 => Self::ThreadStarterMessage,
            22 => Self::GuildInviteReminder,
            23 => Self::ContextMenuCommand,
            24 => Self::AutoModerationAction,
            25 => Self::RoleSubscriptionPurchase,
            26 => Self::InteractionPremiumUpsell,
            27 => Self::StageStart,
            28 => Self::StageEnd,
            29 => Self::StageSpeaker,
            31 => Self::StageTopic,
            32 => Self::GuildApplicationPremiumSubscription,
            36 => Self::GuildIncidentAlertModeEnabled,
            37 => Self::GuildIncidentAlertModeDisabled,
            38 => Self::GuildIncidentReportRaid,
            39 => Self::GuildIncidentReportRaidFalseAlarm,
            unknown => Self::Unknown(unknown),
        }
    }
}

impl From<MessageType> for u8 {
    fn from(value: MessageType) -> Self {
        match value {
            MessageType::Regular => 0,
            MessageType::RecipientAdd => 1,
            MessageType::RecipientRemove => 2,
            MessageType::Call => 3,
            MessageType::ChannelNameChange => 4,
            MessageType::ChannelIconChange => 5,
            MessageType::ChannelMessagePinned => 6,
            MessageType::UserJoin => 7,
            MessageType::GuildBoost => 8,
            MessageType::GuildBoostTier1 => 9,
            MessageType::GuildBoostTier2 => 10,
            MessageType::GuildBoostTier3 => 11,
            MessageType::ChannelFollowAdd => 12,
            MessageType::GuildDiscoveryDisqualified => 14,
            MessageType::GuildDiscoveryRequalified => 15,
            MessageType::GuildDiscoveryGracePeriodInitialWarning => 16,
            MessageType::GuildDiscoveryGracePeriodFinalWarning => 17,
            MessageType::ThreadCreated => 18,
            MessageType::Reply => 19,
            MessageType::ChatInputCommand => 20,
            MessageType::ThreadStarterMessage => 21,
            MessageType::GuildInviteReminder => 22,
            MessageType::ContextMenuCommand => 23,
            MessageType::AutoModerationAction => 24,
            MessageType::RoleSubscriptionPurchase => 25,
            MessageType::InteractionPremiumUpsell => 26,
            MessageType::StageStart => 27,
            MessageType::StageEnd => 28,
            MessageType::StageSpeaker => 29,
            MessageType::StageTopic => 31,
            MessageType::GuildApplicationPremiumSubscription => 32,
            MessageType::GuildIncidentAlertModeEnabled => 36,
            MessageType::GuildIncidentAlertModeDisabled => 37,
            MessageType::GuildIncidentReportRaid => 38,
            MessageType::GuildIncidentReportRaidFalseAlarm => 39,
            MessageType::Unknown(unknown) => unknown,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::MessageType;
    use crate::guild::Permissions;
    use serde::{Deserialize, Serialize};
    use serde_test::Token;
    use static_assertions::assert_impl_all;
    use std::{fmt::Debug, hash::Hash};

    assert_impl_all!(
        MessageType: Clone,
        Copy,
        Debug,
        Deserialize<'static>,
        Eq,
        Hash,
        PartialEq,
        Send,
        Serialize,
        Sync
    );

    #[test]
    fn message_type() {
        const MAP: &[(MessageType, u8, bool)] = &[
            (MessageType::Regular, 0, true),
            (MessageType::RecipientAdd, 1, false),
            (MessageType::RecipientRemove, 2, false),
            (MessageType::Call, 3, false),
            (MessageType::ChannelNameChange, 4, false),
            (MessageType::ChannelIconChange, 5, false),
            (MessageType::ChannelMessagePinned, 6, true),
            (MessageType::UserJoin, 7, true),
            (MessageType::GuildBoost, 8, true),
            (MessageType::GuildBoostTier1, 9, true),
            (MessageType::GuildBoostTier2, 10, true),
            (MessageType::GuildBoostTier3, 11, true),
            (MessageType::ChannelFollowAdd, 12, true),
            (MessageType::GuildDiscoveryDisqualified, 14, false),
            (MessageType::GuildDiscoveryRequalified, 15, false),
            (
                MessageType::GuildDiscoveryGracePeriodInitialWarning,
                16,
                false,
            ),
            (
                MessageType::GuildDiscoveryGracePeriodFinalWarning,
                17,
                false,
            ),
            (MessageType::ThreadCreated, 18, true),
            (MessageType::Reply, 19, true),
            (MessageType::ChatInputCommand, 20, true),
            (MessageType::ThreadStarterMessage, 21, false),
            (MessageType::GuildInviteReminder, 22, true),
            (MessageType::ContextMenuCommand, 23, true),
            (MessageType::AutoModerationAction, 24, true),
            (MessageType::RoleSubscriptionPurchase, 25, true),
            (MessageType::InteractionPremiumUpsell, 26, true),
            (MessageType::StageStart, 27, true),
            (MessageType::StageEnd, 28, true),
            (MessageType::StageSpeaker, 29, true),
            (MessageType::StageTopic, 31, true),
            (MessageType::GuildApplicationPremiumSubscription, 32, true),
            (MessageType::GuildIncidentAlertModeEnabled, 36, true),
            (MessageType::GuildIncidentAlertModeDisabled, 37, true),
            (MessageType::GuildIncidentReportRaid, 38, true),
            (MessageType::GuildIncidentReportRaidFalseAlarm, 39, true),
        ];

        for (message_type, number, deletable) in MAP {
            assert_eq!(*message_type, MessageType::from(*number));
            assert_eq!(*number, u8::from(*message_type));
            assert_eq!(*deletable, message_type.deletable());
            serde_test::assert_tokens(message_type, &[Token::U8(*number)]);
        }
    }

    #[test]
    fn deletable_with_permissions() {
        assert!(MessageType::AutoModerationAction
            .deletable_with_permissions(Permissions::MANAGE_MESSAGES));
        assert!(!MessageType::AutoModerationAction.deletable_with_permissions(Permissions::empty()));
    }
}