From 0b01712712ee74feb7d15ec4a34d58c50f2a56b5 Mon Sep 17 00:00:00 2001 From: Supositware Date: Wed, 21 Dec 2022 21:19:50 +0100 Subject: [PATCH] load/unload commands --- commands/owner/load.js | 22 ++++++++++++++++++ commands/owner/unload.js | 50 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 commands/owner/load.js create mode 100644 commands/owner/unload.js diff --git a/commands/owner/load.js b/commands/owner/load.js new file mode 100644 index 0000000..3031398 --- /dev/null +++ b/commands/owner/load.js @@ -0,0 +1,22 @@ +import { SlashCommandBuilder } from 'discord.js'; + +export default { + data: new SlashCommandBuilder() + .setName('load') + .setDescription('load a command.') + .addStringOption(option => + option.setName('file') + .setDescription('File location of the command.') + .setRequired(true)), + category: 'owner', + ownerOnly: true, + async execute(interaction, args, client) { + await interaction.deferReply(); + + let command = await import(`../../${args.file}`); + command = command.default; + + client.commands.set(command.data.name, command); + return await interaction.editReply(`${command.data.name} has been loaded.`); + }, +}; diff --git a/commands/owner/unload.js b/commands/owner/unload.js new file mode 100644 index 0000000..760035a --- /dev/null +++ b/commands/owner/unload.js @@ -0,0 +1,50 @@ +import { SlashCommandBuilder } from 'discord.js'; +import fs from 'node:fs'; + +export default { + data: new SlashCommandBuilder() + .setName('unload') + .setDescription('Unload a command and replace it with a placeholder') + .addStringOption(option => + option.setName('commandname') + .setDescription('The command to unload.') + .setRequired(true)) + .addStringOption(option => + option.setName('placeholder') + .setDescription('The placeholder message you want for the command.')) + .addBooleanOption(option => + option.setName('nofile') + .setDescription('Don\'t create the placeholder file')), + category: 'owner', + ownerOnly: true, + async execute(interaction, args, client) { + await interaction.deferReply(); + if (!client.commands.has(args.commandname)) return await interaction.editReply('Command not found.'); + if (!args.placeholder) args.placeholder = 'This command is unloaded, please check back later.'; + + if (!args.nofile) { + fs.writeFileSync(`./unloaded/${args.commandname}.js`, ` + import { SlashCommandBuilder } from 'discord.js'; + + export default { + data: ${JSON.stringify(client.commands.get(args.commandname).data)}, + category: '${client.commands.get(args.commandname).category}', + async execute(interaction) { + return interaction.reply('${args.placeholder}'); + }, + }; + + `); + } + + client.commands.delete(args.commandname); + if (!args.nofile) { + let command = await import(`../../unloaded/${args.commandname}.js`); + command = command.default; + + client.commands.set(args.commandname, command); + } + + return await interaction.editReply(`${args.commandname} has been unloaded.`); + }, +};