diff options
| author | Uninteresting Account <[email protected]> | 2017-10-29 18:27:38 +1000 |
|---|---|---|
| committer | alex <[email protected]> | 2017-10-29 09:27:38 +0100 |
| commit | e694766bb6c93d5f6a75ad9871cfdefbd0309a17 (patch) | |
| tree | 877c47f7b32451a892bfd16d3604f1634c77edf5 /src/framework | |
| parent | Fall back to `str::parse` if `parse_username` fails (diff) | |
| download | serenity-e694766bb6c93d5f6a75ad9871cfdefbd0309a17.tar.xz serenity-e694766bb6c93d5f6a75ad9871cfdefbd0309a17.zip | |
Fix #206 (#207)
Diffstat (limited to 'src/framework')
| -rw-r--r-- | src/framework/standard/command.rs | 37 | ||||
| -rw-r--r-- | src/framework/standard/mod.rs | 2 |
2 files changed, 28 insertions, 11 deletions
diff --git a/src/framework/standard/command.rs b/src/framework/standard/command.rs index 6330810..b53ed90 100644 --- a/src/framework/standard/command.rs +++ b/src/framework/standard/command.rs @@ -131,6 +131,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) { @@ -155,22 +156,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 @@ -185,3 +182,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 1a7a5ee..f4c98e8 100644 --- a/src/framework/standard/mod.rs +++ b/src/framework/standard/mod.rs @@ -860,7 +860,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 { |