Created the command type `per-guild` these commands are registered when the bot enters a new guild, different of the global type, they can have permissions edited (the permissions system need to be developed yet). `guild` commands will be registered just in the development guild (probably the purpose of them need to be rethought). The reload command became adapted to be compatible with the command type system. When a command is reloaded, it's on all other guilds.
140 lines
3.8 KiB
JavaScript
140 lines
3.8 KiB
JavaScript
const { SlashCommandBuilder } = require('@discordjs/builders');
|
|
const { Routes } = require('discord-api-types/v9');
|
|
|
|
const fs = require('fs');
|
|
|
|
const console = require('../../functions/meta/console');
|
|
|
|
module.exports = {
|
|
data: new SlashCommandBuilder()
|
|
.setName('reload')
|
|
.setDescription('Reloads a command')
|
|
.addStringOption(option =>
|
|
option.setName('command')
|
|
.setDescription('The command name to be reloaded')
|
|
.setRequired(true)),
|
|
permissions: [
|
|
{
|
|
id: process.env.OWNER,
|
|
type: 'USER',
|
|
permission: true,
|
|
},
|
|
],
|
|
properties: {
|
|
type: [ 'guild', 'per-guild' ],
|
|
},
|
|
async execute(interaction, client) {
|
|
|
|
await interaction.deferReply({ ephemeral: true });
|
|
|
|
const { commands, commandsData, rest } = require('../../index');
|
|
|
|
const cmdName = interaction.options.getString('command').toLowerCase();
|
|
|
|
const command = commands.find(c => c.data.name === cmdName);
|
|
|
|
switch (command?.properties?.folder) {
|
|
case 'dev':
|
|
interaction.editReply({ content: `You can't reload \`${cmdName}\` because it is a development command.`, ephemeral: true });
|
|
return;
|
|
case undefined:
|
|
interaction.editReply({ content: `Command \`${cmdName}\` wasn't found.`, ephemeral: true });
|
|
return;
|
|
}
|
|
|
|
const cmdIdx = commands.findIndex(c => c.data.name === cmdName);
|
|
const dataIdx = commandsData.guild.findIndex(c => c.name === cmdName);
|
|
|
|
const oldCommand = command;
|
|
const oldCommandData = await commandsData.guild[dataIdx];
|
|
|
|
const folder = fs.readdirSync('./commands').find(f => fs.readdirSync(`./commands/${f}`).includes(`${cmdName}.js`));
|
|
|
|
delete require.cache[require.resolve(`../../commands/${folder}/${cmdName}.js`)];
|
|
|
|
try {
|
|
|
|
console.log(
|
|
'Old Command --------------------------------\n', oldCommand,
|
|
'\nData:\n', oldCommandData,
|
|
);
|
|
if(oldCommand.permissions) {
|
|
console.log('Permissions');
|
|
console.table(oldCommand.permissions);
|
|
}
|
|
|
|
const newCommand = require(`../../commands/${folder}/${cmdName}.js`);
|
|
|
|
const clientCommand = (await client.guilds.cache.get(process.env.DEV_GUILD)?.commands.fetch()).find(c => c.name === cmdName);
|
|
|
|
if(!newCommand.properties) newCommand.properties = { folder: folder };
|
|
else newCommand.properties.folder = folder;
|
|
|
|
commands[cmdIdx] = {
|
|
data: newCommand.data,
|
|
properties: newCommand.properties,
|
|
permissions: newCommand.permissions,
|
|
execute: newCommand.execute,
|
|
id: clientCommand.id,
|
|
name: clientCommand.name,
|
|
};
|
|
|
|
if(newCommand.properties.type.includes('guild')) {
|
|
|
|
commandsData.guild[dataIdx] = newCommand.data.toJSON();
|
|
|
|
await rest.put(
|
|
Routes.applicationGuildCommands(process.env.CLIENT, process.env.DEV_GUILD), {
|
|
body: commandsData.guild,
|
|
},
|
|
);
|
|
|
|
if(newCommand.permissions) {
|
|
await clientCommand.permissions.set({ id: clientCommand.id, permissions: newCommand.permissions });
|
|
}
|
|
|
|
}
|
|
|
|
const guilds = client.guilds.cache.map(g => g.id);
|
|
|
|
if(newCommand.properties.type.includes('per-guild')) {
|
|
|
|
commandsData.perGuild[dataIdx] = newCommand.data.toJSON();
|
|
|
|
for (const guild of guilds) {
|
|
|
|
if(guild == process.env.DEV_GUILD) continue;
|
|
|
|
await rest.put(
|
|
Routes.applicationGuildCommands(process.env.CLIENT, guild), {
|
|
body: commandsData.perGuild,
|
|
},
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log(
|
|
'\nNew Command --------------------------------\n', commands[cmdIdx],
|
|
'\nData:\n', commandsData.guild[dataIdx],
|
|
);
|
|
if(newCommand.permissions) {
|
|
console.log('Permissions');
|
|
console.table(newCommand.permissions);
|
|
}
|
|
|
|
console.table(commands);
|
|
|
|
interaction.editReply({ content: `Command \`${cmdName}\` reloaded on ${guilds.length} guilds.`, ephemeral: true });
|
|
|
|
}
|
|
catch (error) {
|
|
console.error(error);
|
|
await interaction.reply({
|
|
content: `A error occur while trying to reload the \`${cmdName}\` command.\n\`\`\`diff\n- ${error}\`\`\``,
|
|
ephemeral: true,
|
|
});
|
|
}
|
|
},
|
|
}; |