eximiabots-radiox/src/client/commands.js

71 lines
2.9 KiB
JavaScript
Raw Normal View History

const { SlashCommandBuilder } = require('@discordjs/builders');
2021-08-18 22:50:56 +00:00
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
2021-08-21 14:55:10 +00:00
const { token, version } = require('../config.js');
2021-08-18 22:50:56 +00:00
const fs = require('fs');
const path = require ('path');
module.exports = {
2021-08-21 14:55:10 +00:00
async execute(client) {
2021-08-18 22:50:56 +00:00
const commands = [];
const commandFiles = fs.readdirSync(path.join("./src/client/commands")).filter(f => f.endsWith(".js"));
for (const file of commandFiles) {
2021-08-21 14:55:10 +00:00
const command = require(`./commands/${file}`);
command.data = new SlashCommandBuilder()
.setName(command.name)
.setDescription(command.description);
2021-09-09 09:44:50 +00:00
2021-08-26 18:50:15 +00:00
command.data = command.data.toJSON();
if(command.options) {
command.options.forEach(function(option) {
if(option.type == "STRING") option.type = 3;
if(option.type == "NUMBER") option.type = 10;
2021-08-26 18:50:15 +00:00
command.data.options.push(option);
});
}
commands.push(command.data);
2021-08-18 22:50:56 +00:00
}
const rest = new REST({ version: '9' }).setToken(token);
(async () => {
2021-08-21 14:55:10 +00:00
try {
2021-08-22 17:49:11 +00:00
client.funcs.logger('Slash Commands', 'Started refreshing application (/) commands.');
2021-08-21 14:55:10 +00:00
if(version.includes("-dev")){
await rest.put(
Routes.applicationCommands(client.user.id),
{ body: [] },
);
let guilds = await client.guilds.fetch();
guilds.forEach(async guild => {
try {
await rest.put(
Routes.applicationGuildCommands(client.user.id, guild.id),
2021-09-05 01:41:53 +00:00
{ body: commands }
2021-08-21 14:55:10 +00:00
);
2021-09-05 01:41:53 +00:00
client.funcs.logger('Slash Commands', 'Guild Applications Successful' + "\n" + guild.id + " / " + guild.name);
2021-08-21 14:55:10 +00:00
} catch (DiscordAPIError) {
2021-09-05 01:41:53 +00:00
client.funcs.logger('Slash Commands', 'Guild Applications Failed' + "\n" + guild.id + " / " + guild.name);
if(DiscordAPIError.name != "DiscordAPIError[50001]") console.error(DiscordAPIError.message + "\n\n");
2021-08-21 14:55:10 +00:00
}
});
} else {
await rest.put(
Routes.applicationCommands(client.user.id),
2021-09-05 01:41:53 +00:00
{ body: commands }
2021-08-21 14:55:10 +00:00
);
}
2021-08-22 17:49:11 +00:00
client.funcs.logger('Slash Commands', 'Successfully reloaded application (/) commands.' + "\n");
2021-08-21 14:55:10 +00:00
} catch (error) {
2021-08-22 17:49:11 +00:00
client.funcs.logger('Slash Commands', 'Reloading application (/) commands failed.' + "\n");
2021-08-21 14:55:10 +00:00
console.error(error);
}
2021-08-18 22:50:56 +00:00
})();
}
2021-09-09 09:44:50 +00:00
}