Konpeki-Discord-Bot/deploy-commands.js

111 lines
3.7 KiB
JavaScript
Raw Normal View History

/*
* Konpeki Discord Bot - Slash Command Configuration File
* deploy-commands.js - Gathers all current .js files in the commands folder and publishes a list to Discord
*
* Code modified based on example from the discord.js guides
*/
2022-12-15 13:07:35 -06:00
const { REST, Routes } = require('discord.js');
const { clientId, token } = require('./config.json');
const fs = require('node:fs');
const commands = [];
const commandsHelp = [];
2022-12-15 13:07:35 -06:00
// Grab all the command files from the commands directory you created earlier
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
2023-09-13 01:17:31 -05:00
const command = require(`./commands/${file}`);
commands.push(command.data.toJSON());
commandsHelp.push(command.data);
2022-12-15 13:07:35 -06:00
}
// Construct and prepare an instance of the REST module
const rest = new REST({ version: '10' }).setToken(token);
// Generate help-text.json
try {
2023-09-13 01:17:31 -05:00
console.log(`Generating ${commands.length} help text entries.`);
2022-12-15 13:07:35 -06:00
2023-09-13 01:17:31 -05:00
let helpJSONString = '{\n';
2022-12-15 13:07:35 -06:00
2023-09-13 01:17:31 -05:00
for (let i = 0; i < commandsHelp.length; i++) {
helpJSONString += ` "${commandsHelp[i].name}": {\n`;
helpJSONString += ` "description": "${commandsHelp[i].description}",\n`;
helpJSONString += ' "options": {\n';
2023-09-13 01:17:31 -05:00
for (let j = 0; j < commandsHelp[i].options.length; j++) {
2023-09-13 01:17:31 -05:00
helpJSONString += ` "${commandsHelp[i].options[j].name}": {\n`;
helpJSONString += ` "description": "${commandsHelp[i].options[j].description}",\n`;
helpJSONString += ` "required": "${commandsHelp[i].options[j].required}",\n`;
helpJSONString += ' "choices": {\n';
2023-09-13 01:17:31 -05:00
if (typeof commandsHelp[i].options[j].choices !== 'undefined') {
for (let k = 0; k < commandsHelp[i].options[j].choices.length; k++) {
2023-09-13 01:17:31 -05:00
helpJSONString += ` "${commandsHelp[i].options[j].choices[k].name}": {\n`;
helpJSONString += ` "value": "${commandsHelp[i].options[j].choices[k].value}",\n`;
helpJSONString += ' },\n';
2023-09-13 01:17:31 -05:00
}
}
2023-09-13 01:17:31 -05:00
helpJSONString += ' },\n';
helpJSONString += ' },\n';
2023-09-13 01:17:31 -05:00
}
2023-09-13 01:17:31 -05:00
helpJSONString += ' },\n';
helpJSONString += ' },\n';
}
2023-09-13 01:17:31 -05:00
helpJSONString += '}';
2023-09-13 01:17:31 -05:00
// Lazy way out of removing trailing commas
// See https://stackoverflow.com/a/34347475
const helpJSON = JSON.stringify(JSON.parse(helpJSONString.replace(/,(?!\s*?[{["'\w])/g, '')), null, 4);
2023-09-13 01:17:31 -05:00
// Create data folder if it doesn't exist
if (!fs.existsSync('data')) {
fs.mkdirSync('data');
}
2023-09-13 01:17:31 -05:00
// Write file to disk
fs.writeFile('./data/help-text.json', helpJSON, err => {
if (err) {
console.log(`Unable to write help-text.json: ${err}`);
console.log('Stopping...');
return;
}
});
2023-09-13 01:17:31 -05:00
console.log(`Successfully generated ${commandsHelp.length} help text entries.`);
2023-09-13 01:17:31 -05:00
console.log();
2023-09-13 01:17:31 -05:00
// Update slash commands on Discord's side
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
2023-09-13 01:17:31 -05:00
// The put method is used to fully refresh all commands
const data = await rest.put(
Routes.applicationCommands(clientId),
{ body: commands },
);
2023-09-13 01:17:31 -05:00
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
}
catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
})();
}
catch (error) {
2023-09-13 01:17:31 -05:00
// And of course, make sure you catch and log any errors!
console.error(error);
}