interactionCreate.ts

1import { Client, Events, Interaction } from 'discord.js'
2import { Bot, Event } from '@/types/bot'
3import Logger from '@/classes/logger'
4
5export default {
6	name: Events.InteractionCreate,
7	once: false,
8	async execute(client: Bot<Client>, interaction: Interaction) {
9		if (!interaction.isChatInputCommand()) return
10
11		const command = client.commands.get(interaction.commandName)
12		if (!command) {
13			Logger.log(
14				'warn',
15				`Command ${interaction.commandName} not found`,
16				'Commands',
17			)
18			return
19		}
20
21		try {
22			await command.execute(interaction)
23		} catch (error: any) {
24			Logger.log(
25				'error',
26				`Error executing command ${interaction.commandName}: ${error.message}`,
27				'Commands',
28			)
29			if (interaction.replied || interaction.deferred) {
30				await interaction.followUp({
31					content: 'There was an error while executing this command!',
32					ephemeral: true,
33				})
34			} else {
35				await interaction.reply({
36					content: 'There was an error while executing this command!',
37					ephemeral: true,
38				})
39			}
40		}
41	},
42} as Event
43