refactor: Refactor interaction handling and event management

- Updated interactionCreate event to improve error handling and logging.
- Enhanced ready event to ensure client user is available before proceeding.
- Refactored voiceStateUpdate event for better clarity and error handling.
- Adjusted index.ts to improve client initialization and command/event loading.
- Improved Shoukaku event handling and initialization in ShoukakuEvents.ts.
- Enhanced logger utility for better message formatting.
- Updated TypeScript configuration for better compatibility and strictness.
- Created a new botClient type definition for improved type safety.
This commit is contained in:
2025-04-24 23:42:36 +08:00
parent c42e0931d6
commit 72a59bbcdd
18 changed files with 1088 additions and 984 deletions

View File

@@ -1,64 +1,37 @@
import { Events, Interaction } from 'discord.js'; // Import Interaction type
import logger from '../utils/logger'; // Use default import
import { BotClient } from '../index'; // Import BotClient type
import { Events, Interaction } from "discord.js";
import { BotClient } from "../types/botClient";
import logger from "../utils/logger";
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;
export default {
name: Events.InteractionCreate,
async execute(interaction: Interaction, client?: BotClient) {
if (!interaction.isChatInputCommand()) return;
// Store command name after type check
const commandName = interaction.commandName;
if (!client) {
logger.error("Client not provided to interaction handler");
return;
}
// client.commands should be typed as Collection<string, CommandType> on BotClient
const command = client.commands.get(commandName);
const command = client.commands.get(interaction.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;
}
if (!command) {
await interaction.reply({
content: "Command not found!",
ephemeral: true,
});
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}`);
}
}
},
try {
await command.execute(interaction, client);
} catch (error) {
logger.error(`Error executing command ${interaction.commandName}:`, error);
if (interaction.isRepliable()) {
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
}
},
};