Skip to content

Commit

Permalink
Rework CommandParser to always trim prefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
hkwu committed Feb 17, 2018
1 parent 8cce281 commit bb39289
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 39 deletions.
47 changes: 15 additions & 32 deletions 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.
Expand All @@ -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(' '),
});
}
}
13 changes: 6 additions & 7 deletions 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 () {
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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>',
Expand All @@ -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',
Expand All @@ -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.