summaryrefslogtreecommitdiff
path: root/src/command/mod.rs
diff options
context:
space:
mode:
authorseth <[email protected]>2024-05-27 04:55:45 -0400
committerseth <[email protected]>2024-05-27 04:56:48 -0400
commitc69eea2f4823da476628742fbbec600ee95ac049 (patch)
tree7cf3d87f5f202e6049ba44a06ac6fe9d3558826b /src/command/mod.rs
initial commit
Diffstat (limited to 'src/command/mod.rs')
-rw-r--r--src/command/mod.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/command/mod.rs b/src/command/mod.rs
new file mode 100644
index 0000000..eda4167
--- /dev/null
+++ b/src/command/mod.rs
@@ -0,0 +1,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(())
+}