diff options
| author | Lakelezz <[email protected]> | 2017-08-13 15:13:13 +0200 |
|---|---|---|
| committer | alex <[email protected]> | 2017-08-13 15:13:13 +0200 |
| commit | 069df4f85d8c462df58c1fce00595462f2825337 (patch) | |
| tree | fdfc25d1942f3ededc0d4b87b03396318b46e564 /src/framework | |
| parent | Trim a second time for a sake (diff) | |
| download | serenity-069df4f85d8c462df58c1fce00595462f2825337.tar.xz serenity-069df4f85d8c462df58c1fce00595462f2825337.zip | |
Fix string delimiters (#134)
Diffstat (limited to 'src/framework')
| -rw-r--r-- | src/framework/configuration.rs | 24 | ||||
| -rw-r--r-- | src/framework/mod.rs | 42 |
2 files changed, 35 insertions, 31 deletions
diff --git a/src/framework/configuration.rs b/src/framework/configuration.rs index a6d0440..2854d90 100644 --- a/src/framework/configuration.rs +++ b/src/framework/configuration.rs @@ -58,7 +58,7 @@ pub struct Configuration { #[doc(hidden)] pub prefixes: Vec<String>, #[doc(hidden)] - pub delimeters: Vec<String>, + pub delimiters: Vec<String>, } impl Configuration { @@ -370,7 +370,7 @@ impl Configuration { self } - /// Sets a delimeter to be used when splitting the content after a command. + /// Sets a delimiter to be used when splitting the content after a command. /// /// # Examples /// @@ -386,15 +386,16 @@ impl Configuration { /// use serenity::framework::BuiltinFramework; /// /// client.with_framework(BuiltinFramework::new().configure(|c| c - /// .delimeter(", "))); + /// .delimiter(", "))); /// ``` - pub fn delimeter(mut self, delimeter: &str) -> Self { - self.delimeters.push(delimeter.to_string()); + pub fn delimiter(mut self, delimiter: &str) -> Self { + self.delimiters.push(delimiter.to_string()); self } - /// Sets multiple delimeters to be used when splitting the content after a command. + /// Sets multiple delimiters to be used when splitting the content after a command. + /// Additionally cleans the default delimiter from the vector. /// /// # Examples /// @@ -410,10 +411,11 @@ impl Configuration { /// use serenity::framework::BuiltinFramework; /// /// client.with_framework(BuiltinFramework::new().configure(|c| c - /// .delimeters(vec![", ", " "]))); + /// .delimiters(vec![", ", " "]))); /// ``` - pub fn delimeters(mut self, delimeters: Vec<&str>) -> Self { - self.delimeters.extend(delimeters.into_iter().map(|s| s.to_string())); + pub fn delimiters(mut self, delimiters: Vec<&str>) -> Self { + self.delimiters.clear(); + self.delimiters.extend(delimiters.into_iter().map(|s| s.to_string())); self } @@ -426,7 +428,7 @@ impl Default for Configuration { /// - **depth** to `5` /// - **on_mention** to `false` (basically) /// - **prefix** to `None` - /// - **delimeters** to vec![" "] + /// - **delimiters** to vec![" "] fn default() -> Configuration { Configuration { depth: 5, @@ -441,7 +443,7 @@ impl Default for Configuration { disabled_commands: HashSet::default(), allow_dm: true, ignore_webhooks: true, - delimeters: vec![" ".to_string()], + delimiters: vec![" ".to_string()], } } } diff --git a/src/framework/mod.rs b/src/framework/mod.rs index ba80e60..69b0de8 100644 --- a/src/framework/mod.rs +++ b/src/framework/mod.rs @@ -78,6 +78,9 @@ use model::{ChannelId, GuildId, Message, UserId}; use model::permissions::Permissions; use utils; use tokio_core::reactor::Handle; +use itertools::Itertools; +use regex::Regex; +use regex::escape; #[cfg(feature = "cache")] use client::CACHE; @@ -518,9 +521,9 @@ impl BuiltinFramework { let rate_limit = bucket.take(message.author.id.0); match bucket.check { Some(ref check) => { - let apply = feature_cache! {{ + let apply = feature_cache! {{ let guild_id = message.guild_id(); - (check)(context, guild_id, message.channel_id, message.author.id) + (check)(context, guild_id, message.channel_id, message.author.id) } else { (check)(context, message.channel_id, message.author.id) }}; @@ -567,7 +570,7 @@ impl BuiltinFramework { } if (!self.configuration.allow_dm && message.is_private()) || - (command.guild_only && message.is_private()) { + (command.guild_only && message.is_private()) { return Some(DispatchError::OnlyForGuilds); } @@ -579,13 +582,13 @@ impl BuiltinFramework { if command.owners_only { Some(DispatchError::OnlyForOwners) } else if !command - .checks - .iter() - .all(|check| (check)(&mut context, message, command)) { + .checks + .iter() + .all(|check| (check)(&mut context, message, command)) { Some(DispatchError::CheckFailed) } else if self.configuration - .blocked_users - .contains(&message.author.id) { + .blocked_users + .contains(&message.author.id) { Some(DispatchError::BlockedUser) } else if self.configuration.disabled_commands.contains(to_check) { Some(DispatchError::CommandDisabled(to_check.to_owned())) @@ -909,7 +912,7 @@ impl ::Framework for BuiltinFramework { let command_length = built.len(); if let Some(&CommandOrAlias::Alias(ref points_to)) = - group.commands.get(&built) { + group.commands.get(&built) { built = points_to.to_owned(); } @@ -924,7 +927,7 @@ impl ::Framework for BuiltinFramework { }; if let Some(&CommandOrAlias::Command(ref command)) = - group.commands.get(&to_check) { + group.commands.get(&to_check) { let before = self.before.clone(); let command = command.clone(); let after = self.after.clone(); @@ -937,16 +940,15 @@ impl ::Framework for BuiltinFramework { if command.use_quotes { utils::parse_quotes(&content[command_length..]) } else { - let delimeter = self.configuration.delimeters - .iter() - .find(|d| content.contains(d.as_str())) - .map(|s| s.as_str()) - .unwrap_or(" "); - - content - .split(delimeter) - .map(|arg| arg.to_owned()) - .collect::<Vec<String>>() + let delimiters = &self.configuration.delimiters; + let regular_expression = delimiters.iter() + .map(|delimiter| escape(delimiter)).join("|"); + + let regex = Regex::new(®ular_expression).unwrap(); + + regex.split(content) + .filter_map(|p| if !p.is_empty() { Some(p.to_string()) } else { None }) + .collect::<Vec<_>>() } }; |