summaryrefslogtreecommitdiff
path: root/src/command/mod.rs
blob: eda416704c0d3728abc084ca9c899023f4d3eda9 (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
use eyre::{OptionExt, Result};
use serenity::builder::{
	CreateCommand, CreateInteractionResponse, CreateInteractionResponseMessage,
};
use serenity::model::application::CommandInteraction;
use serenity::prelude::Context;
use tracing::instrument;

use crate::client::SharedClient;

mod ping;
mod track;

macro_rules! cmd {
	($module: ident) => {
		$module::register()
	};
}

/// Return a list of all our [CreateCommand]s
pub fn to_vec() -> Vec<CreateCommand> {
	vec![cmd!(ping), cmd!(track)]
}

/// Dispatch our commands from a [CommandInteraction]
#[instrument(skip(ctx))]
pub async fn dispatch(ctx: &Context, command: &CommandInteraction) -> Result<()> {
	let command_name = command.data.name.as_str();

	// grab our http client from the aether
	let http = {
		let read = ctx.data.read().await;
		read.get::<SharedClient>()
			.ok_or_eyre("Couldn't get shared HTTP client! WHY??????")?
			.clone()
	};

	match command_name {
		"ping" => ping::respond(ctx, command).await?,
		"track" => track::respond(ctx, &http, command).await?,
		_ => {
			let message = CreateInteractionResponseMessage::new().content(format!(
				"It doesn't look like you can use `{command_name}`. Sorry :("
			));
			let response = CreateInteractionResponse::Message(message);
			command.create_response(&ctx, response).await?
		}
	};

	Ok(())
}