mirror of
https://github.com/TheShadowEevee/Konpeki-Discord-Bot.git
synced 2025-01-11 14:38:49 -06:00
Generate a help text file when deploying commands
This commit is contained in:
parent
25eca4ed60
commit
651125d18c
2 changed files with 81 additions and 16 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,4 +1,5 @@
|
||||||
config.json
|
config.json
|
||||||
presence.json
|
presence.json
|
||||||
|
|
||||||
node_modules
|
node_modules
|
||||||
|
data
|
||||||
|
|
|
@ -10,6 +10,8 @@ const { clientId, token } = require('./config.json');
|
||||||
const fs = require('node:fs');
|
const fs = require('node:fs');
|
||||||
|
|
||||||
const commands = [];
|
const commands = [];
|
||||||
|
const commandsHelp = [];
|
||||||
|
|
||||||
// Grab all the command files from the commands directory you created earlier
|
// Grab all the command files from the commands directory you created earlier
|
||||||
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
|
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
|
||||||
|
|
||||||
|
@ -17,26 +19,88 @@ const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('
|
||||||
for (const file of commandFiles) {
|
for (const file of commandFiles) {
|
||||||
const command = require(`./commands/${file}`);
|
const command = require(`./commands/${file}`);
|
||||||
commands.push(command.data.toJSON());
|
commands.push(command.data.toJSON());
|
||||||
|
commandsHelp.push(command.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construct and prepare an instance of the REST module
|
// Construct and prepare an instance of the REST module
|
||||||
const rest = new REST({ version: '10' }).setToken(token);
|
const rest = new REST({ version: '10' }).setToken(token);
|
||||||
|
|
||||||
// and deploy your commands!
|
// Generate help-text.json
|
||||||
(async () => {
|
try {
|
||||||
try {
|
console.log(`Generating ${commands.length} help text entries.`);
|
||||||
console.log(`Started refreshing ${commands.length} application (/) commands.`);
|
|
||||||
|
|
||||||
// The put method is used to fully refresh all commands
|
let helpJSONString = '{\n';
|
||||||
const data = await rest.put(
|
|
||||||
Routes.applicationCommands(clientId),
|
|
||||||
{ body: commands },
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
|
for (let i = 0; i < commandsHelp.length; i++) {
|
||||||
|
helpJSONString += ` "${commandsHelp[i].name}": {\n`;
|
||||||
|
helpJSONString += ` "description": "${commandsHelp[i].description}",\n`;
|
||||||
|
helpJSONString += ' "options": {\n';
|
||||||
|
|
||||||
|
for (let j = 0; j < commandsHelp[i].options.length; j++) {
|
||||||
|
|
||||||
|
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';
|
||||||
|
|
||||||
|
if (typeof commandsHelp[i].options[j].choices !== 'undefined') {
|
||||||
|
for (let k = 0; k < commandsHelp[i].options[j].choices.length; k++) {
|
||||||
|
|
||||||
|
helpJSONString += ` "${commandsHelp[i].options[j].choices[k].name}": {\n`;
|
||||||
|
helpJSONString += ` "value": "${commandsHelp[i].options[j].choices[k].value}",\n`;
|
||||||
|
helpJSONString += ' },\n';
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
helpJSONString += ' },\n';
|
||||||
|
helpJSONString += ' },\n';
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
helpJSONString += ' },\n';
|
||||||
|
helpJSONString += ' },\n';
|
||||||
}
|
}
|
||||||
catch (error) {
|
|
||||||
// And of course, make sure you catch and log any errors!
|
helpJSONString += '}';
|
||||||
console.error(error);
|
|
||||||
}
|
// 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);
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`Successfully generated ${commandsHelp.length} help text entries.`);
|
||||||
|
|
||||||
|
console.log();
|
||||||
|
|
||||||
|
// Update slash commands on Discord's side
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
console.log(`Started refreshing ${commands.length} application (/) commands.`);
|
||||||
|
|
||||||
|
// The put method is used to fully refresh all commands
|
||||||
|
const data = await rest.put(
|
||||||
|
Routes.applicationCommands(clientId),
|
||||||
|
{ body: commands },
|
||||||
|
);
|
||||||
|
|
||||||
|
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) {
|
||||||
|
// And of course, make sure you catch and log any errors!
|
||||||
|
console.error(error);
|
||||||
|
}
|
Loading…
Reference in a new issue