aboutsummaryrefslogtreecommitdiff
path: root/src/commands/mod.rs
blob: d793c956c794c33856cb40f4c86967ddfb783330 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
pub 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(())
            }
        },
        Interaction::Component(component) => {
            match component
                .message
                .interaction
                .as_ref()
                .map(|i| &i.name as &str)
            {
                Some("discocaml") => {
                    discocaml::handle_button(ctx, &config.discocaml, db, component)
                        .await
                        .context("failed to handle discocaml command")
                }
                _ => {
                    log::warn!("unexpected interaction: {:#?}", interaction);
                    Ok(())
                }
            }
        }

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