diff options
| author | acdenisSK <[email protected]> | 2017-11-04 20:06:40 +0100 |
|---|---|---|
| committer | acdenisSK <[email protected]> | 2017-11-04 20:06:40 +0100 |
| commit | 0bd519f4ef9784d0fb5663d74db0d567f0bb1ae5 (patch) | |
| tree | cc2a6f44e97e42420507964dab4662fcccd9beb3 /src/framework | |
| parent | Fix Help-Commands to list all eligible commands in DMs. (#212) (diff) | |
| parent | Bump to v0.4.3 (diff) | |
| download | serenity-0bd519f4ef9784d0fb5663d74db0d567f0bb1ae5.tar.xz serenity-0bd519f4ef9784d0fb5663d74db0d567f0bb1ae5.zip | |
Merge v0.4.3
Diffstat (limited to 'src/framework')
| -rw-r--r-- | src/framework/mod.rs | 2 | ||||
| -rw-r--r-- | src/framework/standard/command.rs | 37 | ||||
| -rw-r--r-- | src/framework/standard/mod.rs | 4 |
3 files changed, 30 insertions, 13 deletions
diff --git a/src/framework/mod.rs b/src/framework/mod.rs index a5f458d..85ae6f4 100644 --- a/src/framework/mod.rs +++ b/src/framework/mod.rs @@ -36,7 +36,7 @@ //! use serenity::model::Message; //! use std::env; //! -//! let mut client = Client::new(&env::var("DISCORD_BOT_TOKEN").unwrap()); +//! let mut client = Client::new(&env::var("DISCORD_TOKEN").unwrap()); //! //! client.with_framework(|f| f //! .configure(|c| c.prefix("~")) diff --git a/src/framework/standard/command.rs b/src/framework/standard/command.rs index 797bed5..be7ec2a 100644 --- a/src/framework/standard/command.rs +++ b/src/framework/standard/command.rs @@ -128,6 +128,7 @@ pub fn positions(ctx: &mut Context, msg: &Message, conf: &Configuration) -> Opti if let Some(mention_end) = find_mention_end(&msg.content, conf) { positions.push(mention_end); + return Some(positions); } else if let Some(ref func) = conf.dynamic_prefix { if let Some(x) = func(ctx, msg) { if msg.content.starts_with(&x) { @@ -152,22 +153,18 @@ pub fn positions(ctx: &mut Context, msg: &Message, conf: &Configuration) -> Opti 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); + if conf.allow_whitespace { + positions.insert(0, find_end_of_prefix_with_whitespace(&msg.content, pos).unwrap_or(pos)); + } else if find_end_of_prefix_with_whitespace(&msg.content, pos).is_some() { + return None; } Some(positions) } else if conf.on_mention.is_some() { find_mention_end(&msg.content, conf).map(|mention_end| { - let mut positions = vec![mention_end]; - - if conf.allow_whitespace { - positions.insert(0, mention_end + 1); - } - - positions + vec![mention_end] // This can simply be returned without trying to find the end whitespaces as trim will remove it later }) } else { None @@ -182,3 +179,23 @@ fn find_mention_end(content: &str, conf: &Configuration) -> Option<usize> { .map(|m| m.len()) }) } + +// Finds the end of the first continuous block of whitespace after the prefix +fn find_end_of_prefix_with_whitespace(content: &str, position: usize) -> Option<usize> { + let mut ws_split = content.split_whitespace(); + if let Some(cmd) = ws_split.nth(1) { + if let Some(index_of_cmd) = content.find(cmd) { + if index_of_cmd > position && index_of_cmd <= content.len() { + let slice = unsafe { content.slice_unchecked(position, index_of_cmd) }.as_bytes(); + for byte in slice.iter() { + // 0x20 is ASCII for space + if *byte != 0x20u8 { + return None; + } + } + return Some(index_of_cmd); + } + } + } + None +} diff --git a/src/framework/standard/mod.rs b/src/framework/standard/mod.rs index 1681bd7..fdab3a8 100644 --- a/src/framework/standard/mod.rs +++ b/src/framework/standard/mod.rs @@ -859,7 +859,7 @@ impl Framework for StandardFramework { 'outer: for position in positions { let mut built = String::new(); let round = message.content.chars().skip(position).collect::<String>(); - let round = round.trim().split_whitespace().collect::<Vec<&str>>(); + let round = round.trim().split_whitespace().collect::<Vec<&str>>(); // Call to `trim` causes the related bug under the main bug #206 - where the whitespace settings are ignored. The fix is implemented as an additional check inside command::positions for i in 0..self.configuration.depth { if i != 0 { @@ -965,7 +965,7 @@ pub fn has_correct_permissions(command: &Command, message: &Message) -> bool { if !command.required_permissions.is_empty() { if let Some(guild) = message.guild() { let perms = guild - .with(|g| g.permissions_for(message.channel_id, message.author.id)); + .with(|g| g.permissions_in(message.channel_id, message.author.id)); return perms.contains(command.required_permissions); } |