aboutsummaryrefslogtreecommitdiff
path: root/src/commands/mod.rs
blob: 083b200ddba176e7c50c4677d9227003e0d2432b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
mod discocaml;

use crate::commands::discocaml::DiscocamlConfig;
use anyhow::{Context as _, Result};
use serde::Deserialize;
use serenity::{
    all::{Command, Interaction},
    client::Context,
};
use sqlx::SqlitePool;

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CommandsConfig {
    pub discocaml: DiscocamlConfig,
}

pub async fn set_commands(ctx: &Context) -> Result<()> {
    Command::set_global_commands(&ctx.http, vec![discocaml::command()])
        .await
        .context("failed to set commands")?;
    Ok(())
}

pub async fn handle_interaction(
    ctx: &Context,
    config: &CommandsConfig,
    db: &SqlitePool,
    interaction: &Interaction,
) -> Result<()> {
    match interaction {
        Interaction::Command(cmd) => match &cmd.data.name as &str {
            "discocaml" => discocaml::handle_command(ctx, &config.discocaml, db, cmd)
                .await
                .context("failed to handle discocaml command"),
            _ => {
                log::warn!("unexpected interaction: {:?}", interaction);
                Ok(())
            }
        },

        _ => {
            log::warn!("unexpected interaction: {:?}", interaction);
            Ok(())
        }
    }
}