|
| 1 | +use anyhow::Context; |
| 2 | +use async_trait::async_trait; |
| 3 | +use builders::command_option::CommandOptionBuilder; |
| 4 | +use twilight_model::application::command::{Command, CommandOptionType, CommandType}; |
| 5 | +use twilight_model::application::interaction::application_command::CommandOptionValue; |
| 6 | +use twilight_model::application::interaction::{ |
| 7 | + Interaction, InteractionContextType, InteractionData, |
| 8 | +}; |
| 9 | +use twilight_model::guild::Permissions; |
| 10 | +use twilight_model::http::interaction::{InteractionResponse, InteractionResponseType}; |
| 11 | +use twilight_model::oauth::ApplicationIntegrationType; |
| 12 | +use twilight_util::builder::command::CommandBuilder; |
| 13 | +use twilight_util::builder::InteractionResponseDataBuilder; |
| 14 | + |
| 15 | +use crate::commands::CommandHandler; |
| 16 | + |
| 17 | +const QUERY_OPTION_NAME: &str = "query"; |
| 18 | +const MENTION_OPTION_NAME: &str = "mention"; |
| 19 | + |
| 20 | +#[allow(dead_code)] |
| 21 | +pub(crate) struct Faq<'a> { |
| 22 | + pub(crate) cmd: &'a Interaction, |
| 23 | +} |
| 24 | + |
| 25 | +#[async_trait] |
| 26 | +impl CommandHandler for Faq<'_> { |
| 27 | + fn model(ctx: Option<crate::Context>) -> anyhow::Result<Command> { |
| 28 | + let ctx = ctx.expect("ctx is required"); |
| 29 | + let query_option = CommandOptionBuilder::new( |
| 30 | + QUERY_OPTION_NAME, |
| 31 | + "The response to send.", |
| 32 | + CommandOptionType::String, |
| 33 | + ) |
| 34 | + .choices(ctx.cfg.faq_option_choices()) |
| 35 | + .required(true) |
| 36 | + .build()?; |
| 37 | + |
| 38 | + let mention_option = CommandOptionBuilder::new( |
| 39 | + MENTION_OPTION_NAME, |
| 40 | + "The user to mention in the response.", |
| 41 | + CommandOptionType::User, |
| 42 | + ) |
| 43 | + .build()?; |
| 44 | + |
| 45 | + Ok(CommandBuilder::new( |
| 46 | + "faq", |
| 47 | + "Send quick responses to common questions/queries.", |
| 48 | + CommandType::ChatInput, |
| 49 | + ) |
| 50 | + .contexts([InteractionContextType::Guild]) |
| 51 | + .integration_types([ApplicationIntegrationType::GuildInstall]) |
| 52 | + .default_member_permissions(Permissions::MANAGE_CHANNELS) |
| 53 | + .option(query_option) |
| 54 | + .option(mention_option) |
| 55 | + .validate() |
| 56 | + .context("validate faq command")? |
| 57 | + .build()) |
| 58 | + } |
| 59 | + |
| 60 | + async fn exec(&self, ctx: crate::Context) -> anyhow::Result<()> { |
| 61 | + let Some(InteractionData::ApplicationCommand(data)) = &self.cmd.data else { |
| 62 | + anyhow::bail!("expected application command interaction"); |
| 63 | + }; |
| 64 | + // Get the query option from the command data |
| 65 | + let query = data |
| 66 | + .options |
| 67 | + .iter() |
| 68 | + .find(|opt| opt.name == QUERY_OPTION_NAME) |
| 69 | + .context("missing query option")?; |
| 70 | + let CommandOptionValue::String(query) = &query.value else { |
| 71 | + anyhow::bail!("expected string query option"); |
| 72 | + }; |
| 73 | + let Some(embed) = ctx.cfg.faq_option_embed(query) else { |
| 74 | + anyhow::bail!("unknown query option: {}", query); |
| 75 | + }; |
| 76 | + |
| 77 | + // Create the response builder with an embed |
| 78 | + let mut response_builder = InteractionResponseDataBuilder::new().embeds([embed]); |
| 79 | + |
| 80 | + // Add mention if provided |
| 81 | + let mention = data |
| 82 | + .options |
| 83 | + .iter() |
| 84 | + .find(|opt| opt.name == MENTION_OPTION_NAME); |
| 85 | + |
| 86 | + if let Some(mention) = mention { |
| 87 | + let CommandOptionValue::User(u_id) = mention.value else { |
| 88 | + anyhow::bail!("expected user option"); |
| 89 | + }; |
| 90 | + response_builder = response_builder.content(format!("<@{u_id}>")); |
| 91 | + } |
| 92 | + |
| 93 | + // Send the response |
| 94 | + ctx.http |
| 95 | + .interaction(self.cmd.application_id) |
| 96 | + .create_response(self.cmd.id, &self.cmd.token, &InteractionResponse { |
| 97 | + kind: InteractionResponseType::ChannelMessageWithSource, |
| 98 | + data: Some(response_builder.build()), |
| 99 | + }) |
| 100 | + .await?; |
| 101 | + |
| 102 | + Ok(()) |
| 103 | + } |
| 104 | +} |
0 commit comments