Skip to content

Port to Discord.JS v14 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ build/Release
node_modules/
jspm_packages/

# Environment variables
test/config.json

# TypeScript v1 declaration files
typings/

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
}
Expand Down
2 changes: 1 addition & 1 deletion src/OPCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 18 additions & 11 deletions src/handlers/Command.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
Expand All @@ -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.");
Expand Down Expand Up @@ -71,7 +66,6 @@ class CommandHandler {
})();
}
});

const cooldowns = new Discord.Collection();
_this.client.on("interactionCreate", (interaction) => {
if (!interaction.isCommand()) return;
Expand Down Expand Up @@ -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);
Expand All @@ -125,5 +133,4 @@ class CommandHandler {
});
}
}

module.exports = CommandHandler;
module.exports = CommandHandler;
7 changes: 4 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");