diff options
| author | Austin Hellyer <[email protected]> | 2016-09-19 09:00:03 -0700 |
|---|---|---|
| committer | Austin Hellyer <[email protected]> | 2016-10-18 11:14:27 -0700 |
| commit | 8fc8c81403c3daa187ba96a7d488a64db21463bf (patch) | |
| tree | 81bc4890c28b08ce806f69084617066bce863c2d /src/ext/framework | |
| download | serenity-8fc8c81403c3daa187ba96a7d488a64db21463bf.tar.xz serenity-8fc8c81403c3daa187ba96a7d488a64db21463bf.zip | |
Initial commit
Diffstat (limited to 'src/ext/framework')
| -rw-r--r-- | src/ext/framework/command.rs | 7 | ||||
| -rw-r--r-- | src/ext/framework/configuration.rs | 48 | ||||
| -rw-r--r-- | src/ext/framework/mod.rs | 122 |
3 files changed, 177 insertions, 0 deletions
diff --git a/src/ext/framework/command.rs b/src/ext/framework/command.rs new file mode 100644 index 0000000..31b2520 --- /dev/null +++ b/src/ext/framework/command.rs @@ -0,0 +1,7 @@ +use std::sync::Arc; +use ::client::Context; +use ::model::Message; + +pub type Command = Fn(Context, Message) + Send + Sync; +#[doc(hidden)] +pub type InternalCommand = Arc<Command>; diff --git a/src/ext/framework/configuration.rs b/src/ext/framework/configuration.rs new file mode 100644 index 0000000..cd68c69 --- /dev/null +++ b/src/ext/framework/configuration.rs @@ -0,0 +1,48 @@ +use std::default::Default; +use ::client::http; + +pub struct Configuration { + pub depth: usize, + pub on_mention: Option<Vec<String>>, + pub prefix: Option<String>, +} + +impl Configuration { + /// The default depth of the message to check for commands. Defaults to 5. + pub fn depth(mut self, depth: u8) -> Self { + self.depth = depth as usize; + + self + } + + pub fn on_mention(mut self, on_mention: bool) -> Self { + if !on_mention { + return self; + } + + if let Ok(current_user) = http::get_current_user() { + self.on_mention = Some(vec![ + format!("<@{}>", current_user.id), // Regular mention + format!("<@!{}>", current_user.id), // Nickname mention + ]); + } + + self + } + + pub fn prefix<S: Into<String>>(mut self, prefix: S) -> Self { + self.prefix = Some(prefix.into()); + + self + } +} + +impl Default for Configuration { + fn default() -> Configuration { + Configuration { + depth: 5, + on_mention: None, + prefix: None, + } + } +} diff --git a/src/ext/framework/mod.rs b/src/ext/framework/mod.rs new file mode 100644 index 0000000..3021d5b --- /dev/null +++ b/src/ext/framework/mod.rs @@ -0,0 +1,122 @@ +mod command; +mod configuration; + +pub use self::command::Command; +pub use self::configuration::Configuration; + +use self::command::InternalCommand; +use std::collections::HashMap; +use std::sync::Arc; +use std::thread; +use ::client::Context; +use ::model::Message; + +#[allow(type_complexity)] +#[derive(Default)] +pub struct Framework { + configuration: Configuration, + commands: HashMap<String, InternalCommand>, + checks: HashMap<String, Arc<Fn(&Context, &Message) -> bool + Send + Sync + 'static>>, + pub initialized: bool, +} + +impl Framework { + pub fn configure<F>(mut self, f: F) -> Self + where F: FnOnce(Configuration) -> Configuration { + self.configuration = f(self.configuration); + self.initialized = true; + + self + } + + #[doc(hidden)] + pub fn dispatch(&mut self, context: Context, message: Message) { + // Determine the point at which the prefix ends, and the command starts. + let pos = if let Some(ref prefix) = self.configuration.prefix { + let mut mention_ends = None; + + if let Some(ref mentions) = self.configuration.on_mention { + for mention in mentions { + if !message.content.starts_with(&mention[..]) { + continue; + } + + mention_ends = Some(mention.len() + 1); + + break; + } + } + + if let Some(mention_ends) = mention_ends { + mention_ends + } else if !message.content.starts_with(prefix) { + return; + } else { + prefix.len() + } + } else { + 0 + }; + + // Ensure that the message length is at least longer than the prefix + // length. There's no point in checking further ahead if there's nothing + // to check. + if message.content.len() <= pos { + return; + } + + let mut built = String::new(); + + for i in 0..self.configuration.depth { + if i > 0 { + built.push(' '); + } + + built.push_str(match { + message.content + .split_at(pos) + .1 + .split_whitespace() + .collect::<Vec<&str>>() + .get(i) + } { + Some(piece) => piece, + None => return, + }); + + if let Some(command) = self.commands.get(&built) { + if let Some(check) = self.checks.get(&built) { + if !(check)(&context, &message) { + return; + } + } + + let command = command.clone(); + + thread::spawn(move || { + (command)(context, message) + }); + + return; + } + } + } + + pub fn on<F, S>(mut self, command_name: S, f: F) -> Self + where F: Fn(Context, Message) + Send + Sync + 'static, + S: Into<String> { + self.commands.insert(command_name.into(), Arc::new(f)); + self.initialized = true; + + self + } + + pub fn set_check<F, S>(mut self, command: S, check: F) -> Self + where F: Fn(&Context, &Message) -> bool + Send + Sync + 'static, + S: Into<String> { + self.checks.insert(command.into(), Arc::new(check)); + self.initialized = true; + + self + } +} |