1use crate::guild::Permissions;
2use serde::{Deserialize, Serialize};
3
4#[allow(missing_docs)]
11#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
12#[non_exhaustive]
13#[serde(from = "u8", into = "u8")]
14pub enum MessageType {
15 Regular,
17 RecipientAdd,
19 RecipientRemove,
21 Call,
23 ChannelNameChange,
25 ChannelIconChange,
27 ChannelMessagePinned,
29 UserJoin,
31 GuildBoost,
33 GuildBoostTier1,
35 GuildBoostTier2,
37 GuildBoostTier3,
39 ChannelFollowAdd,
41 GuildDiscoveryDisqualified,
43 GuildDiscoveryRequalified,
45 GuildDiscoveryGracePeriodInitialWarning,
47 GuildDiscoveryGracePeriodFinalWarning,
49 ThreadCreated,
50 Reply,
52 ChatInputCommand,
54 ThreadStarterMessage,
55 GuildInviteReminder,
56 ContextMenuCommand,
57 AutoModerationAction,
59 RoleSubscriptionPurchase,
61 InteractionPremiumUpsell,
63 StageStart,
65 StageEnd,
67 StageSpeaker,
69 StageTopic,
71 GuildApplicationPremiumSubscription,
73 GuildIncidentAlertModeEnabled,
75 GuildIncidentAlertModeDisabled,
77 GuildIncidentReportRaid,
79 GuildIncidentReportRaidFalseAlarm,
81 Unknown(u8),
83}
84
85impl MessageType {
86 pub const fn deletable(self) -> bool {
100 matches!(
101 self,
102 Self::Regular
103 | Self::ChannelMessagePinned
104 | Self::UserJoin
105 | Self::GuildBoost
106 | Self::GuildBoostTier1
107 | Self::GuildBoostTier2
108 | Self::GuildBoostTier3
109 | Self::ChannelFollowAdd
110 | Self::ThreadCreated
111 | Self::Reply
112 | Self::ChatInputCommand
113 | Self::GuildInviteReminder
114 | Self::ContextMenuCommand
115 | Self::AutoModerationAction
116 | Self::RoleSubscriptionPurchase
117 | Self::InteractionPremiumUpsell
118 | Self::StageStart
119 | Self::StageEnd
120 | Self::StageSpeaker
121 | Self::StageTopic
122 | Self::GuildApplicationPremiumSubscription
123 | Self::GuildIncidentAlertModeEnabled
124 | Self::GuildIncidentAlertModeDisabled
125 | Self::GuildIncidentReportRaid
126 | Self::GuildIncidentReportRaidFalseAlarm
127 )
128 }
129
130 pub const fn deletable_with_permissions(self, permissions: Permissions) -> bool {
142 let required_permissions = match self {
143 Self::AutoModerationAction => Permissions::MANAGE_MESSAGES,
144 _ => Permissions::empty(),
145 };
146
147 if !permissions.contains(required_permissions) {
148 return false;
149 }
150
151 self.deletable()
152 }
153}
154
155impl From<u8> for MessageType {
156 fn from(value: u8) -> Self {
157 match value {
158 0 => Self::Regular,
159 1 => Self::RecipientAdd,
160 2 => Self::RecipientRemove,
161 3 => Self::Call,
162 4 => Self::ChannelNameChange,
163 5 => Self::ChannelIconChange,
164 6 => Self::ChannelMessagePinned,
165 7 => Self::UserJoin,
166 8 => Self::GuildBoost,
167 9 => Self::GuildBoostTier1,
168 10 => Self::GuildBoostTier2,
169 11 => Self::GuildBoostTier3,
170 12 => Self::ChannelFollowAdd,
171 14 => Self::GuildDiscoveryDisqualified,
172 15 => Self::GuildDiscoveryRequalified,
173 16 => Self::GuildDiscoveryGracePeriodInitialWarning,
174 17 => Self::GuildDiscoveryGracePeriodFinalWarning,
175 18 => Self::ThreadCreated,
176 19 => Self::Reply,
177 20 => Self::ChatInputCommand,
178 21 => Self::ThreadStarterMessage,
179 22 => Self::GuildInviteReminder,
180 23 => Self::ContextMenuCommand,
181 24 => Self::AutoModerationAction,
182 25 => Self::RoleSubscriptionPurchase,
183 26 => Self::InteractionPremiumUpsell,
184 27 => Self::StageStart,
185 28 => Self::StageEnd,
186 29 => Self::StageSpeaker,
187 31 => Self::StageTopic,
188 32 => Self::GuildApplicationPremiumSubscription,
189 36 => Self::GuildIncidentAlertModeEnabled,
190 37 => Self::GuildIncidentAlertModeDisabled,
191 38 => Self::GuildIncidentReportRaid,
192 39 => Self::GuildIncidentReportRaidFalseAlarm,
193 unknown => Self::Unknown(unknown),
194 }
195 }
196}
197
198impl From<MessageType> for u8 {
199 fn from(value: MessageType) -> Self {
200 match value {
201 MessageType::Regular => 0,
202 MessageType::RecipientAdd => 1,
203 MessageType::RecipientRemove => 2,
204 MessageType::Call => 3,
205 MessageType::ChannelNameChange => 4,
206 MessageType::ChannelIconChange => 5,
207 MessageType::ChannelMessagePinned => 6,
208 MessageType::UserJoin => 7,
209 MessageType::GuildBoost => 8,
210 MessageType::GuildBoostTier1 => 9,
211 MessageType::GuildBoostTier2 => 10,
212 MessageType::GuildBoostTier3 => 11,
213 MessageType::ChannelFollowAdd => 12,
214 MessageType::GuildDiscoveryDisqualified => 14,
215 MessageType::GuildDiscoveryRequalified => 15,
216 MessageType::GuildDiscoveryGracePeriodInitialWarning => 16,
217 MessageType::GuildDiscoveryGracePeriodFinalWarning => 17,
218 MessageType::ThreadCreated => 18,
219 MessageType::Reply => 19,
220 MessageType::ChatInputCommand => 20,
221 MessageType::ThreadStarterMessage => 21,
222 MessageType::GuildInviteReminder => 22,
223 MessageType::ContextMenuCommand => 23,
224 MessageType::AutoModerationAction => 24,
225 MessageType::RoleSubscriptionPurchase => 25,
226 MessageType::InteractionPremiumUpsell => 26,
227 MessageType::StageStart => 27,
228 MessageType::StageEnd => 28,
229 MessageType::StageSpeaker => 29,
230 MessageType::StageTopic => 31,
231 MessageType::GuildApplicationPremiumSubscription => 32,
232 MessageType::GuildIncidentAlertModeEnabled => 36,
233 MessageType::GuildIncidentAlertModeDisabled => 37,
234 MessageType::GuildIncidentReportRaid => 38,
235 MessageType::GuildIncidentReportRaidFalseAlarm => 39,
236 MessageType::Unknown(unknown) => unknown,
237 }
238 }
239}
240
241#[cfg(test)]
242mod tests {
243 use super::MessageType;
244 use crate::guild::Permissions;
245 use serde::{Deserialize, Serialize};
246 use serde_test::Token;
247 use static_assertions::assert_impl_all;
248 use std::{fmt::Debug, hash::Hash};
249
250 assert_impl_all!(
251 MessageType: Clone,
252 Copy,
253 Debug,
254 Deserialize<'static>,
255 Eq,
256 Hash,
257 PartialEq,
258 Send,
259 Serialize,
260 Sync
261 );
262
263 #[test]
264 fn message_type() {
265 const MAP: &[(MessageType, u8, bool)] = &[
266 (MessageType::Regular, 0, true),
267 (MessageType::RecipientAdd, 1, false),
268 (MessageType::RecipientRemove, 2, false),
269 (MessageType::Call, 3, false),
270 (MessageType::ChannelNameChange, 4, false),
271 (MessageType::ChannelIconChange, 5, false),
272 (MessageType::ChannelMessagePinned, 6, true),
273 (MessageType::UserJoin, 7, true),
274 (MessageType::GuildBoost, 8, true),
275 (MessageType::GuildBoostTier1, 9, true),
276 (MessageType::GuildBoostTier2, 10, true),
277 (MessageType::GuildBoostTier3, 11, true),
278 (MessageType::ChannelFollowAdd, 12, true),
279 (MessageType::GuildDiscoveryDisqualified, 14, false),
280 (MessageType::GuildDiscoveryRequalified, 15, false),
281 (
282 MessageType::GuildDiscoveryGracePeriodInitialWarning,
283 16,
284 false,
285 ),
286 (
287 MessageType::GuildDiscoveryGracePeriodFinalWarning,
288 17,
289 false,
290 ),
291 (MessageType::ThreadCreated, 18, true),
292 (MessageType::Reply, 19, true),
293 (MessageType::ChatInputCommand, 20, true),
294 (MessageType::ThreadStarterMessage, 21, false),
295 (MessageType::GuildInviteReminder, 22, true),
296 (MessageType::ContextMenuCommand, 23, true),
297 (MessageType::AutoModerationAction, 24, true),
298 (MessageType::RoleSubscriptionPurchase, 25, true),
299 (MessageType::InteractionPremiumUpsell, 26, true),
300 (MessageType::StageStart, 27, true),
301 (MessageType::StageEnd, 28, true),
302 (MessageType::StageSpeaker, 29, true),
303 (MessageType::StageTopic, 31, true),
304 (MessageType::GuildApplicationPremiumSubscription, 32, true),
305 (MessageType::GuildIncidentAlertModeEnabled, 36, true),
306 (MessageType::GuildIncidentAlertModeDisabled, 37, true),
307 (MessageType::GuildIncidentReportRaid, 38, true),
308 (MessageType::GuildIncidentReportRaidFalseAlarm, 39, true),
309 ];
310
311 for (message_type, number, deletable) in MAP {
312 assert_eq!(*message_type, MessageType::from(*number));
313 assert_eq!(*number, u8::from(*message_type));
314 assert_eq!(*deletable, message_type.deletable());
315 serde_test::assert_tokens(message_type, &[Token::U8(*number)]);
316 }
317 }
318
319 #[test]
320 fn deletable_with_permissions() {
321 assert!(MessageType::AutoModerationAction
322 .deletable_with_permissions(Permissions::MANAGE_MESSAGES));
323 assert!(!MessageType::AutoModerationAction.deletable_with_permissions(Permissions::empty()));
324 }
325}