feat(events/interaction_create): adding the Interaction handle autodetect
This commit is contained in:
parent
df0d794f44
commit
4a832f75be
1 changed files with 18 additions and 14 deletions
|
|
@ -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) }
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue