aboutsummaryrefslogtreecommitdiff
path: root/src/ext/framework/command.rs
diff options
context:
space:
mode:
authorIllia <[email protected]>2016-12-09 23:30:30 +0200
committerzeyla <[email protected]>2016-12-09 13:30:30 -0800
commit8f24aa391f6b8a9103a9c105138c7610288acb05 (patch)
tree5af9a958502a49d64868c50ef976157c6b71adca /src/ext/framework/command.rs
parentImplement From<Embed> for CreateEmbed (diff)
downloadserenity-8f24aa391f6b8a9103a9c105138c7610288acb05.tar.xz
serenity-8f24aa391f6b8a9103a9c105138c7610288acb05.zip
Command builder, quoted args, and multi-prefixes
Add a command builder, which can take arguments such as multiple checks, quoted arguments, and multiple prefix support, as well as dynamic prefixes per context.
Diffstat (limited to 'src/ext/framework/command.rs')
-rw-r--r--src/ext/framework/command.rs62
1 files changed, 50 insertions, 12 deletions
diff --git a/src/ext/framework/command.rs b/src/ext/framework/command.rs
index 46338a1..225f50a 100644
--- a/src/ext/framework/command.rs
+++ b/src/ext/framework/command.rs
@@ -2,28 +2,66 @@ use std::sync::Arc;
use super::Configuration;
use ::client::Context;
use ::model::Message;
+use std::collections::HashMap;
+
+/// Command function type. Allows to access internal framework things inside
+/// your commands.
+pub enum CommandType {
+ StringResponse(String),
+ Basic(Box<Fn(&Context, &Message, Vec<String>) + Send + Sync + 'static>),
+ WithCommands(Box<Fn(&Context, &Message, HashMap<String, Arc<Command>>, Vec<String>) + Send + Sync + 'static>)
+}
+
+/// Command struct used to store commands internally.
+pub struct Command {
+ /// A set of checks to be called prior to executing the command. The checks
+ /// will short-circuit on the first check that returns `false`.
+ pub checks: Vec<Box<Fn(&Context, &Message) -> bool + Send + Sync + 'static>>,
+ /// Function called when the command is called.
+ pub exec: CommandType,
+ /// Command description, used by other commands.
+ pub desc: Option<String>,
+ /// Command usage schema, used by other commands.
+ pub usage: Option<String>,
+ /// Whether arguments should be parsed using quote parser or not.
+ pub use_quotes: bool,
+}
-#[doc(hidden)]
-pub type Command = Fn(&Context, &Message, Vec<String>) + Send + Sync;
#[doc(hidden)]
pub type InternalCommand = Arc<Command>;
-pub fn positions(content: &str, conf: &Configuration) -> Option<Vec<usize>> {
- if let Some(ref prefix) = conf.prefix {
+pub fn positions(ctx: &Context, content: &str, conf: &Configuration) -> Option<Vec<usize>> {
+ if conf.prefixes.len() > 0 || conf.dynamic_prefix.is_some() {
// Find out if they were mentioned. If not, determine if the prefix
// was used. If not, return None.
- let mut positions = if let Some(mention_end) = find_mention_end(content, conf) {
- vec![mention_end]
- } else if content.starts_with(prefix) {
- vec![prefix.len()]
+ let mut positions: Vec<usize> = vec![];
+
+ if let Some(mention_end) = find_mention_end(&content, conf) {
+ positions.push(mention_end);
+ } else if let Some(ref func) = conf.dynamic_prefix {
+ if let Some(x) = func(&ctx) {
+ positions.push(x.len());
+ } else {
+ for n in conf.prefixes.clone() {
+ if content.starts_with(&n) {
+ positions.push(n.len());
+ }
+ }
+ }
} else {
- return None;
+ for n in conf.prefixes.clone() {
+ if content.starts_with(&n) {
+ positions.push(n.len());
+ }
+ }
};
+ if positions.len() == 0 {
+ return None;
+ }
+
if conf.allow_whitespace {
- let pos = *unsafe {
- positions.get_unchecked(0)
- };
+ let pos = *unsafe { positions.get_unchecked(0) };
positions.insert(0, pos + 1);
}