Permalink
Browse files

Rework CommandParser to always trim prefix.

  • Loading branch information...
hkwu committed Feb 17, 2018
1 parent 8cce281 commit bb39289bc1489baaf5dda7be1b8a54bb87a180fd
Showing with 21 additions and 39 deletions.
  1. +15 −32 src/command/parsers/CommandParser.js
  2. +6 −7 test/parsers/CommandParserTest.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(' '),
});
}
}
@@ -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);
});
});
});

0 comments on commit bb39289

Please sign in to comment.