Refactor command loading to use dynamic imports and improve logging for missing command properties

This commit is contained in:
2025-02-06 20:08:37 -05:00
parent 566a6784c8
commit 00ba5801ad
+13 -7
View File
@@ -16,22 +16,28 @@ const client = new Client({
GatewayIntentBits.Guilds, GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent, GatewayIntentBits.MessageContent,
] ],
}); });
client.commands = new Collection(); client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands'); const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) { const logger = message => process.stdout.write(`${message}\n`);
await Promise.all(
commandFiles.map(async file => {
const filePath = path.join(commandsPath, file); const filePath = path.join(commandsPath, file);
const command = require(filePath); const command = await import(`file://${filePath}`);
if ('data' in command && 'execute' in command) { if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command); client.commands.set(command.data.name, command);
} else { } else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); logger(
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`,
);
} }
} }),
);
client.once(Events.ClientReady, () => { client.once(Events.ClientReady, () => {
console.log(`Ready! Logged in as ${client.user.tag}`); console.log(`Ready! Logged in as ${client.user.tag}`);
@@ -54,12 +60,12 @@ client.on(Events.InteractionCreate, async interaction => {
if (interaction.replied || interaction.deferred) { if (interaction.replied || interaction.deferred) {
await interaction.followUp({ await interaction.followUp({
content: 'There was an error while executing this command!', content: 'There was an error while executing this command!',
ephemeral: true ephemeral: true,
}); });
} else { } else {
await interaction.reply({ await interaction.reply({
content: 'There was an error while executing this command!', content: 'There was an error while executing this command!',
ephemeral: true ephemeral: true,
}); });
} }
} }