Files
kekbot.js/deploy-commands.js
T

51 lines
1.5 KiB
JavaScript
Raw Normal View History

2025-02-02 14:10:51 -05:00
// deploy-commands.js - Discord bot command deployment
// Copyright (C) 2025 Luis Bauza
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
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');
2025-02-02 14:10:51 -05:00
const commands = [];
const commandsPath = join(__dirname, 'commands');
const commandFiles = readdirSync(commandsPath).filter(file => file.endsWith('.js'));
2025-02-02 14:10:51 -05:00
await Promise.all(
commandFiles.map(async file => {
const filePath = join(commandsPath, file);
const command = await import(filePath);
2025-02-02 14:10:51 -05:00
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());
2025-02-02 14:10:51 -05:00
}
}),
);
2025-02-02 14:10:51 -05:00
const rest = new REST().setToken(process.env.DISCORD_TOKEN);
(async () => {
try {
logger.log(`Started refreshing ${commands.length} application (/) commands.`);
2025-02-02 14:10:51 -05:00
const data = await rest.put(Routes.applicationCommands(process.env.CLIENT_ID), {
body: commands,
});
2025-02-02 14:10:51 -05:00
logger.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
logger.error(error);
}
2025-02-02 14:10:51 -05:00
})();