Refactor codebase to use ES modules, add logger utility, and update package dependencies

This commit is contained in:
2025-02-09 14:58:31 -05:00
parent 55b4111fe3
commit 876c3daa7f
10 changed files with 471 additions and 70 deletions
+30 -23
View File
@@ -1,4 +1,3 @@
require('dotenv').config();
// deploy-commands.js - Discord bot command deployment
// Copyright (C) 2025 Luis Bauza
//
@@ -7,37 +6,45 @@ require('dotenv').config();
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
const { REST, Routes } = require('discord.js');
const fs = require('node:fs');
const path = require('node:path');
import dotenv from 'dotenv';
import { REST, Routes } from 'discord.js';
import { readdirSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
// eslint-disable-next-line import/extensions
import Logger from './logger.js';
dotenv.config();
const __dirname = dirname(fileURLToPath(import.meta.url));
const logger = new Logger('deploy-commands');
const commands = [];
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
const commandsPath = join(__dirname, 'commands');
const commandFiles = readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
await Promise.all(
commandFiles.map(async file => {
const filePath = join(commandsPath, file);
const command = await import(filePath);
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
commands.push(command.data.toJSON());
}
}
}),
);
const rest = new REST().setToken(process.env.DISCORD_TOKEN);
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
try {
logger.log(`Started refreshing ${commands.length} application (/) commands.`);
const data = await rest.put(
Routes.applicationCommands(process.env.CLIENT_ID),
{ body: commands },
);
const data = await rest.put(Routes.applicationCommands(process.env.CLIENT_ID), {
body: commands,
});
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
logger.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
logger.error(error);
}
})();