Skip to content

Commit

Permalink
Added discord emoji handling
Browse files Browse the repository at this point in the history
This will be useful if I decide to add and use custom emojis to the bot
  • Loading branch information
1Git2Clone committed Dec 15, 2024
1 parent 746ef0a commit a51fb34
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
63 changes: 63 additions & 0 deletions src/enums/emojis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/// Discord emojis are sent like this:
///
/// `<EmojiName:EmojiId>`
///
/// 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",
}
}
1 change: 1 addition & 0 deletions src/enums/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod command_enums;
pub mod emojis;
pub mod schemas;
3 changes: 1 addition & 2 deletions src/event_handler/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a51fb34

Please sign in to comment.