The world's most clever kitty cat
at main 96 lines 3.1 kB view raw
1mod dump_chain; 2mod load_chain; 3mod markov; 4mod weights; 5 6use std::sync::Arc; 7 8use log::warn; 9use twilight_interactions::command::CreateCommand; 10use twilight_model::application::interaction::{Interaction, application_command::CommandData}; 11use twilight_model::channel::message::MessageFlags; 12use twilight_model::http::interaction::{ 13 InteractionResponse, InteractionResponseData, InteractionResponseType, 14}; 15 16use crate::{BotContext, prelude::*}; 17 18use dump_chain::DumpChainCommand; 19use load_chain::LoadChainCommand; 20use markov::MarkovCommand; 21use weights::WeightsCommand; 22 23const DEFER_INTER_RESP: InteractionResponse = InteractionResponse { 24 kind: InteractionResponseType::DeferredChannelMessageWithSource, 25 data: None, 26}; 27 28const DEFER_INTER_RESP_EPHEMERAL: InteractionResponse = InteractionResponse { 29 kind: InteractionResponseType::DeferredChannelMessageWithSource, 30 data: Some(InteractionResponseData { 31 allowed_mentions: None, 32 attachments: None, 33 choices: None, 34 components: None, 35 content: None, 36 custom_id: None, 37 embeds: None, 38 flags: Some(MessageFlags::EPHEMERAL), 39 title: None, 40 tts: None, 41 poll: None, 42 }), 43}; 44 45#[macro_export] 46macro_rules! require_owner { 47 ($inter:expr, $ctx:expr, $client:expr) => { 48 if $inter.author_id().is_none_or(|id| !$ctx.owners.contains(&id)) { 49 let data = twilight_util::builder::InteractionResponseDataBuilder::new() 50 .content("You're not allowed to run this command!") 51 .flags(twilight_model::channel::message::MessageFlags::EPHEMERAL) 52 .build(); 53 let resp = twilight_model::http::interaction::InteractionResponse { 54 kind: twilight_model::http::interaction::InteractionResponseType::ChannelMessageWithSource, 55 data: Some(data), 56 }; 57 $client.create_response($inter.id, &$inter.token, &resp).await.context("Failed to deny perms")?; 58 return Ok(()); 59 } 60 }; 61} 62 63pub async fn register_all_commands(ctx: Arc<BotContext>) -> Result { 64 let commands = [ 65 WeightsCommand::create_command().into(), 66 DumpChainCommand::create_command().into(), 67 LoadChainCommand::create_command().into(), 68 MarkovCommand::create_command().into(), 69 ]; 70 71 let client = ctx.http.interaction(ctx.app_id); 72 73 client 74 .set_global_commands(&commands) 75 .await 76 .context("Failed to register app commands")?; 77 78 Ok(()) 79} 80 81pub async fn handle_app_command( 82 data: CommandData, 83 ctx: Arc<BotContext>, 84 inter: Interaction, 85) -> Result { 86 match &*data.name { 87 "weights" => WeightsCommand::handle(inter, data, ctx).await, 88 "dump_chain" => DumpChainCommand::handle(inter, data, ctx).await, 89 "load_chain" => LoadChainCommand::handle(inter, data, ctx).await, 90 "markov" => MarkovCommand::handle(inter, data, ctx).await, 91 other => { 92 warn!("Unknown command send: {other}"); 93 Ok(()) 94 } 95 } 96}