refactor: Convert project from JavaScript to TypeScript
- Converted all .js files to .ts - Added TypeScript configuration (tsconfig.json) - Added ESLint and Prettier configuration - Updated package.json dependencies - Modified Docker and application configurations
This commit is contained in:
64
src/events/interactionCreate.ts
Normal file
64
src/events/interactionCreate.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { Events, Interaction } from 'discord.js'; // Import Interaction type
|
||||
import logger from '../utils/logger'; // Use default import
|
||||
import { BotClient } from '../index'; // Import BotClient type
|
||||
|
||||
export default { // Use export default
|
||||
name: Events.InteractionCreate,
|
||||
async execute(interaction: Interaction, client: BotClient) { // Add types
|
||||
// Handle only slash commands (ChatInputCommand) for now
|
||||
if (!interaction.isChatInputCommand()) return;
|
||||
|
||||
// Store command name after type check
|
||||
const commandName = interaction.commandName;
|
||||
|
||||
// client.commands should be typed as Collection<string, CommandType> on BotClient
|
||||
const command = client.commands.get(commandName);
|
||||
|
||||
if (!command) {
|
||||
logger.error(`No command matching ${commandName} was found.`);
|
||||
try {
|
||||
// Check if interaction is replyable before attempting reply
|
||||
if (interaction.isRepliable()) {
|
||||
await interaction.reply({ content: 'Error: This command was not found!', ephemeral: true });
|
||||
}
|
||||
} catch (replyError: unknown) { // Type caught error
|
||||
const errorMsg = replyError instanceof Error ? replyError.message : String(replyError);
|
||||
// Use stored commandName variable
|
||||
logger.error(`Failed to send 'command not found' reply for command '${commandName}': ${errorMsg}`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Execute the command's logic
|
||||
// Command execute function expects ChatInputCommandInteraction, but we check type above
|
||||
await command.execute(interaction, client);
|
||||
logger.info(`Executed command '${commandName}' for user ${interaction.user.tag}`);
|
||||
} catch (error: unknown) { // Type caught error
|
||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||
// Use stored commandName variable
|
||||
logger.error(`Error executing command '${commandName}': ${errorMsg}`, error);
|
||||
|
||||
// Try to reply to the interaction, otherwise edit the deferred reply if applicable
|
||||
const replyOptions = { content: 'There was an error while executing this command!', ephemeral: true };
|
||||
try {
|
||||
// Check if interaction is replyable before attempting reply/followUp
|
||||
if (!interaction.isRepliable()) {
|
||||
// Use stored commandName variable
|
||||
logger.warn(`Interaction for command '${commandName}' is no longer replyable.`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (interaction.replied || interaction.deferred) {
|
||||
await interaction.followUp(replyOptions);
|
||||
} else {
|
||||
await interaction.reply(replyOptions);
|
||||
}
|
||||
} catch (replyError: unknown) { // Type caught error
|
||||
const replyErrorMsg = replyError instanceof Error ? replyError.message : String(replyError);
|
||||
// Use stored commandName variable
|
||||
logger.error(`Failed to send error reply for command '${commandName}': ${replyErrorMsg}`);
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user