pub struct Parser<'a> { /* private fields */ }
twilight-http
or twilight-gateway
Expand description
A struct to parse prefixes, commands, and arguments out of messages.
While parsing, the parser takes into account the configuration that it was
configured with. This configuration is mutable during runtime via the
Parser::config_mut
method.
After parsing, youโre given an optional Command
: a struct representing a
command and its relevant information. Refer to its documentation for more
information.
ยงExamples
Using a parser configured with the commands "echo"
and "ping"
and the
prefix "!"
, parse the message โ!echo foo bar bazโ:
use twilight_command_parser::{Command, CommandParserConfig, Parser};
let mut config = CommandParserConfig::new();
config.add_command("echo", false);
config.add_command("ping", false);
config.add_prefix("!");
let parser = Parser::new(config);
if let Some(command) = parser.parse("!echo foo bar baz") {
match command {
Command { name: "echo", arguments, .. } => {
let content = arguments.as_str();
println!("Got a request to echo `{}`", content);
},
Command { name: "ping", .. } => {
println!("Got a ping request");
},
_ => {},
}
}
Implementationsยง
Sourceยงimpl<'a> Parser<'a>
impl<'a> Parser<'a>
Sourcepub fn new(config: impl Into<CommandParserConfig<'a>>) -> Self
๐Deprecated since 0.8.1: use interactions via twilight-http
or twilight-gateway
pub fn new(config: impl Into<CommandParserConfig<'a>>) -> Self
twilight-http
or twilight-gateway
Creates a new parser from a given configuration.
Sourcepub const fn config(&self) -> &CommandParserConfig<'a>
๐Deprecated since 0.8.1: use interactions via twilight-http
or twilight-gateway
pub const fn config(&self) -> &CommandParserConfig<'a>
twilight-http
or twilight-gateway
Returns an immutable reference to the configuration.
Sourcepub fn config_mut(&mut self) -> &mut CommandParserConfig<'a>
๐Deprecated since 0.8.1: use interactions via twilight-http
or twilight-gateway
pub fn config_mut(&mut self) -> &mut CommandParserConfig<'a>
twilight-http
or twilight-gateway
Returns a mutable reference to the configuration.
Sourcepub fn parse(&'a self, buf: &'a str) -> Option<Command<'a>>
๐Deprecated since 0.8.1: use interactions via twilight-http
or twilight-gateway
pub fn parse(&'a self, buf: &'a str) -> Option<Command<'a>>
twilight-http
or twilight-gateway
Parses a command out of a buffer.
If a configured prefix and command are in the buffer, then some
Command
is returned with them and a lazy iterator of the
argument list.
If a matching prefix or command werenโt found, then None
is returned.
Refer to the struct-level documentation on how to use this.
Sourcepub fn parse_with_prefix(
&'a self,
prefix: &'a str,
buf: &'a str,
) -> Option<Command<'a>>
๐Deprecated since 0.8.1: use interactions via twilight-http
or twilight-gateway
pub fn parse_with_prefix( &'a self, prefix: &'a str, buf: &'a str, ) -> Option<Command<'a>>
twilight-http
or twilight-gateway
Parse a command out of a buffer with a specific prefix.
Instead of using the list of set prefixes, give a specific prefix to parse the message, this can be used to have a kind of dynamic prefixes.
ยงExample
let mut config = CommandParserConfig::new();
config.add_prefix("!");
config.add_command("echo", false);
let parser = Parser::new(config);
let command = parser.parse_with_prefix("=", "=echo foo")?;
assert_eq!("=", command.prefix);
assert_eq!("echo", command.name);