From 1681675e46e37440fd982a2aa35fe373bad48d3c Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sat, 6 Apr 2024 19:54:49 -0400 Subject: [PATCH] Re-implement auto commands --- commands/auto/can-you-not.js | 18 ++++++++++++++++++ commands/auto/unflip.js | 17 +++++++++++++++++ commands/single/can-you-not.js | 17 ----------------- framework/Client.js | 19 ++++++++++++++++++- framework/Command.js | 1 + framework/Dispatcher.js | 7 +++++++ 6 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 commands/auto/can-you-not.js create mode 100644 commands/auto/unflip.js delete mode 100644 commands/single/can-you-not.js diff --git a/commands/auto/can-you-not.js b/commands/auto/can-you-not.js new file mode 100644 index 00000000..86b5c215 --- /dev/null +++ b/commands/auto/can-you-not.js @@ -0,0 +1,18 @@ +const Command = require('../../structures/commands/AutoReply'); + +module.exports = class CanYouNotCommand extends Command { + constructor(client) { + super(client, { + name: 'can-you-not', + aliases: ['can-u-not'], + group: 'auto', + memberName: 'can-you-not', + description: 'Can YOU not?', + patterns: [/^(can (you|u) not\??)$/i] + }); + } + + generateText() { + return 'Can YOU not?'; + } +}; diff --git a/commands/auto/unflip.js b/commands/auto/unflip.js new file mode 100644 index 00000000..009c325c --- /dev/null +++ b/commands/auto/unflip.js @@ -0,0 +1,17 @@ +const Command = require('../../structures/commands/AutoReply'); + +module.exports = class UnflipCommand extends Command { + constructor(client, info) { + super(client, { + name: 'unflip', + group: 'auto', + memberName: 'unflip', + description: 'Unflips a table.', + patterns: [/\(╯°□°)╯︵ ┻━┻/i] + }); + } + + generateText() { + return '┬─┬ノ( º _ ºノ)'; + } +}; diff --git a/commands/single/can-you-not.js b/commands/single/can-you-not.js deleted file mode 100644 index 8d9d6cdf..00000000 --- a/commands/single/can-you-not.js +++ /dev/null @@ -1,17 +0,0 @@ -const Command = require('../../framework/Command'); - -module.exports = class CanYouNotCommand extends Command { - constructor(client) { - super(client, { - name: 'can-you-not', - aliases: ['can-u-not'], - group: 'single', - memberName: 'can-you-not', - description: 'Can YOU not?' - }); - } - - run(msg) { - return msg.say('Can YOU not?'); - } -}; diff --git a/framework/Client.js b/framework/Client.js index c53ebd27..4a0ee74f 100644 --- a/framework/Client.js +++ b/framework/Client.js @@ -52,8 +52,25 @@ module.exports = class CommandClient extends Client { if (this.blacklist.user.includes(msg.author.id)) return; if (msg.guild && this.blacklist.guild.includes(msg.guild.id)) return; if (msg.guild && !msg.channel.permissionsFor(this.user).has('SEND_MESSAGES')) return; - if (!this.dispatcher.isCommand(msg)) return; + const patternCmd = this.dispatcher.isPatternCommand(msg); + if (patternCmd) { + try { + const result = await patternCmd.run(msg); + patternCmd.uses++; + patternCmd.lastRun = new Date(); + this.emit('commandRun', patternCmd, result, msg); + } catch (err) { + this.emit('commandError', patternCmd, err, msg); + await msg.reply(stripIndents` + An error occurred while running this command: \`${err.message}\`. + You shouldn't ever recieve an error like this. + ${this.invite ? `Please visit ${this.invite} for support.` : ''} + `); + } + } + + if (!this.dispatcher.isCommand(msg)) return; const parsed = await this.dispatcher.parseMessage(msg); if (typeof parsed === 'string') { const helpUsage = this.registry.commands.get('help').usage(); diff --git a/framework/Command.js b/framework/Command.js index bdc427ac..e15484c0 100644 --- a/framework/Command.js +++ b/framework/Command.js @@ -12,6 +12,7 @@ module.exports = class Command { this.details = options.details || null; this.flags = options.flags || []; this.args = options.args ? options.args.map(arg => new Argument(client, arg)) : []; + this.patterns = options.patterns || []; this.clientPermissions = options.clientPermissions || []; this.userPermissions = options.userPermissions || []; this.ownerOnly = options.ownerOnly || false; diff --git a/framework/Dispatcher.js b/framework/Dispatcher.js index d8b514dc..9eadcae0 100644 --- a/framework/Dispatcher.js +++ b/framework/Dispatcher.js @@ -24,6 +24,13 @@ module.exports = class CommandDispatcher { return Boolean(command); } + isPatternCommand(msg) { + const patternCommands = this.commands + .filter(cmd => cmd.patterns.length && cmd.patterns.some(pattern => pattern.test(msg.content))); + if (!patternCommands.size) return false; + return patternCommands.first(); + } + async parseMessage(msg) { const matched = msg.content.match(this.commandPattern); const command = this.resolveCommand(matched[2].toLowerCase());