refactor: Migrate to ES modules and implement command loading utility for improved structure and logging
This commit is contained in:
@@ -6,11 +6,17 @@
|
|||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
|
|
||||||
require('dotenv').config();
|
import 'dotenv/config';
|
||||||
const fs = require('node:fs');
|
import { fileURLToPath } from 'url';
|
||||||
const path = require('node:path');
|
import { dirname } from 'path';
|
||||||
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
|
import fs from 'node:fs';
|
||||||
const Logger = require('./logger');
|
import path from 'node:path';
|
||||||
|
import { Client, Collection, Events, GatewayIntentBits } from 'discord.js';
|
||||||
|
import Logger from './logger.js';
|
||||||
|
import { loadCommands } from './utils/commandLoader.js';
|
||||||
|
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = dirname(__filename);
|
||||||
|
|
||||||
const logger = new Logger('bot');
|
const logger = new Logger('bot');
|
||||||
|
|
||||||
@@ -24,21 +30,11 @@ const client = new Client({
|
|||||||
|
|
||||||
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'));
|
|
||||||
|
|
||||||
await Promise.all(
|
const commands = await loadCommands(commandsPath, logger);
|
||||||
commandFiles.map(async file => {
|
commands.forEach(command => {
|
||||||
const filePath = path.join(commandsPath, file);
|
client.commands.set(command.data.name, command);
|
||||||
const command = await import(filePath);
|
});
|
||||||
if ('data' in command && 'execute' in command) {
|
|
||||||
client.commands.set(command.data.name, command);
|
|
||||||
} else {
|
|
||||||
logger.warn(
|
|
||||||
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
client.once(Events.ClientReady, () => {
|
client.once(Events.ClientReady, () => {
|
||||||
logger.log(`Ready! Logged in as ${client.user.tag}`);
|
logger.log(`Ready! Logged in as ${client.user.tag}`);
|
||||||
|
|||||||
+5
-15
@@ -8,39 +8,29 @@
|
|||||||
|
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
import { REST, Routes } from 'discord.js';
|
import { REST, Routes } from 'discord.js';
|
||||||
import { readdirSync } from 'node:fs';
|
|
||||||
import { join, dirname } from 'node:path';
|
import { join, dirname } from 'node:path';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
// eslint-disable-next-line import/extensions
|
// eslint-disable-next-line import/extensions
|
||||||
import Logger from './logger.js';
|
import Logger from './logger.js';
|
||||||
|
import { loadCommands } from './utils/commandLoader.js';
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||||
const logger = new Logger('deploy-commands');
|
const logger = new Logger('deploy-commands');
|
||||||
|
|
||||||
const commands = [];
|
|
||||||
const commandsPath = join(__dirname, 'commands');
|
const commandsPath = join(__dirname, 'commands');
|
||||||
const commandFiles = readdirSync(commandsPath).filter(file => file.endsWith('.js'));
|
const commands = await loadCommands(commandsPath, logger);
|
||||||
|
const commandData = commands.map(command => command.data.toJSON());
|
||||||
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());
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
const rest = new REST().setToken(process.env.DISCORD_TOKEN);
|
const rest = new REST().setToken(process.env.DISCORD_TOKEN);
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
logger.log(`Started refreshing ${commands.length} application (/) commands.`);
|
logger.log(`Started refreshing ${commandData.length} application (/) commands.`);
|
||||||
|
|
||||||
const data = await rest.put(Routes.applicationCommands(process.env.CLIENT_ID), {
|
const data = await rest.put(Routes.applicationCommands(process.env.CLIENT_ID), {
|
||||||
body: commands,
|
body: commandData,
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.log(`Successfully reloaded ${data.length} application (/) commands.`);
|
logger.log(`Successfully reloaded ${data.length} application (/) commands.`);
|
||||||
|
|||||||
@@ -46,6 +46,12 @@
|
|||||||
dest: "{{ app_dir }}/commands/"
|
dest: "{{ app_dir }}/commands/"
|
||||||
mode: '0644'
|
mode: '0644'
|
||||||
|
|
||||||
|
- name: Copy utils directory
|
||||||
|
copy:
|
||||||
|
src: utils/
|
||||||
|
dest: "{{ app_dir }}/utils/"
|
||||||
|
mode: '0644'
|
||||||
|
|
||||||
- name: Install npm dependencies
|
- name: Install npm dependencies
|
||||||
npm:
|
npm:
|
||||||
path: "{{ app_dir }}"
|
path: "{{ app_dir }}"
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import { readdirSync } from 'node:fs';
|
||||||
|
import { join } from 'node:path';
|
||||||
|
|
||||||
|
export async function loadCommands(commandsPath, logger) {
|
||||||
|
const commands = [];
|
||||||
|
const commandFiles = readdirSync(commandsPath).filter(file => file.endsWith('.js'));
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
commandFiles.map(async file => {
|
||||||
|
try {
|
||||||
|
const filePath = join(commandsPath, file);
|
||||||
|
const commandModule = await import(filePath);
|
||||||
|
const command = commandModule.default;
|
||||||
|
|
||||||
|
if (!command?.data || !command?.execute) {
|
||||||
|
logger.warn(
|
||||||
|
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
commands.push(command);
|
||||||
|
logger.log(`Loaded command: ${command.data.name}`);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`Error loading command from ${file}:`, error);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
return commands;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user