Skip to content

Commit

Permalink
Rewamp the custom delimeter functionality to support more
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis committed Aug 12, 2017
1 parent 125c1b8 commit fdfb184
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
32 changes: 28 additions & 4 deletions src/framework/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub struct Configuration {
#[doc(hidden)]
pub prefixes: Vec<String>,
#[doc(hidden)]
pub delimeter: String,
pub delimeters: Vec<String>,
}

impl Configuration {
Expand Down Expand Up @@ -389,7 +389,31 @@ impl Configuration {
/// .delimeter(", ")));
/// ```
pub fn delimeter(mut self, delimeter: &str) -> Self {
self.delimeter = delimeter.to_string();
self.delimeters.push(delimeter.to_string());

self
}

/// Sets multiple delimeters to be used when splitting the content after a command.
///
/// # Examples
///
/// Have the args be seperated by a comma and a space; and a regular space:
///
/// ```rust
/// # use serenity::prelude::*;
/// # struct Handler;
/// #
/// # impl EventHandler for Handler {}
/// # let mut client = Client::new("token", Handler);
/// #
/// use serenity::framework::BuiltinFramework;
///
/// client.with_framework(BuiltinFramework::new().configure(|c| c
/// .delimeters(vec![", ", " "])));
/// ```
pub fn delimeters(mut self, delimeters: Vec<&str>) -> Self {
self.delimeters.extend(delimeters.into_iter().map(|s| s.to_string()));

self
}
Expand All @@ -402,7 +426,7 @@ impl Default for Configuration {
/// - **depth** to `5`
/// - **on_mention** to `false` (basically)
/// - **prefix** to `None`
/// - **delimeter** to " "
/// - **delimeters** to vec![" "]
fn default() -> Configuration {
Configuration {
depth: 5,
Expand All @@ -417,7 +441,7 @@ impl Default for Configuration {
disabled_commands: HashSet::default(),
allow_dm: true,
ignore_webhooks: true,
delimeter: " ".to_string(),
delimeters: vec![" ".to_string()],
}
}
}
7 changes: 6 additions & 1 deletion src/framework/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,8 +936,13 @@ impl ::Framework for BuiltinFramework {
if command.use_quotes {
utils::parse_quotes(&content[command_length..])
} else {
let delimeter = {
let delimeter = self.configuration.delimeters.iter().find(|d| content[command_length..].contains(d.as_str()));
delimeter.map(|s| s.as_str()).unwrap_or(" ")
};

content[command_length..]
.split(&self.configuration.delimeter)
.split(delimeter)
.map(|arg| arg.to_owned())
.collect::<Vec<String>>()
}
Expand Down

0 comments on commit fdfb184

Please sign in to comment.