Handle command registration checking on startup
This commit is contained in:
1
.env
1
.env
@@ -32,6 +32,7 @@ OPENAI_TOKEN= # Optional
|
|||||||
OPENAI_BASE_URL=https://api.openai.com/v1
|
OPENAI_BASE_URL=https://api.openai.com/v1
|
||||||
OPENAI_MODEL=gpt-4o-mini
|
OPENAI_MODEL=gpt-4o-mini
|
||||||
|
|
||||||
|
FORCE_REGISTER=false
|
||||||
DEFAULT_API_KEY=test_api_key
|
DEFAULT_API_KEY=test_api_key
|
||||||
DEFAULT_SERVER=
|
DEFAULT_SERVER=
|
||||||
DEFAULT_USER=
|
DEFAULT_USER=
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
use std::sync::{Arc, OnceLock};
|
use std::sync::{Arc, OnceLock};
|
||||||
use serenity::all::{
|
use serenity::all::{CreateInteractionResponse, CreateInteractionResponseMessage, EditInteractionResponse, Interaction, ResumedEvent, UnavailableGuild, UserId};
|
||||||
CreateEmbed, CreateInteractionResponse, CreateInteractionResponseMessage,
|
|
||||||
EditInteractionResponse, Interaction, ResumedEvent, UserId,
|
|
||||||
};
|
|
||||||
use serenity::async_trait;
|
use serenity::async_trait;
|
||||||
use serenity::model::gateway::Ready;
|
use serenity::model::gateway::Ready;
|
||||||
use serenity::model::channel::Message;
|
use serenity::model::channel::Message;
|
||||||
@@ -16,7 +13,7 @@ use crate::data::guilds::GuildCache;
|
|||||||
use crate::HttpKey;
|
use crate::HttpKey;
|
||||||
use crate::utils::{a_or_an, number_to_words};
|
use crate::utils::{a_or_an, number_to_words};
|
||||||
use super::{commands};
|
use super::{commands};
|
||||||
use super::chat::{create_modal_response, user_dm};
|
use super::chat::{create_modal_response};
|
||||||
|
|
||||||
pub struct BotHandler {
|
pub struct BotHandler {
|
||||||
// Open AI Config
|
// Open AI Config
|
||||||
@@ -114,48 +111,7 @@ impl EventHandler for BotHandler {
|
|||||||
|
|
||||||
log::trace!("Handling {} guilds", ready.guilds.len());
|
log::trace!("Handling {} guilds", ready.guilds.len());
|
||||||
for guild in ready.guilds {
|
for guild in ready.guilds {
|
||||||
// Check if guild exists in database
|
update_guild_commands(&ctx, &guild).await;
|
||||||
let guild_id = guild.id.get() as i64;
|
|
||||||
if let None = GuildCache::find_by_id(guild_id).await {
|
|
||||||
let guild_cache = GuildCache {
|
|
||||||
id: guild_id,
|
|
||||||
name: guild.id.name(&ctx.cache),
|
|
||||||
owner_id: None,
|
|
||||||
volume: 100,
|
|
||||||
};
|
|
||||||
guild_cache.insert().await.unwrap();
|
|
||||||
}
|
|
||||||
let commands = guild
|
|
||||||
.id
|
|
||||||
.set_commands(
|
|
||||||
&ctx.http,
|
|
||||||
vec![
|
|
||||||
commands::audio::play::register(),
|
|
||||||
commands::audio::stop::register(),
|
|
||||||
commands::audio::pause::register(),
|
|
||||||
commands::audio::resume::register(),
|
|
||||||
commands::audio::mute::register(),
|
|
||||||
commands::audio::skip::register(),
|
|
||||||
commands::audio::volume::register(),
|
|
||||||
commands::event::schedule::register(),
|
|
||||||
commands::fun::roll::register(),
|
|
||||||
commands::fun::request_roll::register(),
|
|
||||||
commands::utility::ping::register(),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
match commands {
|
|
||||||
Ok(c) => log::info!(
|
|
||||||
"Registered {} commands for guild {}",
|
|
||||||
c.len(),
|
|
||||||
guild.id.get(),
|
|
||||||
),
|
|
||||||
Err(why) => log::error!(
|
|
||||||
"Could not register commands for guild {}: {:?}",
|
|
||||||
guild.id.get(),
|
|
||||||
why
|
|
||||||
),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -264,3 +220,67 @@ async fn handle_oai_messages(oai: &OAI, ctx: &Context, msg: &Message) {
|
|||||||
Err(why) => log::warn!("Could not check mentions: {why}"),
|
Err(why) => log::warn!("Could not check mentions: {why}"),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn update_guild_commands(ctx: &Context, guild: &UnavailableGuild) {
|
||||||
|
// List of commands to register for the guild
|
||||||
|
let guild_commands = vec![
|
||||||
|
commands::audio::play::register(),
|
||||||
|
commands::audio::stop::register(),
|
||||||
|
commands::audio::pause::register(),
|
||||||
|
commands::audio::resume::register(),
|
||||||
|
commands::audio::mute::register(),
|
||||||
|
commands::audio::skip::register(),
|
||||||
|
commands::audio::volume::register(),
|
||||||
|
commands::event::schedule::register(),
|
||||||
|
commands::fun::roll::register(),
|
||||||
|
commands::fun::request_roll::register(),
|
||||||
|
commands::utility::ping::register(),
|
||||||
|
];
|
||||||
|
|
||||||
|
let guild_id = guild.id.get() as i64;
|
||||||
|
let register_commands = match GuildCache::find_by_id(guild_id).await {
|
||||||
|
Some(_) => {
|
||||||
|
env::var("FORCE_REGISTER")
|
||||||
|
.ok()
|
||||||
|
// Parse to true/false
|
||||||
|
.map(|val| val.to_lowercase() == "true")
|
||||||
|
// Default to true on error
|
||||||
|
.unwrap_or(true)
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
// If no guild cache is found, create a new one.
|
||||||
|
let guild_cache = GuildCache {
|
||||||
|
id: guild_id,
|
||||||
|
name: guild.id.name(&ctx.cache),
|
||||||
|
owner_id: None,
|
||||||
|
volume: 100,
|
||||||
|
};
|
||||||
|
if let Err(err) = guild_cache.insert().await {
|
||||||
|
log::error!("Could not insert guild cache: {err}");
|
||||||
|
};
|
||||||
|
true
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
if register_commands {
|
||||||
|
// Register the commands in the guild
|
||||||
|
match guild.id.set_commands(&ctx.http, guild_commands).await {
|
||||||
|
Ok(registered_commands) => {
|
||||||
|
log::info!(
|
||||||
|
"Registered {} commands for guild {}",
|
||||||
|
registered_commands.len(),
|
||||||
|
guild.id.get()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Err(why) => {
|
||||||
|
log::error!(
|
||||||
|
"Could not register commands for guild {}: {:?}",
|
||||||
|
guild.id.get(),
|
||||||
|
why
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
log::debug!("Guild {guild_id} already registered");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
10
src/main.rs
10
src/main.rs
@@ -132,10 +132,16 @@ async fn get_bot_info(http: &Http) -> SirenResult<(Option<UserId>, UserId)> {
|
|||||||
}
|
}
|
||||||
match http.get_current_user().await {
|
match http.get_current_user().await {
|
||||||
Ok(bot) => Ok((bot_owner, bot.id)),
|
Ok(bot) => Ok((bot_owner, bot.id)),
|
||||||
Err(why) => Err(Error::new(500, format!("Could not access the bot id: {why:?}"))),
|
Err(why) => Err(Error::new(
|
||||||
|
500,
|
||||||
|
format!("Could not access the bot id: {why:?}"),
|
||||||
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(why) => Err(Error::new(500, format!("Could not access application info: {why:?}"))),
|
Err(why) => Err(Error::new(
|
||||||
|
500,
|
||||||
|
format!("Could not access application info: {why:?}"),
|
||||||
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user