diff options
| author | Zeyla Hellyer <[email protected]> | 2017-06-13 22:01:02 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-06-13 22:01:02 -0700 |
| commit | 28456813f6f05e9bdaf08e8cad641df1e3dfaff7 (patch) | |
| tree | 09eb479537ee4284b57a06bc8d5962bab450f130 /src/framework | |
| parent | Remove Context::{channel_id, queue} (diff) | |
| download | serenity-28456813f6f05e9bdaf08e8cad641df1e3dfaff7.tar.xz serenity-28456813f6f05e9bdaf08e8cad641df1e3dfaff7.zip | |
Make framework dynamic_prefix accept an &Message
Diffstat (limited to 'src/framework')
| -rw-r--r-- | src/framework/command.rs | 19 | ||||
| -rw-r--r-- | src/framework/configuration.rs | 8 | ||||
| -rw-r--r-- | src/framework/mod.rs | 2 |
3 files changed, 14 insertions, 15 deletions
diff --git a/src/framework/command.rs b/src/framework/command.rs index 5f43284..6db228f 100644 --- a/src/framework/command.rs +++ b/src/framework/command.rs @@ -1,8 +1,7 @@ use std::sync::Arc; use super::Configuration; use ::client::Context; -use ::model::Message; -use ::model::Permissions; +use ::model::{Message, Permissions}; use std::collections::HashMap; pub type Check = Fn(&mut Context, &Message) -> bool + Send + Sync + 'static; @@ -12,7 +11,7 @@ pub type BeforeHook = Fn(&mut Context, &Message, &String) -> bool + Send + Sync pub type AfterHook = Fn(&mut Context, &Message, &String, Result<(), String>) + Send + Sync + 'static; #[doc(hidden)] pub type InternalCommand = Arc<Command>; -pub type PrefixCheck = Fn(&mut Context) -> Option<String> + Send + Sync + 'static; +pub type PrefixCheck = Fn(&mut Context, &Message) -> Option<String> + Send + Sync + 'static; #[doc(hidden)] pub enum CommandOrAlias { @@ -92,29 +91,29 @@ impl Command { } } -pub fn positions(ctx: &mut Context, content: &str, conf: &Configuration) -> Option<Vec<usize>> { +pub fn positions(ctx: &mut Context, msg: &Message, conf: &Configuration) -> Option<Vec<usize>> { if !conf.prefixes.is_empty() || 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: Vec<usize> = vec![]; - if let Some(mention_end) = find_mention_end(content, conf) { + 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) { - if content.starts_with(&x) { + 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 content.starts_with(&n) { + if msg.content.starts_with(&n) { positions.push(n.len()); } } } } else { for n in conf.prefixes.clone() { - if content.starts_with(&n) { + if msg.content.starts_with(&n) { positions.push(n.len()); } } @@ -132,7 +131,7 @@ pub fn positions(ctx: &mut Context, content: &str, conf: &Configuration) -> Opti Some(positions) } else if conf.on_mention.is_some() { - match find_mention_end(content, conf) { + match find_mention_end(&msg.content, conf) { Some(mention_end) => { let mut positions = vec![mention_end]; diff --git a/src/framework/configuration.rs b/src/framework/configuration.rs index 0a1c351..fa76a21 100644 --- a/src/framework/configuration.rs +++ b/src/framework/configuration.rs @@ -3,7 +3,7 @@ use std::default::Default; use super::command::PrefixCheck; use ::client::Context; use ::http; -use ::model::{GuildId, UserId}; +use ::model::{GuildId, Message, UserId}; /// The configuration to use for a [`Framework`] associated with a [`Client`] /// instance. @@ -185,8 +185,8 @@ impl Configuration { /// # let mut client = Client::new("token"); /// client.with_framework(|f| f /// .command("ping", |c| c.exec_str("Pong!")) - /// .configure(|c| c.dynamic_prefix(|ctx| { - /// Some(if ctx.channel_id.unwrap().0 % 5 == 0 { + /// .configure(|c| c.dynamic_prefix(|_, msg| { + /// Some(if msg.channel_id.0 % 5 == 0 { /// "!" /// } else { /// "~" @@ -194,7 +194,7 @@ impl Configuration { /// }))); /// ``` pub fn dynamic_prefix<F>(mut self, dynamic_prefix: F) -> Self - where F: Fn(&mut Context) -> Option<String> + Send + Sync + 'static { + where F: Fn(&mut Context, &Message) -> Option<String> + Send + Sync + 'static { self.dynamic_prefix = Some(Box::new(dynamic_prefix)); self diff --git a/src/framework/mod.rs b/src/framework/mod.rs index 1f7a7bb..43d8fe4 100644 --- a/src/framework/mod.rs +++ b/src/framework/mod.rs @@ -452,7 +452,7 @@ impl Framework { #[allow(cyclomatic_complexity)] #[doc(hidden)] pub fn dispatch(&mut self, mut context: Context, message: Message) { - let res = command::positions(&mut context, &message.content, &self.configuration); + let res = command::positions(&mut context, &message, &self.configuration); let positions = match res { Some(mut positions) => { |