twilight-command-parser

Crates.iotwilight-command-parser
lib.rstwilight-command-parser
version0.9.0
sourcesrc
created_at2020-08-30 02:56:06.969914
updated_at2022-01-23 00:25:32.721147
descriptionMessage command parser for the Twilight ecosystem.
homepagehttps://twilight.rs/chapter_1_crates/section_5_command_parser.html
repositoryhttps://github.com/twilight-rs/twilight.git
max_upload_size
id282541
size31,253
Erk (Erk-)

documentation

https://docs.rs/twilight-command-parser

README

twilight-command-parser

codecov badge discord badge github badge license badge rust badge

This crate has been deprecated. Please use Discord interactions via the twilight-gateway or twilight-http crates.

twilight-command-parser is a command parser for the twilight-rs 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.

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();

config.add_command("echo", false);
config.add_command("ping", false);

// 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"),
}
Commit count: 2436

cargo fmt