Make enum into bitflags struct

This commit is contained in:
bitfl0wer 2023-06-07 10:39:05 +02:00
parent e65aa964f0
commit cf74d0e618
1 changed files with 48 additions and 72 deletions

View File

@ -1,3 +1,4 @@
use bitflags::bitflags;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_aux::prelude::{deserialize_option_number_from_string, deserialize_string_from_number}; use serde_aux::prelude::{deserialize_option_number_from_string, deserialize_string_from_number};
@ -51,77 +52,52 @@ pub struct RoleTags {
// guild_connections: bool, // guild_connections: bool,
} }
#[derive(Debug)] bitflags! {
#[repr(u64)] pub struct PermissionFlags: u64 {
pub enum PermissionFlags { const CREATE_INSTANT_INVITE = 1 << 0;
CreateInstantInvite = 0x0000000000000001, const KICK_MEMBERS = 1 << 1;
KickMembers = 0x0000000000000002, const BAN_MEMBERS = 1 << 2;
BanMembers = 0x0000000000000004, const ADMINISTRATOR = 1 << 3;
Administrator = 0x0000000000000008, const MANAGE_CHANNELS = 1 << 4;
ManageChannels = 0x0000000000000010, const MANAGE_GUILD = 1 << 5;
ManageGuild = 0x0000000000000020, const ADD_REACTIONS = 1 << 6;
AddReactions = 0x0000000000000040, const VIEW_AUDIT_LOG = 1 << 7;
ViewAuditLog = 0x0000000000000080, const PRIORITY_SPEAKER = 1 << 8;
PrioritySpeaker = 0x0000000000000100, const STREAM = 1 << 9;
Stream = 0x0000000000000200, const VIEW_CHANNEL = 1 << 10;
ViewChannel = 0x0000000000000400, const SEND_MESSAGES = 1 << 11;
SendMessages = 0x0000000000000800, const SEND_TTS_MESSAGES = 1 << 12;
SendTtsMessages = 0x0000000000001000, const MANAGE_MESSAGES = 1 << 13;
ManageMessages = 0x0000000000002000, const EMBED_LINKS = 1 << 14;
EmbedLinks = 0x0000000000004000, const ATTACH_FILES = 1 << 15;
AttachFiles = 0x0000000000008000, const READ_MESSAGE_HISTORY = 1 << 16;
ReadMessageHistory = 0x0000000000010000, const MENTION_EVERYONE = 1 << 17;
MentionEveryone = 0x0000000000020000, const USE_EXTERNAL_EMOJIS = 1 << 18;
UseExternalEmojis = 0x0000000000040000, const VIEW_GUILD_INSIGHTS = 1 << 19;
ViewGuildInsights = 0x0000000000080000, const CONNECT = 1 << 20;
Connect = 0x0000000000100000, const SPEAK = 1 << 21;
Speak = 0x0000000000200000, const MUTE_MEMBERS = 1 << 22;
MuteMembers = 0x0000000000400000, const DEAFEN_MEMBERS = 1 << 23;
DeafenMembers = 0x0000000000800000, const MOVE_MEMBERS = 1 << 24;
MoveMembers = 0x0000000001000000, const USE_VAD = 1 << 25;
UseVad = 0x0000000002000000, const CHANGE_NICKNAME = 1 << 26;
ChangeNickname = 0x0000000004000000, const MANAGE_NICKNAMES = 1 << 27;
ManageNicknames = 0x0000000008000000, const MANAGE_ROLES = 1 << 28;
ManageRoles = 0x0000000010000000, const MANAGE_WEBHOOKS = 1 << 29;
ManageWebhooks = 0x0000000020000000, const MANAGE_GUILD_EXPRESSIONS = 1 << 30;
ManageGuildExpressions = 0x0000000040000000, const USE_APPLICATION_COMMANDS = 1 << 31;
UseApplicationCommands = 0x0000000080000000, const REQUEST_TO_SPEAK = 1 << 32;
RequestToSpeak = 0x0000000100000000, const MANAGE_EVENTS = 1 << 33;
ManageEvents = 0x0000000200000000, const MANAGE_THREADS = 1 << 34;
ManageThreads = 0x0000000400000000, const CREATE_PUBLIC_THREADS = 1 << 35;
CreatePublicThreads = 0x0000000800000000, const CREATE_PRIVATE_THREADS = 1 << 36;
CreatePrivateThreads = 0x0000001000000000, const USE_EXTERNAL_STICKERS = 1 << 37;
UseExternalStickers = 0x0000002000000000, const SEND_MESSAGES_IN_THREADS = 1 << 38;
SendMessagesInThreads = 0x0000004000000000, const USE_EMBEDDED_ACTIVITIES = 1 << 39;
UseEmbeddedActivities = 0x0000008000000000, const MODERATE_MEMBERS = 1 << 40;
ModerateMembers = 0x0000010000000000, const VIEW_CREATOR_MONETIZATION_ANALYTICS = 1 << 41;
ViewCreatorMonetizationAnalytics = 0x0000020000000000, const USE_SOUNDBOARD = 1 << 42;
UseSoundboard = 0x0000040000000000, const USE_EXTERNAL_SOUNDS = 1 << 45;
UseExternalSounds = 0x0000200000000000, const SEND_VOICE_MESSAGES = 1 << 46;
SendVoiceMessages = 0x0000400000000000,
}
impl RoleObject {
/// Checks if the role has a specific permission.
///
/// # Arguments
///
/// * `permission` - The permission to check for.
///
/// # Example
///
/// ```
/// use chorus::types;
/// let mut role = types::RoleObject::default();
/// let permission = types::PermissionFlags::ModerateMembers as u64 | types::PermissionFlags::UseSoundboard as u64;
/// role.permissions = permission.to_string();
/// assert_eq!(true, role.has_permission(types::PermissionFlags::ModerateMembers));
/// assert_eq!(true, role.has_permission(types::PermissionFlags::UseSoundboard));
/// ```
pub fn has_permission(&self, permission: PermissionFlags) -> bool {
if self.permissions.parse::<u64>().unwrap() & permission as u64 != 0 {
return true;
}
false
} }
} }