feat(events/mod): adding the Interaction handle autodetect

This commit is contained in:
Raphael 2026-02-12 20:10:54 +01:00
parent cd85236218
commit e3834bd289
No known key found for this signature in database

View file

@ -1,19 +1,64 @@
include!("./mod_gen.rs");
pub mod bot;
use crate::commands::SlashCommand;
use serenity::all::*;
#[serenity::async_trait]
pub trait BotEvent: Send + Sync {
fn event_type(&self) -> &'static str;
async fn on_ready(&self, _ctx: &Context, _ready: &Ready, _commands: &[Box<dyn SlashCommand>]) {}
async fn on_interaction_create(
&self,
_ctx: &Context,
_interaction: &Interaction,
_commands: &[Box<dyn SlashCommand>],
) {
}
async fn on_message(&self, _ctx: &Context, _msg: &Message) {}
}
pub struct EventEntry {
pub create: fn() -> Box<dyn BotEvent>,
}
inventory::collect!(EventEntry);
pub fn import() -> Vec<Box<dyn BotEvent>> {
inventory::iter::<EventEntry>
.into_iter()
.map(|entry| (entry.create)())
.collect()
}
pub struct Bot {
pub commands: Vec<Box<dyn SlashCommand>>,
pub events: Vec<Box<dyn BotEvent>>,
}
#[serenity::async_trait]
impl EventHandler for Bot {
async fn ready(&self, ctx: Context, ready: Ready) {
bot::ready::handle(&ctx, &ready, &self.commands).await;
for event in self.events.iter().filter(|e| e.event_type() == "ready") {
event.on_ready(&ctx, &ready, &self.commands).await;
}
}
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
bot::interaction_create::handle(&ctx, &interaction, &self.commands).await;
for event in self
.events
.iter()
.filter(|e| e.event_type() == "interaction_create")
{
event
.on_interaction_create(&ctx, &interaction, &self.commands)
.await;
}
}
async fn message(&self, ctx: Context, msg: Message) {
for event in self.events.iter().filter(|e| e.event_type() == "message") {
event.on_message(&ctx, &msg).await;
}
}
}