From fdd4521290789d29e1a26150bf25aaaf51f7ace5 Mon Sep 17 00:00:00 2001 From: TehMartinXz <46550675+TehMartinXz@users.noreply.github.com> Date: Sun, 14 Aug 2022 17:21:53 -0400 Subject: [PATCH 1/6] Apply @MilleniumModsSources's patches Further improvements to the notifyOwner system. - Improved the notification message when running Administrator commands by adding an extra option to customize the notifyCommandMessage code. - Add the notifyCommandMessage for notifications when an Administrator command is executed. How it works: If a command that requires Administrator permissions is executed, and the notifyOwner option is enabled, the notifyCommandMessage will be executed. However, if the notifyCommandMessage option is missing, the bot will simply send the default message which is set in /src/Command.js --- src/handlers/Command.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/handlers/Command.js b/src/handlers/Command.js index 7b5779b..8ef85d1 100644 --- a/src/handlers/Command.js +++ b/src/handlers/Command.js @@ -6,7 +6,6 @@ const ms = require("ms"); const fs = require("fs"); const path = require("path"); const { time } = require("console"); - /** * Command Handler * @class @@ -18,13 +17,10 @@ class CommandHandler { if (!this) throw new Error("[OPCommands] Internal error: missing _this parameter on Command Handler."); if (!commandsDir) throw new Error("[OPCommands] Internal error: missing eventsDir parameter on Command Handler."); if (!fs.existsSync(commandsDir)) throw new Error("[OPCommands] Unexisting command directory."); - _this.client.commands = new Discord.Collection(); const files = fs.readdirSync(commandsDir).filter(file => file.endsWith(".js")); const commands = []; - if (_this.options.logs) console.log("[OPCommands] Loaded " + files.length + " commands."); - for (const file of files) { const commandFile = require(path.join(require.main.path, commandsDir, file)); _this.client.commands.set(commandFile.name, commandFile); @@ -35,7 +31,6 @@ class CommandHandler { if (commandFile.options) Object.assign(commandObj, {options: commandFile.options}); commands.push(JSON.stringify(commandObj)); }; - _this.client.on("ready", async () => { const rest = new REST({ version: "9" }).setToken(_this.client.token); if (_this.options.notifyOwner && !_this.client.msgs.notifyOwnerMessage) throw new Error("[OPCommands] Missing notifyOwnerMessage parameter."); @@ -71,7 +66,6 @@ class CommandHandler { })(); } }); - const cooldowns = new Discord.Collection(); _this.client.on("interactionCreate", (interaction) => { if (!interaction.isCommand()) return; @@ -109,14 +103,20 @@ class CommandHandler { }; }; }; - try { if (_this.options.logs) console.log(`[OPCommands] Command '${interaction.commandName}' executed by: '${interaction.user.tag}'`); _this.client.commands.get(interaction.commandName).run(_this.client, interaction); if (_this.options.notifyOwner && (commandFile.limits.permissions == ('ADMINISTRATOR').toLowerCase())) { + if(!_this.client.msgs.notifyCommandMessage) { + // If there isn't any message, it uses the default one _this.client.users.fetch(_this.client.owners[0]).then(user => { user.send("[Logs] Administrator command `" + interaction.commandName + "` was executed by " + `<@${interaction.user.id}> in **${interaction.guild.name}**`); }); + } else { + _this.client.users.fetch(_this.client.owners[0]).then(user => { + _this.client.msgs.notifyCommandMessage(user, interaction); + }) + } } } catch (e) { if (_this.options.logs) console.log("[OPCommands] Command error: " + interaction.commandName); @@ -125,5 +125,4 @@ class CommandHandler { }); } } - module.exports = CommandHandler; From b0e8535c274eb43d56c76f8453fe82d62af038b6 Mon Sep 17 00:00:00 2001 From: TehMartinXz <46550675+TehMartinXz@users.noreply.github.com> Date: Sun, 14 Aug 2022 17:24:05 -0400 Subject: [PATCH 2/6] Update index.js --- test/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/index.js b/test/index.js index daf18d0..69b548e 100644 --- a/test/index.js +++ b/test/index.js @@ -17,7 +17,8 @@ handler.setMessages({ ownerOnly: (interaction) => interaction.reply("Missing **Bot Owner** permission."), permissions: (interaction, perms) => interaction.reply(`You are missing the following permissions: **${perms.join(", ")}**`), cooldown: (interaction, cooldown) => interaction.reply(`You must wait **${cooldown}** before executing another command.`), - notifyOwnerMessage: (owner) => owner.send("I'm online!"); + notifyOwnerMessage: (owner) => owner.send("I'm online!"), + notifyCommandMessage: (owner, interaction) => owner.send("[OPCommands] Administrator command `" + interaction.commandName + "` was executed by " + `<@${interaction.user.id}> in **${interaction.guild.name}**`) }); client.login("BOT_TOKEN"); From 4269978a802de4ad2f34fe55d8ea141b0bf6a45e Mon Sep 17 00:00:00 2001 From: TehMartinXz <46550675+TehMartinXz@users.noreply.github.com> Date: Sun, 14 Aug 2022 21:29:55 -0400 Subject: [PATCH 3/6] Fixes an issue with notifyOwner system The issue was that if for some reason, the permission at command.limits weren't exactly "administrator", then the notification wouldn't be sent. So now it supports both objects (For when using multiple permissions even though this system only works with Administrator) and toAnyCase strings. And yes, I know that I used 2 ".toLowerCase" when I could've just made it .toUpperCase or make the "ADMINISTRATOR" string lowercase, but. I don't care :D, it's for memes --- src/handlers/Command.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/handlers/Command.js b/src/handlers/Command.js index 8ef85d1..49ce073 100644 --- a/src/handlers/Command.js +++ b/src/handlers/Command.js @@ -106,7 +106,15 @@ class CommandHandler { try { if (_this.options.logs) console.log(`[OPCommands] Command '${interaction.commandName}' executed by: '${interaction.user.tag}'`); _this.client.commands.get(interaction.commandName).run(_this.client, interaction); - if (_this.options.notifyOwner && (commandFile.limits.permissions == ('ADMINISTRATOR').toLowerCase())) { + + let commandFile_perms + if(typeof commandFile.limits.permissions == 'object') { + commandFile_perms = JSON.stringify(commandFile.limits.permissions) + } else { + commandFile_perms = commandFile.limits.permissions + } + + if (_this.options.notifyOwner && (commandFile_perms.toLowerCase().includes(('ADMINISTRATOR').toLowerCase()))) { if(!_this.client.msgs.notifyCommandMessage) { // If there isn't any message, it uses the default one _this.client.users.fetch(_this.client.owners[0]).then(user => { From ba311a405231f565ce8cb8c81d862f3ebcf6fc85 Mon Sep 17 00:00:00 2001 From: MilleniumModsSources Date: Sun, 14 Aug 2022 21:55:24 -0400 Subject: [PATCH 4/6] Fix this --- .gitignore | 3 +++ package.json | 4 ++-- src/OPCommands.js | 2 +- src/handlers/Command.js | 12 ++++++++++-- test/index.js | 9 +++++++++ 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 6704566..97d2fd7 100644 --- a/.gitignore +++ b/.gitignore @@ -41,6 +41,9 @@ build/Release node_modules/ jspm_packages/ +# Environment variables +test/config.json + # TypeScript v1 declaration files typings/ diff --git a/package.json b/package.json index 28b969b..426542a 100644 --- a/package.json +++ b/package.json @@ -21,12 +21,12 @@ "repository": "github:LukeIsHereToDevelop/opcommands", "license": "GPL-3.0-or-later", "engines": { - "node": "^16.6.0" + "node": "^16.9.0" }, "dependencies": { "@discordjs/rest": "^0.1.0-canary.0", "discord-api-types": "^0.22.0", - "discord.js": "^13.0.1", + "discord.js": "^14.2.0", "humanize-ms": "^1.2.1", "ms": "^2.1.3" } diff --git a/src/OPCommands.js b/src/OPCommands.js index 8220983..1751d60 100644 --- a/src/OPCommands.js +++ b/src/OPCommands.js @@ -5,7 +5,7 @@ const CommandHandler = require("./handlers/Command.js"); /** * The main class of OPCommands. * @class - * @requires [Node.js v16.6.0](https://nodejs.org/en/download/current/) & Discord.js v13 + * @requires [Node.js v16.9.0](https://nodejs.org/en/download/current/) & Discord.js v14 * @param {Discord.Client} client * @param {Object} options * @example diff --git a/src/handlers/Command.js b/src/handlers/Command.js index 8ef85d1..0d04817 100644 --- a/src/handlers/Command.js +++ b/src/handlers/Command.js @@ -106,7 +106,15 @@ class CommandHandler { try { if (_this.options.logs) console.log(`[OPCommands] Command '${interaction.commandName}' executed by: '${interaction.user.tag}'`); _this.client.commands.get(interaction.commandName).run(_this.client, interaction); - if (_this.options.notifyOwner && (commandFile.limits.permissions == ('ADMINISTRATOR').toLowerCase())) { + + let commandFile_perms + if(typeof commandFile.limits.permissions == 'object') { + commandFile_perms = JSON.stringify(commandFile.limits.permissions) + } else { + commandFile_perms = commandFile.limits.permissions + } + + if (_this.options.notifyOwner && (commandFile_perms.toLowerCase().includes(('ADMINISTRATOR').toLowerCase()))) { if(!_this.client.msgs.notifyCommandMessage) { // If there isn't any message, it uses the default one _this.client.users.fetch(_this.client.owners[0]).then(user => { @@ -125,4 +133,4 @@ class CommandHandler { }); } } -module.exports = CommandHandler; +module.exports = CommandHandler; \ No newline at end of file diff --git a/test/index.js b/test/index.js index 69b548e..7e2ace5 100644 --- a/test/index.js +++ b/test/index.js @@ -1,3 +1,12 @@ +/* + + FOR TESTING PURPOSES ONLY + +*/ + +const config = require('./config.json'); + + const OPCommands = require("../src/OPCommands.js"); const Discord = require("discord.js"); const client = new Discord.Client({ From 6390bc16e58fcdcf67eff6de2771c036c36bc12d Mon Sep 17 00:00:00 2001 From: MilleniumModsSources Date: Sun, 14 Aug 2022 22:58:42 -0400 Subject: [PATCH 5/6] Initial support to D.js v14 --- README.md | 4 ++-- package.json | 4 ++-- src/handlers/Command.js | 2 +- test/index.js | 7 ++++--- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d5b80b7..5bb8c16 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,8 @@ OPCommands is an advanced Discord.js Command Handler that supports slash command ## Requirements -* Discord.js v13+ -* Node.js v16.6.0+ +* Discord.js v14+ +* Node.js v16.9.0+ ## Installation diff --git a/package.json b/package.json index 426542a..b3b898d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "opcommands", - "version": "1.2.0", + "version": "1.3.0", "description": "An advanced Discord.js Command Handler that supports slash commands and more!", "main": "src/OPCommands.js", "scripts": { @@ -25,7 +25,7 @@ }, "dependencies": { "@discordjs/rest": "^0.1.0-canary.0", - "discord-api-types": "^0.22.0", + "discord-api-types": "^0.37.2", "discord.js": "^14.2.0", "humanize-ms": "^1.2.1", "ms": "^2.1.3" diff --git a/src/handlers/Command.js b/src/handlers/Command.js index 0d04817..248aae3 100644 --- a/src/handlers/Command.js +++ b/src/handlers/Command.js @@ -1,6 +1,6 @@ const Discord = require("discord.js"); const { REST } = require('@discordjs/rest'); -const { Routes } = require('discord-api-types/v9'); +const { Routes } = require('discord-api-types/v10'); const hms = require("humanize-ms"); const ms = require("ms"); const fs = require("fs"); diff --git a/test/index.js b/test/index.js index 7e2ace5..a821463 100644 --- a/test/index.js +++ b/test/index.js @@ -1,6 +1,7 @@ /* FOR TESTING PURPOSES ONLY + ALL CONFIG-BASED VARIABLES WILL BE RESET ONCE THE CODE IS TESTED AND PROVEN TO WORK */ @@ -16,12 +17,12 @@ const client = new Discord.Client({ const handler = new OPCommands(client, { commandsDir: "commands", eventsDir: "events", - testGuildID: "GUILD_ID", + testGuildID: config.test_server, testMode: false, logs: true, notifyOwner: true, }); -handler.setBotOwner("OWNER_ID"); +handler.setBotOwner(config.owner_id); handler.setMessages({ ownerOnly: (interaction) => interaction.reply("Missing **Bot Owner** permission."), permissions: (interaction, perms) => interaction.reply(`You are missing the following permissions: **${perms.join(", ")}**`), @@ -30,4 +31,4 @@ handler.setMessages({ notifyCommandMessage: (owner, interaction) => owner.send("[OPCommands] Administrator command `" + interaction.commandName + "` was executed by " + `<@${interaction.user.id}> in **${interaction.guild.name}**`) }); -client.login("BOT_TOKEN"); +client.login(config.token); From 2eddd034b72536e84845388160b668c836675c8e Mon Sep 17 00:00:00 2001 From: MilleniumModsSources Date: Mon, 15 Aug 2022 20:09:53 -0400 Subject: [PATCH 6/6] Remove testing variables --- test/index.js | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/test/index.js b/test/index.js index a821463..a1221e4 100644 --- a/test/index.js +++ b/test/index.js @@ -1,13 +1,3 @@ -/* - - FOR TESTING PURPOSES ONLY - ALL CONFIG-BASED VARIABLES WILL BE RESET ONCE THE CODE IS TESTED AND PROVEN TO WORK - -*/ - -const config = require('./config.json'); - - const OPCommands = require("../src/OPCommands.js"); const Discord = require("discord.js"); const client = new Discord.Client({ @@ -17,12 +7,12 @@ const client = new Discord.Client({ const handler = new OPCommands(client, { commandsDir: "commands", eventsDir: "events", - testGuildID: config.test_server, + testGuildID: "TEST_GUILD_ID", testMode: false, logs: true, notifyOwner: true, }); -handler.setBotOwner(config.owner_id); +handler.setBotOwner("BOT_OWNER_ID"); handler.setMessages({ ownerOnly: (interaction) => interaction.reply("Missing **Bot Owner** permission."), permissions: (interaction, perms) => interaction.reply(`You are missing the following permissions: **${perms.join(", ")}**`), @@ -31,4 +21,4 @@ handler.setMessages({ notifyCommandMessage: (owner, interaction) => owner.send("[OPCommands] Administrator command `" + interaction.commandName + "` was executed by " + `<@${interaction.user.id}> in **${interaction.guild.name}**`) }); -client.login(config.token); +client.login("BOT_TOKEN");