diff options
Diffstat (limited to 'src/framework')
| -rw-r--r-- | src/framework/standard/args.rs | 7 | ||||
| -rw-r--r-- | src/framework/standard/command.rs | 20 | ||||
| -rw-r--r-- | src/framework/standard/create_command.rs | 18 | ||||
| -rw-r--r-- | src/framework/standard/create_group.rs | 8 | ||||
| -rw-r--r-- | src/framework/standard/help_commands.rs | 25 | ||||
| -rw-r--r-- | src/framework/standard/mod.rs | 22 |
6 files changed, 44 insertions, 56 deletions
diff --git a/src/framework/standard/args.rs b/src/framework/standard/args.rs index 7fb82e4..6186cd4 100644 --- a/src/framework/standard/args.rs +++ b/src/framework/standard/args.rs @@ -64,7 +64,12 @@ pub struct Args { } impl Args { - pub fn new(message: &str, delimiter: &str) -> Self { + pub fn new(message: &str, possible_delimiters: Vec<String>) -> Self { + let delimiter = possible_delimiters + .iter() + .find(|&d| message.contains(d)) + .map_or(possible_delimiters[0].as_str(), |s| s.as_str()); + let split = if message.trim().is_empty() { Vec::new() } else { diff --git a/src/framework/standard/command.rs b/src/framework/standard/command.rs index 6330810..5910684 100644 --- a/src/framework/standard/command.rs +++ b/src/framework/standard/command.rs @@ -9,12 +9,9 @@ pub type Check = Fn(&mut Context, &Message, &mut Args, &Arc<Command>) -> bool + Send + Sync + 'static; -pub type Exec = Fn(&mut Context, &Message, Args) -> Result<(), Error> + Send + Sync + 'static; -pub type Help = Fn(&mut Context, &Message, HashMap<String, Arc<CommandGroup>>, Args) - -> Result<(), Error> - + Send - + Sync - + 'static; +pub type Exec = fn(&mut Context, &Message, Args) -> Result<(), Error>; +pub type Help = fn(&mut Context, &Message, HashMap<String, Arc<CommandGroup>>, Args) + -> Result<(), Error>; pub type BeforeHook = Fn(&mut Context, &Message, &str) -> bool + Send + Sync + 'static; pub type AfterHook = Fn(&mut Context, &Message, &str, Result<(), Error>) + Send + Sync + 'static; pub(crate) type InternalCommand = Arc<Command>; @@ -40,8 +37,8 @@ impl<D: fmt::Display> From<D> for Error { /// your commands. pub enum CommandType { StringResponse(String), - Basic(Box<Exec>), - WithCommands(Box<Help>), + Basic(Exec), + WithCommands(Help), } pub struct CommandGroup { @@ -92,10 +89,9 @@ pub struct Command { } impl Command { - pub fn new<F>(f: F) -> Self - where F: Fn(&mut Context, &Message, Args) -> Result<(), Error> + Send + Sync + 'static { + pub fn new(f: fn(&mut Context, &Message, Args) -> Result<(), Error>) -> Self { Command { - exec: CommandType::Basic(Box::new(f)), + exec: CommandType::Basic(f), ..Command::default() } } @@ -106,7 +102,7 @@ impl Default for Command { Command { aliases: Vec::new(), checks: Vec::default(), - exec: CommandType::Basic(Box::new(|_, _, _| Ok(()))), + exec: CommandType::Basic(|_, _, _| Ok(())), desc: None, usage: None, example: None, diff --git a/src/framework/standard/create_command.rs b/src/framework/standard/create_command.rs index 9df9c82..e4665e8 100644 --- a/src/framework/standard/create_command.rs +++ b/src/framework/standard/create_command.rs @@ -106,9 +106,8 @@ impl CreateCommand { /// See [`exec_str`] if you _only_ need to return a string on command use. /// /// [`exec_str`]: #method.exec_str - pub fn exec<F>(mut self, func: F) -> Self - where F: Fn(&mut Context, &Message, Args) -> Result<(), CommandError> + Send + Sync + 'static { - self.0.exec = CommandType::Basic(Box::new(func)); + pub fn exec(mut self, func: fn(&mut Context, &Message, Args) -> Result<(), CommandError>) -> Self { + self.0.exec = CommandType::Basic(func); self } @@ -117,14 +116,11 @@ impl CreateCommand { /// the internal HashMap of commands, used specifically for creating a help /// command. /// - /// You can return `Err(Custom(string))` if there's an error. - pub fn exec_help<F>(mut self, f: F) -> Self - where F: Fn(&mut Context, &Message, HashMap<String, Arc<CommandGroup>>, Args) - -> Result<(), CommandError> - + Send - + Sync - + 'static { - self.0.exec = CommandType::WithCommands(Box::new(f)); + /// You can return `Err(From::from(string))` if there's an error. + pub fn exec_help(mut self, f: + fn(&mut Context, &Message, HashMap<String, Arc<CommandGroup>>, Args) + -> Result<(), CommandError>) -> Self { + self.0.exec = CommandType::WithCommands(f); self } diff --git a/src/framework/standard/create_group.rs b/src/framework/standard/create_group.rs index 911d2ea..39fbcc6 100644 --- a/src/framework/standard/create_group.rs +++ b/src/framework/standard/create_group.rs @@ -70,14 +70,14 @@ impl CreateGroup { } /// Adds a command to group with simplified API. - /// You can return Err(string) if there's an error. - pub fn on<F>(mut self, command_name: &str, f: F) -> Self - where F: Fn(&mut Context, &Message, Args) -> Result<(), CommandError> + Send + Sync + 'static { + /// You can return Err(From::from(string)) if there's an error. + pub fn on(mut self, name: &str, + f: fn(&mut Context, &Message, Args) -> Result<(), CommandError>) -> Self { let cmd = Arc::new(Command::new(f)); self.0 .commands - .insert(command_name.to_string(), CommandOrAlias::Command(cmd)); + .insert(name.to_string(), CommandOrAlias::Command(cmd)); self } diff --git a/src/framework/standard/help_commands.rs b/src/framework/standard/help_commands.rs index 6b85239..ef7732b 100644 --- a/src/framework/standard/help_commands.rs +++ b/src/framework/standard/help_commands.rs @@ -55,7 +55,7 @@ fn remove_aliases(cmds: &HashMap<String, CommandOrAlias>) -> HashMap<&String, &I /// and given the required permissions. pub fn has_all_requirements(cmd: &Command, msg: &Message) -> bool { if let Some(guild) = msg.guild() { - let guild = guild.read().unwrap(); + let guild = guild.read(); if let Some(member) = guild.members.get(&msg.author.id) { @@ -155,26 +155,25 @@ pub fn with_embeds(_: &mut Context, } if let Some(ref usage) = command.usage { - embed = embed.field(|f| { - f.name("Usage") - .value(&format!("`{} {}`", command_name, usage)) - }); + let value = format!("`{} {}`", command_name, usage); + + embed = embed.field("Usage", value, true); } if let Some(ref example) = command.example { - embed = embed.field(|f| { - f.name("Sample usage") - .value(&format!("`{} {}`", command_name, example)) - }); + let value = format!("`{} {}`", command_name, example); + + embed = embed.field("Sample usage", value, true); } if group_name != "Ungrouped" { - embed = embed.field(|f| f.name("Group").value(&group_name)); + embed = embed.field("Group", group_name, true); } if !command.aliases.is_empty() { let aliases = command.aliases.join(", "); - embed = embed.field(|f| f.name("Aliases").value(&aliases)); + + embed = embed.field("Aliases", aliases, true); } let available = if command.dm_only { @@ -185,7 +184,7 @@ pub fn with_embeds(_: &mut Context, "In DM and guilds" }; - embed = embed.field(|f| f.name("Available").value(available)); + embed = embed.field("Available", available, true); embed }) @@ -235,7 +234,7 @@ pub fn with_embeds(_: &mut Context, } if has_commands { - e = e.field(|f| f.name(group_name).value(&desc)); + e = e.field(&group_name[..], &desc[..], true); } } e diff --git a/src/framework/standard/mod.rs b/src/framework/standard/mod.rs index 1a7a5ee..36570af 100644 --- a/src/framework/standard/mod.rs +++ b/src/framework/standard/mod.rs @@ -425,7 +425,7 @@ impl StandardFramework { #[cfg(feature = "cache")] fn is_blocked_guild(&self, message: &Message) -> bool { - if let Some(Channel::Guild(channel)) = CACHE.read().unwrap().channel(message.channel_id) { + if let Some(Channel::Guild(channel)) = CACHE.read().channel(message.channel_id) { let guild_id = channel.with(|g| g.guild_id); if self.configuration.blocked_guilds.contains(&guild_id) { return true; @@ -536,7 +536,7 @@ impl StandardFramework { } else { if !command.allowed_roles.is_empty() { if let Some(guild) = message.guild() { - let guild = guild.read().unwrap(); + let guild = guild.read(); if let Some(member) = guild.members.get(&message.author.id) { if let Ok(permissions) = member.permissions() { @@ -603,20 +603,18 @@ impl StandardFramework { /// }); /// # } /// ``` - pub fn on<F, S>(mut self, command_name: S, f: F) -> Self - where F: Fn(&mut Context, &Message, Args) -> Result<(), CommandError> + Send + Sync + 'static, - S: Into<String> { + pub fn on(mut self, name: &str, + f: fn(&mut Context, &Message, Args) + -> Result<(), CommandError>) -> Self { { let ungrouped = self.groups .entry("Ungrouped".to_string()) .or_insert_with(|| Arc::new(CommandGroup::default())); if let Some(ref mut group) = Arc::get_mut(ungrouped) { - let name = command_name.into(); - group .commands - .insert(name, CommandOrAlias::Command(Arc::new(Command::new(f)))); + .insert(name.to_string(), CommandOrAlias::Command(Arc::new(Command::new(f)))); } } @@ -910,13 +908,7 @@ impl Framework for StandardFramework { let mut content = message.content[position..].trim(); content = content[command_length..].trim(); - let delimiter = self.configuration - .delimiters - .iter() - .find(|&d| content.contains(d)) - .map_or(" ", |s| s.as_str()); - - Args::new(content, delimiter) + Args::new(&content, self.configuration.delimiters.clone()) }; if let Some(error) = self.should_fail( |