From a311ca7a2ee4d29091e7473838d43a8d83862316 Mon Sep 17 00:00:00 2001 From: Ladislav Hano Date: Sun, 6 Oct 2024 16:54:08 +0200 Subject: [PATCH] feat: add commands to main, guild/global command registration --- Cargo.toml | 1 + src/main.rs | 32 +++++++++++++++++++++----------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7b8e356..359cd1d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,3 +23,4 @@ tenorv2 = { path = "./tenor-v2/tenorv2" } [features] DEBUG = [] RELEASE = [] +GUILD_COMMAND = [] \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index de3c551..59da021 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,13 +5,14 @@ use std::time::Duration; use std::error; use std::env; -use poise::samples::register_in_guild; +use poise::samples::register_globally; +use poise::samples::register_in_guild; use serenity::async_trait; use serenity::prelude::GatewayIntents; use serenity::client::Context; use serenity::model::gateway::Ready; -use serenity::all::{EventHandler, GuildId, Message}; +use serenity::all::{EventHandler, Message}; use serenity::Client; use dotenv::dotenv; @@ -82,7 +83,10 @@ async fn main() -> anyhow::Result<()> { // create poise framework for registering commands let options: poise::FrameworkOptions<(), Box> = poise::FrameworkOptions { - commands: vec![], + commands: vec![ + commands::say(), + commands::hug() + ], prefix_options: poise::PrefixFrameworkOptions { prefix: Some("/".into()), edit_tracker: Some(Arc::new(poise::EditTracker::for_timespan( @@ -99,15 +103,21 @@ async fn main() -> anyhow::Result<()> { ..Default::default() }; - let debug_guild_id = env::var("DEBUG_GUILD_ID") - .context("DEBUG_GUILD_ID not found in env")? - .parse::().unwrap(); - + let framework = poise::Framework::builder() - .setup(move |ctx, _ready, framework| { - Box::pin(async move { - register_in_guild(ctx, &framework.options().commands, GuildId::new(debug_guild_id)).await?; - Ok(()) + .setup(move |ctx, _ready, framework| { + Box::pin(async move { + #[cfg(feature="GUILD_COMMAND")] { + let debug_guild_id = env::var("DEBUG_GUILD_ID") + .context("DEBUG_GUILD_ID not found in env")? + .parse::().unwrap(); + + register_in_guild(ctx, &framework.options().commands, debug_guild_id).await?; + } + #[cfg(not(feature="GUILD_COMMAND"))] { + register_globally(ctx, &framework.options().commands).await?; + } + Ok(()) }) }) .options(options)