From a51fb34e2fbd2034c6117d563266f19097748145 Mon Sep 17 00:00:00 2001 From: 1Kill2Steal <171241044+1Git2Clone@users.noreply.github.com> Date: Sun, 15 Dec 2024 23:14:25 +0200 Subject: [PATCH] Added discord emoji handling This will be useful if I decide to add and use custom emojis to the bot --- src/enums/emojis.rs | 63 ++++++++++++++++++++++++++++++++++++ src/enums/mod.rs | 1 + src/event_handler/handler.rs | 3 +- 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/enums/emojis.rs diff --git a/src/enums/emojis.rs b/src/enums/emojis.rs new file mode 100644 index 0000000..d138759 --- /dev/null +++ b/src/enums/emojis.rs @@ -0,0 +1,63 @@ +/// Discord emojis are sent like this: +/// +/// `` +/// +/// This macro aims to simplify the process by writing the display implementor like this: +/// +/// ```rust +/// use serenity_discord_bot::display_emoji_impl; +/// +/// pub enum Emojis { +/// EmojiOne, +/// EmojiTwo, +/// } +/// +/// display_emoji_impl! { +/// Emojis { +/// EmojiOne => "123456789", +/// EmojiTwo => "987654321", +/// } +/// } +/// +/// assert_eq!(Emojis::EmojiOne.to_string(), "<:EmojiOne:123456789>".to_string()); +/// assert_eq!(Emojis::EmojiTwo.to_string(), "<:EmojiTwo:987654321>".to_string()); +/// ``` +#[macro_export] +macro_rules! display_emoji_impl { + ($enum_name:ident { $($variant:ident => $id:expr),* $(,)? }) => { + impl std::fmt::Display for $enum_name { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + $( + Self::$variant => { + write!( + f, + concat!( + "<:", + stringify!($variant), + ":", + $id, + ">" + ) + ) + } + )* + } + } + } + }; +} + +/// NOTE: This allows non-PascalCase because the emoji itself could have a non-PascalCase name. I'd +/// still try to have them all be PascalCase though. +#[allow(non_camel_case_types)] +#[allow(dead_code)] +pub enum Emojis { + HuTaoHeh, +} + +display_emoji_impl! { + Emojis { + HuTaoHeh => "1317920658021290097", + } +} diff --git a/src/enums/mod.rs b/src/enums/mod.rs index 305b09f..4430cc2 100644 --- a/src/enums/mod.rs +++ b/src/enums/mod.rs @@ -1,2 +1,3 @@ pub mod command_enums; +pub mod emojis; pub mod schemas; diff --git a/src/event_handler/handler.rs b/src/event_handler/handler.rs index 4a0fb4c..144cf57 100644 --- a/src/event_handler/handler.rs +++ b/src/event_handler/handler.rs @@ -12,8 +12,7 @@ use crate::{ command_data::{Data, Error}, database::DATABASE_FILENAME, }, - database::connect_to_db, - database::level_system::*, + database::{connect_to_db, level_system::*}, utils::{replies::handle_replies, string_manipulation::remove_emojis_and_embeds_from_str}, }; use poise::serenity_prelude as serenity;