Package Exports
- discord-command-parser
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (discord-command-parser) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
discord-command-parser
Basic parsing for messages received with Discord.js.
Installation
With npm:
$ npm install --save discord-command-parserWith Yarn:
$ yarn add discord-command-parserQuick Example
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.
Documentation
See the source code for more details and comments.
parse(message, prefix, [options])
Returns a
ParsedMessage.
messageMessage; the Discord.js Message to parse.prefixstring | string[]; the prefix(es) to check for in commands.optionsobject, optional; additional configuration.options.allowBotsboolean; whether to parse messages sent by bot accounts (message.author.bot).options.allowSelfboolean; whether to parse messages sent by the client account.options.allowSpaceBeforeCommandboolean; whether to allow a space (or multiple) between the prefix and the command name.options.ignorePrefixCaseboolean; whether to ignore the case of the prefix used. Note that the prefix returned in theParsedMessagewill be set to the matching element in the prefix argument, not the prefix in the message, in the event of mismatching case.
ParsedMessage
Used internally and returned by parse.
Note: if suceess is false, only success, error, and message are defined.
Properties:
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
!pingwould have acommandvalue ofping.arguments: string[]Consider using
readerfor 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
argumentsvalue 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 returnnullwhen the argument array is exhausted.error: stringIf
successisfalse, 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.
Example Result
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]
}