Giter Club home page Giter Club logo

discord-command-parser's Introduction

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

discord-command-parser's People

Contributors

app24 avatar brenfwd avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.