From 9bb9effaf7745ce1cd1b1a2b4271bfbdcaafaa12 Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Sun, 4 Jul 2021 12:48:06 +0200 Subject: [PATCH] Add custom variables to custom message feature --- commands/custom.js | 6 +++++- server/message.js | 3 ++- util/customReplaceWithVariables.js | 23 +++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 util/customReplaceWithVariables.js diff --git a/commands/custom.js b/commands/custom.js index 56881aa..5b01be2 100644 --- a/commands/custom.js +++ b/commands/custom.js @@ -10,7 +10,8 @@ module.exports = { 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" + "custom list - list all custom commands", + "custom variables - list all variables you can use" ], execute({message, args, client}) { const customPath = './data/customCommands.json'; @@ -53,6 +54,9 @@ module.exports = { sendText = embed }else sendText = "NO CUSTOM COMMANDS" break; + case "variables": + sendText = "The variables you can use are:\n\n\n\n\n\n\n\n" + break; default: sendText = "Argument not recognized." break; diff --git a/server/message.js b/server/message.js index e729d13..04d4603 100644 --- a/server/message.js +++ b/server/message.js @@ -27,7 +27,8 @@ module.exports = function(client, owners, message, globalPrefix){ let customCommands = JSON.parse(json) customCommands.forEach(function (customCommand) { if (customCommand.customName === commandName) { - message.channel.send(customCommand.customMessage) + let customMessage = customReplaceWithVariables(customCommand.customMessage, message, prefix, globalPrefix) + message.channel.send(customMessage) } }); } diff --git a/util/customReplaceWithVariables.js b/util/customReplaceWithVariables.js new file mode 100644 index 0000000..70dfdf2 --- /dev/null +++ b/util/customReplaceWithVariables.js @@ -0,0 +1,23 @@ +const getNickname = require('./getNickname') + +module.exports = function(customMessage, message, prefix, globalPrefix){ + + let user = message.guild.members.cache.get(message.author.id); + let nickname = getNickname(user, message.guild) + let username = user.user.username + let userID = user.user.id + let discriminator = user.user.discriminator + let guildName = message.guild.name + let guildID = message.guild.id + + let variables = ["", "", "", "", "", "", "", ""] + let replacer = [prefix, globalPrefix, username, nickname, userID, discriminator, guildName, guildID] + + + for (let i = 0; i < variables.length; i++){ + const regex = new RegExp(variables[i], 'g') + customMessage = customMessage.replace(regex, replacer[i]) + } + + return customMessage +} \ No newline at end of file