Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 37 ยท Commands and Events

Parsing Commands

Write a robust command parser for chat bots - prefixes and mentions, quoted arguments, flags, typed and validated parameters, aliases and generated help - with runnable Node.js examples.

Telegram delivers commands as plain text (/remind 10m take out the trash), and older Discord bots did the same with a prefix such as !. Even when your bot uses Discord's slash commands, you will still parse text in places: arguments in a free-text field, admin commands in a Telegram group, rules typed by moderators. A good parser is small, testable and kind to users. Because it is a pure function from string to a result, you can write it once and use it on both platforms.

From text to command and arguments

The naive approach is text.split(' '). It breaks down on quoted values (!say "hello world"), on extra spaces, on mentions and on the @botname suffix Telegram adds in groups. Build up from a tokenizer that understands quotes:

  • Split on whitespace outside quotes.
  • Treat "double" and 'single' quoted text as one token.
  • Allow a backslash to escape a quote.
  • Report an unterminated quote instead of guessing.
Node.js A quote-aware tokenizer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Now wrap it: strip the prefix (or the leading / on Telegram), remove a @botname suffix when it names this bot, lowercase the command name and keep the remainder tokenized.

Node.js Prefix, suffix and mention handling

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The tokenizer is now a regular expression, which is shorter but less informative when a quote is unclosed. Both are fine; choose the one whose error handling suits your bot. A regex tokenizer treats an unclosed quote as plain text, which is often the friendlier behaviour for a chat command.

Typed arguments and validation

Raw tokens are strings. Commands need numbers, durations, users and choices, and users make mistakes. Describe each command with a small schema, then let one function convert and validate every argument and generate an error message that tells the user exactly what went wrong.

Node.js A schema-driven argument parser

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice how every failure message names the argument, quotes the bad input and shows the usage line. That is more valuable than clever parsing: a user who gets "Usage: /remind " fixes the command in seconds, while one who gets silence assumes the bot is broken.

Aliases, permissions and cooldowns

Three small additions make a command table production-ready:

  • Aliases map several names to one command (/h and /help). Keep them in the command spec and build a lookup map once at startup, throwing if two commands claim the same name.
  • Permission checks belong next to the spec (adminOnly: true), checked before the handler runs, so a handler can assume the caller is allowed.
  • Cooldowns stop one user from flooding the bot. Keep a Map from "user:command" to the time of the last call.
Node.js Aliases and per-user cooldowns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Passing now in as a parameter instead of calling Date.now() inside is a testing trick: the function becomes deterministic, which is why this example prints the same output every time.

Why slash commands help

Discord's slash commands and Telegram's command menu do part of this parsing for you: Discord validates option types, ranges and choices before your bot sees the interaction. Our schema idea maps almost one to one onto Discord Slash Commands, so the work you did here is not wasted.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is text.split(' ') a poor command parser?

    Plain splitting cannot keep "New York" together, copes badly with extra whitespace and ignores @botname in groups.
  2. 2. A group command is /weather@other_bot Paris. demo_bot should...

    The suffix names the bot that should answer, so other bots stay silent.
  3. 3. What should an invalid argument produce?

    Clear errors with usage help let users correct themselves immediately.
  4. 4. Why pass the current time into a cooldown function as a parameter?

    Injecting time removes hidden dependencies, so tests can use fixed timestamps.

Summary

  • Tokenize with quote and escape support; never rely on split(' ').
  • Strip the prefix, handle the @botname suffix and ignore commands meant for other bots.
  • Describe commands with schemas: types, optional arguments, defaults and choices, and let one function validate and generate usage text.
  • Keep a command registry with aliases (checked for duplicates), permission flags and cooldowns.
  • Make logic pure (inject time) so it can be tested without a network.

Next: Discord Slash Commands