From 2c44a552d69afb5910f92f92e923fdc73169216b Mon Sep 17 00:00:00 2001 From: baz Date: Sat, 18 Nov 2023 20:27:02 +0000 Subject: [PATCH] Add command to reload commands --- commands/testing/reload.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 commands/testing/reload.js diff --git a/commands/testing/reload.js b/commands/testing/reload.js new file mode 100644 index 0000000..bf4836a --- /dev/null +++ b/commands/testing/reload.js @@ -0,0 +1,31 @@ +const { SlashCommandBuilder } = require('discord.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('reload') + .setDescription('Reload a command.') + .addStringOption(option => + option.setName('command') + .setDescription('The command to reload.') + .setRequired(true)), + async execute(interaction) { + const commandName = interaction.options.getString('command', true).toLowerCase(); + const command = interaction.client.commands.get(commandName); + + if (!command) { + return interaction.reply(`There is no command with name \`${commandName}\`!`); + } + + delete require.cache[require.resolve(`./${command.data.name}.js`)]; + + try { + interaction.client.commands.delete(command.data.name); + const newCommand = require(`./${command.data.name}.js`); + interaction.client.commands.set(newCommand.data.name, newCommand); + await interaction.reply(`Command \`${newCommand.data.name}\` was reloaded!`); + } catch (error) { + console.error(error); + await interaction.reply(`There was an error while reloading a command \`${command.data.name}\`:\n\`${error.message}\``); + } + }, +}; \ No newline at end of file