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