From a41907a25868b175b6c53d348ef9148bf99f747e Mon Sep 17 00:00:00 2001 From: jakubmanczak Date: Fri, 2 May 2025 23:16:55 +0200 Subject: [PATCH] privileged embed sending command; infowitaj embed --- src/discordbot/commands/embed.rs | 81 ++++++++++++++++++++++++++++++++ src/discordbot/commands/mod.rs | 1 + src/discordbot/mod.rs | 2 + 3 files changed, 84 insertions(+) create mode 100644 src/discordbot/commands/embed.rs diff --git a/src/discordbot/commands/embed.rs b/src/discordbot/commands/embed.rs new file mode 100644 index 0000000..0b2e6ea --- /dev/null +++ b/src/discordbot/commands/embed.rs @@ -0,0 +1,81 @@ +use serenity::all::{ + Colour, CommandInteraction, CommandOptionType, Context, CreateCommandOption, CreateEmbed, + CreateEmbedAuthor, CreateInteractionResponse, CreateInteractionResponseMessage, CreateMessage, + EditInteractionResponse, Permissions, ResolvedValue, +}; +use serenity::builder::CreateCommand; + +const GRACTWO_X64_GHLINK: &str = + "https://raw.githubusercontent.com/gractwo/assets/refs/heads/master/raster/logo-x64.png"; + +pub async fn run(ctx: &Context, interaction: &CommandInteraction) -> Result<(), serenity::Error> { + let options = interaction.data.options(); + + let embed_type = options + .iter() + .find_map(|opt| match (opt.name, &opt.value) { + ("type", ResolvedValue::String(str)) => Some(*str), + _ => None, + }) + .expect("Embed type is a required field; should be present."); + + let embed = match embed_type { + "infowitaj" => Some(CreateEmbed::new() + .author(CreateEmbedAuthor::new("Witajcie w Gractwie!").icon_url(GRACTWO_X64_GHLINK)) + .description("Jesteśmy grupą ludzi których kręcą gry. Proste, nie?\nSerwer ten miał wcześniej tematykę Team Fortressową.") + .colour(Colour::BLITZ_BLUE) + .field("Zasady i regulamin", "Zgrubsza: nie bądź uschłą wierzbą.", false) + .field("Gractwo WWW", "https://gractwo.pl", true) + .field("Gractwo DISCORD", "https://gractwo.pl/discord", true) + .field("DISCORD bezpośredni", "discord.gg/NBXq95C", true) + .field("Gractwo BLUESKY", "https://gractwo.pl/bsky", true) + .field("Gractwo YOUTUBE", "https://gractwo.pl/youtube", true) + .field("Gractwo GITHUB", "https://gractwo.pl/github", true)), + _ => None + }; + + let message = CreateInteractionResponseMessage::new() + .ephemeral(true) + .content(if embed.is_some() { + format!("Sending embed of type {embed_type}...") + } else { + format!("{embed_type} isn't a valid embed type") + }); + interaction + .create_response(&ctx.http, CreateInteractionResponse::Message(message)) + .await + .ok(); + + match embed { + Some(e) => { + let msg = CreateMessage::new().embed(e); + match interaction.channel_id.send_message(&ctx.http, msg).await { + Ok(_) => interaction + .edit_response( + &ctx.http, + EditInteractionResponse::new().content("Success!"), + ) + .await + .map(|_| ()), + Err(why) => interaction + .edit_response( + &ctx.http, + EditInteractionResponse::new() + .content(format!("Failed to send embed: {why}")), + ) + .await + .map(|_| ()), + } + } + None => Ok(()), + } +} + +pub fn register() -> CreateCommand { + CreateCommand::new("embed") + .description("komenda do wysyłania embedów.") + .default_member_permissions(Permissions::ADMINISTRATOR) + .add_option( + CreateCommandOption::new(CommandOptionType::String, "type", "type").required(true), + ) +} diff --git a/src/discordbot/commands/mod.rs b/src/discordbot/commands/mod.rs index fb8d046..88cb163 100644 --- a/src/discordbot/commands/mod.rs +++ b/src/discordbot/commands/mod.rs @@ -1,3 +1,4 @@ +pub mod embed; pub mod kiss; pub mod perish; pub mod ping; diff --git a/src/discordbot/mod.rs b/src/discordbot/mod.rs index 2a30c5f..b2160b3 100644 --- a/src/discordbot/mod.rs +++ b/src/discordbot/mod.rs @@ -22,6 +22,7 @@ impl EventHandler for Handler { commands::ping::register(), commands::kiss::register(), commands::perish::register(), + commands::embed::register(), ]; let guild_id = serenity::model::id::GuildId::from( @@ -42,6 +43,7 @@ impl EventHandler for Handler { "ping" => commands::ping::run(&ctx, &cmd).await, "kiss" => commands::kiss::run(&ctx, &cmd).await, "perish" => commands::perish::run(&ctx, &cmd).await, + "embed" => commands::embed::run(&ctx, &cmd).await, _ => { cmd.create_response( &ctx.http,