use std::sync::Arc;
use super::Configuration;
use ::client::Context;
use ::model::{Message, Permissions};
use std::collections::HashMap;
pub type Check = Fn(&mut Context, &Message) -> bool + Send + Sync + 'static;
pub type Exec = Fn(&mut Context, &Message, Vec<String>) -> Result<(), String> + Send + Sync + 'static;
pub type Help = Fn(&mut Context, &Message, HashMap<String, Arc<CommandGroup>>, &[String]) -> Result<(), String> + Send + Sync + 'static;
pub type BeforeHook = Fn(&mut Context, &Message, &String) -> bool + Send + Sync + 'static;
pub type AfterHook = Fn(&mut Context, &Message, &String, Result<(), String>) + Send + Sync + 'static;
pub(crate) type InternalCommand = Arc<Command>;
pub type PrefixCheck = Fn(&mut Context, &Message) -> Option<String> + Send + Sync + 'static;
pub(crate) enum CommandOrAlias {
Alias(String),
Command(InternalCommand),
}
pub enum CommandType {
StringResponse(String),
Basic(Box<Exec>),
WithCommands(Box<Help>),
}
#[derive(Default)]
pub struct CommandGroup {
pub prefix: Option<String>,
pub(crate) commands: HashMap<String, CommandOrAlias>,
}
pub struct Command {
pub checks: Vec<Box<Check>>,
pub exec: CommandType,
pub bucket: Option<String>,
pub desc: Option<String>,
pub example: Option<String>,
pub usage: Option<String>,
pub use_quotes: bool,
pub min_args: Option<i32>,
pub max_args: Option<i32>,
pub required_permissions: Permissions,
pub help_available: bool,
pub dm_only: bool,
pub guild_only: bool,
pub owners_only: bool,
pub(crate) aliases: Vec<String>,
}
impl Command {
pub fn new<F>(f: F) -> Self
where F: Fn(&mut Context, &Message, Vec<String>) -> Result<(), String> + Send + Sync + 'static {
Command {
aliases: Vec::new(),
checks: Vec::default(),
exec: CommandType::Basic(Box::new(f)),
desc: None,
usage: None,
example: None,
use_quotes: false,
dm_only: false,
bucket: None,
guild_only: false,
help_available: true,
min_args: None,
max_args: None,
owners_only: false,
required_permissions: Permissions::empty(),
}
}
}
pub fn positions(ctx: &mut Context, msg: &Message, conf: &Configuration) -> Option<Vec<usize>> {
if !conf.prefixes.is_empty() || conf.dynamic_prefix.is_some() {
let mut positions: Vec<usize> = vec![];
if let Some(mention_end) = find_mention_end(&msg.content, conf) {
positions.push(mention_end);
} else if let Some(ref func) = conf.dynamic_prefix {
if let Some(x) = func(ctx, msg) {
if msg.content.starts_with(&x) {
positions.push(x.len());
}
} else {
for n in conf.prefixes.clone() {
if msg.content.starts_with(&n) {
positions.push(n.len());
}
}
}
} else {
for n in conf.prefixes.clone() {
if msg.content.starts_with(&n) {
positions.push(n.len());
}
}
};
if positions.is_empty() {
return None;
}
if conf.allow_whitespace {
let pos = *unsafe { positions.get_unchecked(0) };
positions.insert(0, pos + 1);
}
Some(positions)
} else if conf.on_mention.is_some() {
match find_mention_end(&msg.content, conf) {
Some(mention_end) => {
let mut positions = vec![mention_end];
if conf.allow_whitespace {
positions.insert(0, mention_end + 1);
}
Some(positions)
},
None => None,
}
} else {
None
}
}
fn find_mention_end(content: &str, conf: &Configuration) -> Option<usize> {
if let Some(ref mentions) = conf.on_mention {
for mention in mentions {
if !content.starts_with(&mention[..]) {
continue;
}
return Some(mention.len());
}
}
None
}