From 1e7f3017229e40f5c9e88c31894c367520b9d9b1 Mon Sep 17 00:00:00 2001 From: Raphael Date: Wed, 25 Feb 2026 12:42:57 +0100 Subject: [PATCH] feat(commands/gestion): Adding the set command --- src/commands/gestion/set.rs | 120 ++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/commands/gestion/set.rs diff --git a/src/commands/gestion/set.rs b/src/commands/gestion/set.rs new file mode 100644 index 0000000..34cebc7 --- /dev/null +++ b/src/commands/gestion/set.rs @@ -0,0 +1,120 @@ +use crate::{ + commands::{ + CommandCategory, CommandEntry, SlashCommand + }, + config::EmojiConfig, utils::perm::is_owner, +}; + +use serenity::all::{ + CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOption, CommandOptionType, Context, CreateAttachment, CreateCommand, CreateCommandOption, CreateInteractionResponse, CreateInteractionResponseMessage, EditInteractionResponse, EditProfile +}; +use sqlx::PgPool; +use tracing::{debug, info}; +use anyhow::Result; + +pub struct Set; + +async fn set_picture(slashcmd: &Set, ctx: &Context, cmd: &CommandInteraction, db: &PgPool, emoji: &EmojiConfig, subcmd: &CommandDataOption) -> Result<()> { + let inner_options = match &subcmd.value { + CommandDataOptionValue::SubCommand(opts) => opts, + _ => return Err(anyhow::anyhow!("Expected a subcommand")), + }; + + let url = inner_options + .iter() + .find(|opt| opt.name == "link") + .ok_or_else(|| anyhow::anyhow!("Option 'link' not found"))? + .value + .as_str() + .ok_or_else(|| anyhow::anyhow!("Option 'link' is not a string"))?; + + let attachment: CreateAttachment = CreateAttachment::url(&ctx.http, &url) + .await?; + + let builder: EditProfile = EditProfile::new().avatar(&attachment); + ctx.http + .get_current_user() + .await? + .edit(&ctx.http, builder) + .await?; + + let message: CreateInteractionResponseMessage = CreateInteractionResponseMessage::new() + .content(format!("{} | Avatar changed", emoji.answer.yes)) + .ephemeral(true); + + let response: CreateInteractionResponse = CreateInteractionResponse::Message(message); + + cmd.create_response(&ctx.http, response).await?; + + Ok(()) +} + +#[serenity::async_trait] +impl SlashCommand for Set { + fn name(&self) -> &'static str { + "set" + } + + fn description(&self) -> &'static str { + "Edit bot configuration" + } + + fn category(&self) -> &'static CommandCategory { + &CommandCategory::Gestion + } + + fn register(&self) -> CreateCommand { + let mut options: Vec = Vec::new(); + + let link: CreateCommandOption = CreateCommandOption::new(CommandOptionType::String, "link", "The link to change this options") + .required(true); + + let picture: CreateCommandOption = CreateCommandOption::new(CommandOptionType::SubCommand, "picture", "Set the new photo profile of the bot") + .add_sub_option(link); + options.push(picture); + + info!("\t✅ | {}", self.name()); + CreateCommand::new(self.name()).description(self.description()) + .set_options(options) + } + + async fn run( + &self, + ctx: &Context, + command: &CommandInteraction, + _database: &PgPool, + _emoji: &EmojiConfig, + ) -> Result<()> { + debug!("Set command called"); + if !is_owner(_database, &command.user.id.to_string()).await? { + let message: CreateInteractionResponseMessage = CreateInteractionResponseMessage::new() + .content(format!("{} | This command is only for the owner", _emoji.answer.no)) + .ephemeral(true); + + let response: CreateInteractionResponse = CreateInteractionResponse::Message(message); + + command.create_response(&ctx.http, response).await?; + Ok(()) + } else { + let subcmd: &CommandDataOption = command.data.options.iter().find(|opt| opt.kind() == CommandOptionType::SubCommand).ok_or_else(|| anyhow::anyhow!("Subcommand not found"))?; + let check_ret = match subcmd.name.as_str() { + "picture" => set_picture(self, ctx, command, _database, _emoji, subcmd).await, + _ => Err(anyhow::anyhow!("Set subcommand cannot be found!")) + }; + if check_ret.is_err() { + let message: CreateInteractionResponseMessage = CreateInteractionResponseMessage::new() + .content(format!("{} | Error during command", _emoji.answer.error)) + .ephemeral(true); + + let response: CreateInteractionResponse = CreateInteractionResponse::Message(message); + + command.create_response(&ctx.http, response).await?; + } + check_ret + } + } +} + +inventory::submit! { + CommandEntry { create: || Box::new(Set) } +}