diff --git a/src/command/parsers/CommandParser.js b/src/command/parsers/CommandParser.js index 56b6992..7e2ec8f 100644 --- a/src/command/parsers/CommandParser.js +++ b/src/command/parsers/CommandParser.js @@ -1,7 +1,5 @@ -import ClosureFilter from '../../client/dispatcher/ClosureFilter'; import CommandParserError from '../../errors/CommandParserError'; import ParsedCommand from './ParsedCommand'; -import RegexFilter from '../../client/dispatcher/RegexFilter'; /** * @desc Handles parsing a message for a command. @@ -11,41 +9,26 @@ export default class CommandParser { /** * Parses the given message for a command. * @param {Message} message - The Discord.js `Message` object. - * @param {PrefixFilter} prefixFilter - The prefix filter. + * @param {RegExp} prefix - The prefix to remove from the message. * @returns {ParsedCommand} Data parsed from the message content. * @throws {CommandParserError} Thrown if the given message could not be parsed as a command. */ - static parse(message, prefixFilter) { - if (prefixFilter instanceof ClosureFilter) { - const split = message.content.split(' ').filter(word => word); - const [identifier, ...args] = split; + static parse(message, prefix) { + const trimmed = message.content.replace(prefix, '').trim(); + const split = trimmed.split(' ').filter(word => word); - return new ParsedCommand({ - raw: message.content, - trimmed: message.content, - identifier, - args, - rawArgs: args.join(' '), - }); - } else if (prefixFilter instanceof RegexFilter) { - const trimmed = message.content.replace(prefixFilter.filter, '').trim(); - const split = trimmed.split(' ').filter(word => word); - - if (!split.length) { - throw new CommandParserError('Message does not contain enough words to specify a command.'); - } - - const [identifier, ...args] = split; - - return new ParsedCommand({ - raw: message.content, - trimmed, - identifier, - args, - rawArgs: args.join(' '), - }); + if (!split.length) { + throw new CommandParserError('Message does not contain enough words to specify a command.'); } - throw new CommandParserError('Unrecognized prefix filter type.'); + const [identifier, ...args] = split; + + return new ParsedCommand({ + raw: message.content, + trimmed, + identifier, + args, + rawArgs: args.join(' '), + }); } } diff --git a/test/parsers/CommandParserTest.js b/test/parsers/CommandParserTest.js index 29da757..19277b0 100644 --- a/test/parsers/CommandParserTest.js +++ b/test/parsers/CommandParserTest.js @@ -1,7 +1,6 @@ import chai, { expect } from 'chai'; import chaiSubset from 'chai-subset'; import CommandParser from '../../src/command/parsers/CommandParser'; -import RegexFilter from '../../src/client/dispatcher/RegexFilter'; describe('CommandParser', function () { before(function () { @@ -19,7 +18,7 @@ describe('CommandParser', function () { }, }; - expect(CommandParser.parse(message, new RegexFilter('!'))).to.containSubset({ + expect(CommandParser.parse(message, new RegExp('!'))).to.containSubset({ raw: '!cmd pro', trimmed: 'cmd pro', identifier: 'cmd', @@ -35,7 +34,7 @@ describe('CommandParser', function () { }, }; - expect(CommandParser.parse(message, new RegexFilter('!'))).to.containSubset({ + expect(CommandParser.parse(message, new RegExp('!'))).to.containSubset({ raw: '!cmd', trimmed: 'cmd', identifier: 'cmd', @@ -53,7 +52,7 @@ describe('CommandParser', function () { }, }; - expect(CommandParser.parse(message, new RegexFilter('<@123456789>'))).to.containSubset({ + expect(CommandParser.parse(message, new RegExp('<@123456789>'))).to.containSubset({ raw: '<@123456789> cmd pro pro', trimmed: 'cmd pro pro', identifier: 'cmd', @@ -69,7 +68,7 @@ describe('CommandParser', function () { }, }; - expect(CommandParser.parse(message, new RegexFilter('<@0>'))).to.containSubset({ + expect(CommandParser.parse(message, new RegExp('<@0>'))).to.containSubset({ raw: '<@123456789> cmd pro pro', trimmed: '<@123456789> cmd pro pro', identifier: '<@123456789>', @@ -85,7 +84,7 @@ describe('CommandParser', function () { }, }; - expect(CommandParser.parse(message, new RegexFilter('!'))).to.containSubset({ + expect(CommandParser.parse(message, new RegExp('!'))).to.containSubset({ raw: '!cmd pro <@123456789> pro', trimmed: 'cmd pro <@123456789> pro', identifier: 'cmd', @@ -103,7 +102,7 @@ describe('CommandParser', function () { }, }; - expect(() => CommandParser.parse(message, new RegexFilter('<@123456789>'))).to.throw(Error); + expect(() => CommandParser.parse(message, new RegExp('<@123456789>'))).to.throw(Error); }); }); });