wzhouwzhou / discord-command-parser

Basic parsing for messages received with discord.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

discord-command-parser

Basic parsing for messages received with Discord.js.

npm npm

Installation

With npm:

$ npm install --save discord-command-parser

With Yarn:

$ yarn add discord-command-parser

Quick 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.

  • 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 the ParsedMessage will 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: boolean

    Whether the message could be parsed and appears to be a valid command.

  • prefix: string

    The prefix that matched. Useful when providing an array of prefixes to parse.

  • command: string

    The command that was parsed from the message. For example, the message !ping would have a command value of ping.

  • 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: MessageArgumentReader

    Use the getString(), getUserID(), getChannelID() and getRemaining() methods to get command arguments in a more object-oriented fashion. All methods return null when the argument array is exhausted.

  • error: string

    If success is false, a description of why the message was rejected. Otherwise empty.

  • body: string

    The unparsed body of the message immediately following the command.

  • message: Message

    Redundant 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]
}

About

Basic parsing for messages received with discord.js

License:MIT License


Languages

Language:TypeScript 52.0%Language:JavaScript 48.0%