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/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 28b969b..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": { @@ -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-api-types": "^0.37.2", + "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 7b5779b..248aae3 100644 --- a/src/handlers/Command.js +++ b/src/handlers/Command.js @@ -1,12 +1,11 @@ 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"); 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,28 @@ 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 => { 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 +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 daf18d0..a1221e4 100644 --- a/test/index.js +++ b/test/index.js @@ -7,17 +7,18 @@ const client = new Discord.Client({ const handler = new OPCommands(client, { commandsDir: "commands", eventsDir: "events", - testGuildID: "GUILD_ID", + testGuildID: "TEST_GUILD_ID", testMode: false, logs: true, notifyOwner: true, }); -handler.setBotOwner("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(", ")}**`), 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");