From e6bf4045ed25cddd820b1e5dd4fa091d480026be Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Mon, 4 Apr 2022 16:46:44 +0200 Subject: [PATCH] Add rename custom command feature --- commands/misc/custom.js | 5 +++++ util/renameCustomCommand.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 util/renameCustomCommand.js diff --git a/commands/misc/custom.js b/commands/misc/custom.js index 04c34a0..87fe6f3 100644 --- a/commands/misc/custom.js +++ b/commands/misc/custom.js @@ -2,6 +2,7 @@ const addCustomCommand = require("../../util/addCustomCommand"); const deleteCustomCommand = require("../../util/deleteCustomCommand"); const getAllCustomCommands = require("../../util/getAllCustomCommands"); const getOwnerOfCustomCommand = require("../../util/getOwnerOfCustomCommand"); +const renameCustomCommand = require("../../util/renameCustomCommand"); const Discord = require('discord.js'); const fs = require('fs'); const editCustomCommand = require("../../util/editCustomCommand"); @@ -11,6 +12,7 @@ module.exports = { description: "Manage custom commands, see help custom for more", moreHelp: ["custom add - Add new custom commands", "custom edit - Edit an existing command that you own", + "custom rename - Rename an existing command that you own", "custom show - Show custom message unformatted.", "custom remove - Delete your custom commands.", "custom owner - check owner of custom command", @@ -81,6 +83,9 @@ module.exports = { } }); break; + case "rename": + sendText = renameCustomCommand(customName, args[2], message.author.id); + break; default: sendText = `Argument not recognized.\n"${prefix}help custom" to see all arguments you can use.` break; diff --git a/util/renameCustomCommand.js b/util/renameCustomCommand.js new file mode 100644 index 0000000..540c41d --- /dev/null +++ b/util/renameCustomCommand.js @@ -0,0 +1,33 @@ +const fs = require('fs') +module.exports = function(customName, newCustomName, author){ + const customPath = './data/customCommands.json'; + let sendText = "This custom command does not exist."; + let json = fs.readFileSync(customPath, 'utf8'); + let customCommands = JSON.parse(json) + let alreadyExists = false; + let commandExists = false; + customCommands.forEach(function (customCommand) { + if(customCommand.customName === customName){ + commandExists = true; + } + if (customCommand.customName === newCustomName) { + alreadyExists = true; + sendText = "Can't rename this custom command. A custom command with that name already exists."; + } + }); + if (!alreadyExists && commandExists){ + customCommands.forEach(function (customCommand) { + if (customCommand.customName === customName) { + if(customCommand.author === author){ + customCommand.customName = newCustomName; + fs.writeFileSync(customPath, JSON.stringify(customCommands, null, 4)) + sendText = `${customName} has been renamed to ${newCustomName}.`; + }else { + sendText = "You do not own this custom command.\nOnly the one who created the custom command can rename it." + } + } + }); + } + + return sendText; +} \ No newline at end of file