From e4e012bd723a02ad7fc83be77c3a656d75d5c0af Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Fri, 6 May 2022 12:22:49 +0300 Subject: [PATCH] misc fixes n cleanup --- index.js | 7 ++++--- src/structure/DiscordClient.js | 7 ++++--- src/structure/client/wrappers/InvokerWrapper.js | 12 ++++++++++-- src/structure/components/observers/CommandHandler.js | 10 ++++++++-- src/structure/interfaces/commands/Command.js | 2 +- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index c5407ef..09ceacc 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ const { version } = require('./package.json'); new BaseClient(options, version) .build(); -// process.on("unhandledRejection", (error) => { -// console.error("[index.js] Unhandled promise rejection:", error); //eslint-disable-line no-console -// }); \ No newline at end of file +process.on("unhandledRejection", (error) => { + console.error("[index.js] Unhandled promise rejection:", error); //eslint-disable-line no-console + throw error; +}); \ No newline at end of file diff --git a/src/structure/DiscordClient.js b/src/structure/DiscordClient.js index eea4dec..736d836 100644 --- a/src/structure/DiscordClient.js +++ b/src/structure/DiscordClient.js @@ -1,5 +1,6 @@ const { Client, Collection } = require('discord.js'); const chalk = require('chalk'); +const { inspect } = require('util'); const { Intercom, EventHooker, LocaleLoader, Registry, Dispatcher, Resolver, ModerationManager, RateLimiter } = require('./client/'); const { Observer, Command, Setting, Inhibitor } = require('./interfaces/'); @@ -67,9 +68,9 @@ class DiscordClient extends Client { // this.logger.error(`Uncaught exception:\n${err.stack || err}`); // }); - // process.on('unhandledRejection', (err) => { - // this.logger.error(`Unhandled rejection:\n${err.stack || err}`); - // }); + process.on('unhandledRejection', (err, reason) => { + this.logger.error(`Unhandled rejection:\n${err.stack || err}\n${inspect(reason)}`); + }); } diff --git a/src/structure/client/wrappers/InvokerWrapper.js b/src/structure/client/wrappers/InvokerWrapper.js index daaa4aa..faa6067 100644 --- a/src/structure/client/wrappers/InvokerWrapper.js +++ b/src/structure/client/wrappers/InvokerWrapper.js @@ -14,11 +14,19 @@ class InvokerWrapper { this.client = client; this.target = target; // The wrapped wrapper - this.type = target instanceof MessageWrapper ? 'message' : 'interaction'; - this.command = command; + this._type = target instanceof MessageWrapper ? 'message' : 'interaction'; + this._command = command; } + get command() { + return this._command; + } + + get type() { + return this._type; + } + get id() { return this.target.id; } diff --git a/src/structure/components/observers/CommandHandler.js b/src/structure/components/observers/CommandHandler.js index c95ec2c..6164506 100644 --- a/src/structure/components/observers/CommandHandler.js +++ b/src/structure/components/observers/CommandHandler.js @@ -43,7 +43,13 @@ class CommandHandler extends Observer { const invoker = new InvokerWrapper(this.client, messageWrapper, command); const inhibitors = await this._handleInhibitors(invoker); - if (inhibitors.length) return this._generateError(invoker, { type: 'inhibitor', ...inhibitors[0] }); + const silent = inhibitors.filter((i) => i.inhibitor.silent); + const nonsilent = inhibitors.filter((i) => !i.inhibitor.silent); + if (silent.length && silent.some((i) => i.inhibitor.id === 'channelIgnore') + || nonsilent.length && nonsilent.some((i) => i.args.missing.includes('SEND_MESSAGES')) + || silent.length && !nonsilent.length) return; + + if (inhibitors.length) return this._generateError(invoker, { type: 'inhibitor', ...inhibitors[0] }).catch(this.logger.error); const response = await this._parseMessage(invoker, parameters); // There was an error if _parseResponse return value is truthy, i.e. an error message was sent @@ -116,7 +122,7 @@ class CommandHandler extends Observer { const errors = (await Promise.all(promises)).filter((result) => result.error); if (!errors.length) return []; - return errors.sort((a, b) => a.inhibitor.priority - b.inhibitor.priority); + return errors.sort((a, b) => b.inhibitor.priority - a.inhibitor.priority); } diff --git a/src/structure/interfaces/commands/Command.js b/src/structure/interfaces/commands/Command.js index 99896b0..e3ea696 100644 --- a/src/structure/interfaces/commands/Command.js +++ b/src/structure/interfaces/commands/Command.js @@ -38,7 +38,7 @@ class Command extends Component { this.slash = Boolean(options.slash); - this.clientPermissions = options.clientPermissions || []; + this.clientPermissions = [...new Set(['SEND_MESSAGES', ...options.clientPermissions || []])]; this.memberPermissions = options.memberPermissions || []; this._invokes = {