Skip to main content

Module command

Module command 

Source
Expand description

Command parsers.

You can either create an enum with derived BotCommands, containing commands of your bot, or use functions, which split input text into a string command with its arguments.

§Using BotCommands

use teloxide_ng::utils::command::BotCommands;

type UnitOfTime = u8;

#[derive(BotCommands, PartialEq, Debug)]
#[command(rename_rule = "lowercase", parse_with = "split")]
enum AdminCommand {
    Mute(UnitOfTime, char),
    Ban(UnitOfTime, char),
}

let command = AdminCommand::parse("/ban 5 h", "bot_name").unwrap();
assert_eq!(command, AdminCommand::Ban(5, 'h'));

§Using parse_command

use teloxide_ng::utils::command::parse_command;

let (command, args) = parse_command("/ban@MyBotName 3 hours", "MyBotName").unwrap();
assert_eq!(command, "ban");
assert_eq!(args, vec!["3", "hours"]);

§Using parse_command_with_prefix

use teloxide_ng::utils::command::parse_command_with_prefix;

let text = "!ban 3 hours";
let (command, args) = parse_command_with_prefix("!", text, "").unwrap();
assert_eq!(command, "ban");
assert_eq!(args, vec!["3", "hours"]);

See examples/admin as a more complicated examples.

Structs§

CommandDescription
Description of a particular command, used in CommandDescriptions.
CommandDescriptions
Command descriptions that can be shown to the user (e.g. as a part of /help message)

Enums§

ParseError
Errors returned from BotCommands::parse.

Traits§

BotCommands
An enumeration of bot’s commands.

Functions§

parse_command
Parses a string into a command with args.
parse_command_with_prefix
Parses a string into a command with args (custom prefix).

Type Aliases§

BotName
PrefixedBotCommand

Derive Macros§

BotCommandsmacros