2024-01-26 13:28:02 +00:00
|
|
|
import { Events } from 'discord.js';
|
2024-02-11 01:04:12 +00:00
|
|
|
import Module from 'module';
|
2024-01-26 13:28:02 +00:00
|
|
|
|
2024-02-11 01:04:12 +00:00
|
|
|
/**
|
|
|
|
* A more precise execution function specifically to call the main property of a module.
|
|
|
|
* @param {import('discord.js').Interaction} interaction
|
|
|
|
* @param {Module} command
|
|
|
|
*/
|
2024-02-04 22:52:08 +00:00
|
|
|
const executeCommand = async (interaction, command) => {
|
2024-01-26 13:28:02 +00:00
|
|
|
// Try executing command
|
|
|
|
try {
|
2024-04-05 09:56:24 +00:00
|
|
|
console.info(`[INFO] Command '${interaction.commandName}' was executed.`);
|
2024-01-26 13:28:02 +00:00
|
|
|
await command.execute(interaction);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
// Follow up/reply with error message
|
|
|
|
if (interaction.replied || interaction.deferred)
|
|
|
|
await interaction.followUp({
|
|
|
|
content: 'There was an error while executing this command!',
|
|
|
|
ephemeral: true
|
|
|
|
});
|
|
|
|
else
|
|
|
|
await interaction.reply({
|
|
|
|
content: 'There was an error while executing this command!',
|
|
|
|
ephemeral: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-02-11 01:04:12 +00:00
|
|
|
/**
|
|
|
|
* A generic execution function to call command methods.
|
|
|
|
* @param {import('discord.js').Interaction} interaction
|
|
|
|
* @param {Module} command
|
|
|
|
* @param {string} name
|
|
|
|
* @param {string=} description
|
|
|
|
* @param {string=} cmdName
|
|
|
|
*/
|
2024-02-05 21:56:22 +00:00
|
|
|
const genericExecute = async (interaction, command, name, description, cmdName) => {
|
2024-01-26 13:28:02 +00:00
|
|
|
try {
|
2024-02-06 15:18:06 +00:00
|
|
|
console.info(
|
2024-04-05 09:56:24 +00:00
|
|
|
`[INFO] Command '${cmdName ?? interaction.commandName ?? 'anonymous'}' ${
|
2024-02-06 15:18:06 +00:00
|
|
|
description ?? `used "${name}"`
|
|
|
|
}.`
|
|
|
|
);
|
2024-01-26 13:28:02 +00:00
|
|
|
await command[name](interaction);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const name = Events.InteractionCreate;
|
2024-02-11 01:04:12 +00:00
|
|
|
/** @param {import('discord.js').Interaction} interaction */
|
2024-01-26 13:28:02 +00:00
|
|
|
export async function execute(interaction) {
|
2024-02-11 01:04:12 +00:00
|
|
|
/** @type {Module} */
|
2024-01-26 13:28:02 +00:00
|
|
|
let command = interaction.client.commands.get(interaction.commandName);
|
|
|
|
|
2024-02-04 22:52:08 +00:00
|
|
|
// Execute slash- and context-menu-commands
|
|
|
|
if (interaction.isChatInputCommand() || interaction.isMessageContextMenuCommand()) {
|
|
|
|
await executeCommand(interaction, command);
|
2024-01-26 13:28:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Autocomplete input
|
|
|
|
if (interaction.isAutocomplete() && 'autocomplete' in command) {
|
|
|
|
await genericExecute(interaction, command, 'autocomplete');
|
|
|
|
return;
|
|
|
|
}
|
2024-02-04 22:52:08 +00:00
|
|
|
// Modal submit event
|
2024-01-26 13:28:02 +00:00
|
|
|
if (interaction.isModalSubmit()) {
|
|
|
|
const name = interaction.customId.split('-')[0];
|
|
|
|
command = interaction.client.commands.get(name);
|
|
|
|
|
2024-02-05 21:56:22 +00:00
|
|
|
await genericExecute(interaction, command, 'modalSubmit', 'submitted a modal', name);
|
2024-01-26 13:28:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if command exists
|
|
|
|
if (interaction.commandName && !command) {
|
|
|
|
console.warn(`[WARNING] No command matching ${interaction.commandName} was found.`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|