diff --git a/commands/custom.js b/commands/custom.js new file mode 100644 index 0000000..875b471 --- /dev/null +++ b/commands/custom.js @@ -0,0 +1,60 @@ +const addCustomCommand = require("../util/addCustomCommand"); +const deleteCustomCommand = require("../util/deleteCustomCommand"); +const getAllCustomCommands = require("../util/getAllCustomCommands"); +const getOwnerOfCustomCommand = require("../util/getOwnerOfCustomCommand"); +const Discord = require('discord.js'); + +module.exports = { + name: 'custom', + description: 'Add custom commands, see help custom for more', + moreHelp: ["custom add - Add new custom commands", + "custom remove - Delete your custom commands.", + "custom owner - check owner of custom command", + "custom list - list all custom commands" +], + execute({message, args, client}) { + let sendText; + if (args){ + let customName = args[1]; + let customMessage = args.slice(2, args.length).join(" "); + switch (args[0].toLowerCase()) { + case "add": + sendText = addCustomCommand(args[0], customMessage, message.author.id); + break; + case "remove": + case "delete": + sendText = deleteCustomCommand(customName, message.author.id); + break; + case "owner": + let author = getOwnerOfCustomCommand(customName); + let user; + if(!author) + sendText = `${customName} does not exist` + else{ + client.guilds.cache.each(guild => { + user = guild.members.cache.get(author); + }); + sendText = `The owner of ${customName} is ${user.user.username}` + } + + break; + case "list": + const embed = new Discord.MessageEmbed(); + sendText = getAllCustomCommands(); + if(sendText != ""){ + embed.setColor(15780145) + embed.addFields( + { name: "Custom commands", value: sendText }, + ) + sendText = embed + }else sendText = "NO CUSTOM COMMANDS" + break; + default: + sendText = "Argument not recognized." + break; + } + } + + message.channel.send(sendText); + } +}; \ No newline at end of file diff --git a/server/message.js b/server/message.js index ad938df..9eaf5d1 100644 --- a/server/message.js +++ b/server/message.js @@ -1,3 +1,5 @@ +const fs = require('fs'); + module.exports = function(client, owners, message, globalPrefix){ let prefix = globalPrefix; if (client.serverPrefixes.get(message.guild.id)) { @@ -17,6 +19,17 @@ module.exports = function(client, owners, message, globalPrefix){ }catch(e){ console.log(e) } + return; + } + const customPath = './data/customCommands.json'; + if(fs.existsSync(customPath)){ + let json = fs.readFileSync(customPath, 'utf8'); + let customCommands = JSON.parse(json) + customCommands.forEach(function (customCommand) { + if (customCommand.customName === commandName) { + message.channel.send(customCommand.customMessage) + } + }); } return; } diff --git a/util/addCustomCommand.js b/util/addCustomCommand.js new file mode 100644 index 0000000..17fefe7 --- /dev/null +++ b/util/addCustomCommand.js @@ -0,0 +1,28 @@ +const fs = require('fs'); +module.exports = function(customName, customMessage, author){ + let sendText; + const customPath = './data/customCommands.json'; + if(!fs.existsSync(customPath)){ + fs.writeFileSync(customPath,"[]") + } + + let json = fs.readFileSync(customPath, 'utf8'); + let customCommands = JSON.parse(json) + + let isExists = false; + customCommands.forEach(function (customCommand) { + if (customCommand.customName === customName) { + sendText = "This custom command already exists"; + isExists = true; + } + }); + if (!isExists) { + let newCustomCommand = { + "customName": customName, "customMessage": customMessage, "author": author + } + customCommands.push(newCustomCommand) + sendText = "New custom command has been added." + } + fs.writeFileSync(customPath, JSON.stringify(customCommands, null, 4)) + return sendText; +} \ No newline at end of file diff --git a/util/deleteCustomCommand.js b/util/deleteCustomCommand.js new file mode 100644 index 0000000..89a3368 --- /dev/null +++ b/util/deleteCustomCommand.js @@ -0,0 +1,23 @@ +const fs = require('fs') +module.exports = function(customName, 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 index = 0; + customCommands.forEach(function (customCommand) { + if (customCommand.customName === customName) { + if(customCommand.author === author){ + customCommands.splice(index, 1) + sendText = `The custom command "${customName}" has been deleted.` + fs.writeFileSync(customPath, JSON.stringify(customCommands, null, 4)) + }else { + sendText = "You do not own this custom command.\nOnly the one who created the custom command can delete it." + } + } + index++ + }); + + + return sendText; +} \ No newline at end of file diff --git a/util/getAllCustomCommands.js b/util/getAllCustomCommands.js new file mode 100644 index 0000000..fba3b28 --- /dev/null +++ b/util/getAllCustomCommands.js @@ -0,0 +1,13 @@ +const fs = require('fs'); +module.exports = function(){ + const customPath = './data/customCommands.json'; + let list = ""; + + let json = fs.readFileSync(customPath, 'utf8'); + let customCommands = JSON.parse(json) + customCommands.forEach(function (customCommand) { + list = list + customCommand.customName+"\n"; + }); + + return list; +} \ No newline at end of file diff --git a/util/getOwnerOfCustomCommand.js b/util/getOwnerOfCustomCommand.js new file mode 100644 index 0000000..bdcf156 --- /dev/null +++ b/util/getOwnerOfCustomCommand.js @@ -0,0 +1,15 @@ +const fs = require('fs'); +module.exports = function(customName){ + let author = false; + const customPath = './data/customCommands.json'; + let json = fs.readFileSync(customPath, 'utf8'); + let customCommands = JSON.parse(json) + customCommands.forEach(function (customCommand) { + if (customCommand.customName === customName) { + author = customCommand.author; + } + }); + + return author; + +} \ No newline at end of file