use crate::guild::Permissions;
use serde::{Deserialize, Serialize};
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(from = "u8", into = "u8")]
pub enum MessageType {
Regular,
RecipientAdd,
RecipientRemove,
Call,
ChannelNameChange,
ChannelIconChange,
ChannelMessagePinned,
UserJoin,
GuildBoost,
GuildBoostTier1,
GuildBoostTier2,
GuildBoostTier3,
ChannelFollowAdd,
GuildDiscoveryDisqualified,
GuildDiscoveryRequalified,
GuildDiscoveryGracePeriodInitialWarning,
GuildDiscoveryGracePeriodFinalWarning,
ThreadCreated,
Reply,
ChatInputCommand,
ThreadStarterMessage,
GuildInviteReminder,
ContextMenuCommand,
AutoModerationAction,
RoleSubscriptionPurchase,
InteractionPremiumUpsell,
StageStart,
StageEnd,
StageSpeaker,
StageTopic,
GuildApplicationPremiumSubscription,
GuildIncidentAlertModeEnabled,
GuildIncidentAlertModeDisabled,
GuildIncidentReportRaid,
GuildIncidentReportRaidFalseAlarm,
Unknown(u8),
}
impl MessageType {
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
)
}
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()));
}
}