Skip to content

Commit

Permalink
Make framework dynamic_prefix accept an &Message
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeyla Hellyer committed Jun 14, 2017
1 parent 8b504ad commit 2845681
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
19 changes: 9 additions & 10 deletions 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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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());
}
}
Expand All @@ -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];

Expand Down
8 changes: 4 additions & 4 deletions src/framework/configuration.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -185,16 +185,16 @@ 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 {
/// "~"
/// }.to_owned())
/// })));
/// ```
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
Expand Down
2 changes: 1 addition & 1 deletion src/framework/mod.rs
Expand Up @@ -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) => {
Expand Down

0 comments on commit 2845681

Please sign in to comment.