[][src]Crate twilight_command_parser

license badge rust badge

twilight-command-parser

twilight-command-parser is a command parser for the twilight ecosystem.

Included is a mutable configuration that allows you to specify the command names and prefixes. The parser parses out commands matching an available command and prefix and provides the command arguments to you.

Installation

twilight-command-parser requires at least Rust 1.36.0.

Add the following to your Cargo.toml:

[dependencies]
twilight-command-parser = "0.1"

Examples

A simple parser for a bot with one prefix ("!") and two commands: "echo" and "ping":

use twilight_command_parser::{Command, CommandParserConfig, Parser};

let mut config = CommandParserConfig::new();

// (Use `CommandParserConfig::add_command` to add a single command)
config.command("echo").add();
config.command("ping").add();

// Add the prefix `"!"`.
// (Use `CommandParserConfig::add_prefixes` to add multiple prefixes)
config.add_prefix("!");

let parser = Parser::new(config);

// Now pass a command to the parser
match parser.parse("!echo a message") {
    Some(Command { name: "echo", arguments, .. }) => {
        let content = arguments.as_str();

        println!("Got an echo request to send `{}`", content);
    },
    Some(Command { name: "ping", .. }) => {
        println!("Got a ping request");
    },
    // Ignore all other commands.
    Some(_) => {},
    None => println!("Message didn't match a prefix and command"),
}

Structs

Arguments

An iterator over command arguments.

Command

Indicator that a command was used.

CommandBuilder

A builder struct for building commands.

CommandParserConfig

Configuration for a Parser.

Parser

A struct to parse prefixes, commands, and arguments out of messages.

Enums

CaseSensitivity