Basic parsing for messages received with Discord.js.
With npm:
$ npm install --save discord-command-parser
With Yarn:
$ yarn add discord-command-parser
const discord = require("discord.js");
const parser = require("discord-command-parser");
const prefix = "?";
const client = new discord.Client();
client.on("message", (message) => {
const parsed = parser.parse(message, prefix);
if (!parsed.success) return;
if (parsed.command === "ping") {
return message.reply("Pong!");
}
});
client.login("Token").then(() => console.log("Ready!"));
This project is written using TypeScript. If you use a compatible IDE, you should get documentation, autocompletion, and error-checking included when you use this library.
See the source code for more details and comments.
Returns a
ParsedMessage
.
message
Message; the Discord.js Message to parse.prefix
string | string[]; the prefix(es) to check for in commands.options
object, optional; additional configuration.options.allowBots
boolean; whether to parse messages sent by bot accounts (message.author.bot
).options.allowSelf
boolean; whether to parse messages sent by the client account.options.allowSpaceBeforeCommand
boolean; whether to allow a space (or multiple) between the prefix and the command name.options.ignorePrefixCase
boolean; whether to ignore the case of the prefix used. Note that the prefix returned in theParsedMessage
will be set to the matching element in the prefix argument, not the prefix in the message, in the event of mismatching case.
Used internally and returned by parse
.
Note: if suceess
is false
, only success
, error
, and message
are defined.
success
: booleanWhether the message could be parsed and appears to be a valid command.
prefix
: stringThe prefix that matched. Useful when providing an array of prefixes to
parse
.command
: stringThe command that was parsed from the message. For example, the message
!ping
would have acommand
value ofping
.arguments
: string[]Consider using
reader
for a more advanced way of getting arguments.An array of whitespace or quote delimited arguments that were passed to the command. For example, the command
!ban Clyde 7d "repeated spam of \"no u\" after warning"
would have an
arguments
value of:["Clyde", "7d", 'repeated spam of "no u" after warning'];
reader
: MessageArgumentReaderUse the
getString()
,getUserID()
,getChannelID()
andgetRemaining()
methods to get command arguments in a more object-oriented fashion. All methods returnnull
when the argument array is exhausted.error
: stringIf
success
isfalse
, a description of why the message was rejected. Otherwise empty.body
: stringThe unparsed body of the message immediately following the
command
.message
: MessageRedundant to the message that was passed to
parse
.
Suppose we got this message on Discord:
?remindme "collect daily reward" 24h urgent
(Assuming our prefix
is "?"
)
This is our resulting ParsedMessage
:
{
"success": true,
"prefix": "?",
"command": "remindme",
"arguments": [
"collect daily reward",
"24h",
"urgent"
],
"error": "",
"code": 0,
"body": "\"collect daily reward \" 24h urgent",
"reader": [Object: MessageArgumentReader]
}