From 28d5a0a97d27a0cb42409f61070c1e813608bb14 Mon Sep 17 00:00:00 2001 From: Nirsu Date: Thu, 5 Oct 2023 16:11:29 +0200 Subject: [PATCH] [FEAT](commands): emoji-steal --- docker-compose.postgres.yml | 2 +- src/core/commands.rs | 9 ++++++++ src/core/slash_command.rs | 4 ++-- src/features/emoji.rs | 41 +++++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/docker-compose.postgres.yml b/docker-compose.postgres.yml index 97439f2..e166fd2 100644 --- a/docker-compose.postgres.yml +++ b/docker-compose.postgres.yml @@ -1,7 +1,7 @@ version: "3" services: postgres: - image: postgres + image: postgres:15 restart: always ports: - 5432:5432 diff --git a/src/core/commands.rs b/src/core/commands.rs index 3f8ee8f..f362748 100644 --- a/src/core/commands.rs +++ b/src/core/commands.rs @@ -320,6 +320,15 @@ lazy_static! { channel: None, usage: "@BOT check_calendar ", permission: Role::User, + }, + "emoji-steal" => + Command { + exec: emoji::emoji_steal, + argument_min: 0, + argument_max: 0, + channel: None, + usage: "@BOT emoji-steal (expected to be used as a reply to a message containing an emoji)", + permission: Role::User, } ]; } diff --git a/src/core/slash_command.rs b/src/core/slash_command.rs index ff565bc..cd0bc92 100644 --- a/src/core/slash_command.rs +++ b/src/core/slash_command.rs @@ -90,7 +90,7 @@ pub async fn handle_event(interaction: Interaction, ctx: Context) { .unwrap() } "office-week" => { - let month = (Utc::now().iso_week().week() + 2) % 4; + let month = (Utc::now().iso_week().week() + 1) % 3 + 1; let result = format!("it's currently S0{month}"); command .create_interaction_response(&ctx.http, |res| { @@ -108,7 +108,7 @@ pub async fn handle_event(interaction: Interaction, ctx: Context) { #[test] fn test_office_week() { - let month = (Utc::now().iso_week().week() + 2) % 4; + let month = (Utc::now().iso_week().week() + 1) % 3 + 1; dbg!(month); // } diff --git a/src/features/emoji.rs b/src/features/emoji.rs index debe2d5..a7110fd 100644 --- a/src/features/emoji.rs +++ b/src/features/emoji.rs @@ -36,3 +36,44 @@ pub async fn add(params: CallBackParams<'_>) -> CallbackReturn { Ok(Some(String::from("I am not able to get this emoji"))) } } + +#[command] +pub async fn emoji_steal(params: CallBackParams<'_>) -> CallbackReturn { + match ¶ms.message.message_reference { + None => Ok(Some(String::from("This message is not a reply"))), + Some(message) => { + let message_content = params + .context + .http + .get_message(message.channel_id.0, message.message_id.unwrap().0) + .await + .expect("Expected a recent message to be in the cache"); + + if let Some((is_animated, emoji_name, emoji_id)) = emoji_str_convert(&message_content.content) + { + let extension = if is_animated { "gif" } else { "png" }; + let url = format!( + "https://cdn.discordapp.com/emojis/{}.{}?size=128", + emoji_id, extension + ); + + let client = reqwest::Client::builder().build()?; + let response = client.get(url).send().await.unwrap(); + let response_body = response.bytes().await.unwrap(); + let base64_img = format!( + "data:image/{};base64,{}", + extension, + base64::encode(response_body) + ); + + let guild = params.message.guild_id.unwrap_or(GuildId(GUILD_ID)); + guild + .create_emoji(¶ms.context.http, emoji_name, &base64_img) + .await?; + Ok(Some(String::from(":ok:"))) + } else { + Ok(Some(String::from("I am not able to get this emoji"))) + } + } + } +}