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

This commit is contained in:
Raphael 2026-02-12 20:10:29 +01:00
parent df0d794f44
commit 4a832f75be
No known key found for this signature in database

View file

@ -1,17 +1,17 @@
use serenity::all::*; use serenity::all::*;
use crate::commands::SlashCommand; use crate::commands::SlashCommand;
use crate::events::{BotEvent, EventEntry};
pub async fn handle( pub struct InteractionHandler;
ctx: &Context,
interaction: &Interaction,
commands: &[Box<dyn SlashCommand>],
) {
let Interaction::Command(command) = interaction else {
return;
};
let name: &str = command.data.name.as_str(); #[serenity::async_trait]
impl BotEvent for InteractionHandler {
fn event_type(&self) -> &'static str { "interaction_create" }
async fn on_interaction_create(&self, ctx: &Context, interaction: &Interaction, commands: &[Box<dyn SlashCommand>]) {
let Interaction::Command(command) = interaction else { return };
let name = command.data.name.as_str();
match commands.iter().find(|cmd| cmd.name() == name) { match commands.iter().find(|cmd| cmd.name() == name) {
Some(cmd) => { Some(cmd) => {
if let Err(why) = cmd.run(ctx, command).await { if let Err(why) = cmd.run(ctx, command).await {
@ -20,5 +20,9 @@ pub async fn handle(
} }
None => eprintln!("⚠️ | Unable to fetch: /{name}"), None => eprintln!("⚠️ | Unable to fetch: /{name}"),
} }
}
} }
inventory::submit! {
EventEntry { create: || Box::new(InteractionHandler) }
}