From daf92eda815b8f539f6d759ab48cf7a70513915f Mon Sep 17 00:00:00 2001 From: Illia Date: Tue, 13 Dec 2016 21:26:29 +0200 Subject: Implement command groups and buckets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Implement command groups * change to ref mut * Implement framework API. * Remove commands field * Make it all work * Make example use command groups * Requested changes * Implement adding buckets * Add ratelimit check function * Finish everything * Fix voice example * Actually fix it * Fix doc tests * Switch to result * Savage examples * Fix docs * Fixes * Accidental push * 👀 * Fix an example * fix some example * Small cleanup * Abstract ratelimit bucket logic --- src/ext/framework/create_command.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'src/ext/framework/create_command.rs') diff --git a/src/ext/framework/create_command.rs b/src/ext/framework/create_command.rs index 6d40592..e3faffd 100644 --- a/src/ext/framework/create_command.rs +++ b/src/ext/framework/create_command.rs @@ -1,4 +1,4 @@ -pub use ext::framework::command::{Command, CommandType}; +pub use super::{Command, CommandType, CommandGroup}; use std::collections::HashMap; use std::default::Default; @@ -32,8 +32,10 @@ impl CreateCommand { /// .desc("Replies to a ping with a pong") /// .exec(ping))); /// - /// fn ping(context: &Context, _message: &Message, _args: Vec) { - /// context.say("Pong!"); + /// fn ping(context: &Context, _message: &Message, _args: Vec) -> Result<(), String> { + /// let _ = context.say("Pong!"); + /// + /// Ok(()) /// } /// /// fn owner_check(_context: &Context, message: &Message) -> bool { @@ -55,6 +57,13 @@ impl CreateCommand { self } + /// Adds a ratelimit bucket. + pub fn bucket(mut self, bucket: &str) -> Self { + self.0.bucket = Some(bucket.to_owned()); + + self + } + /// Whether command should be displayed in help list or not, used by other commands. pub fn help_available(mut self, help_available: bool) -> Self { self.0.help_available = help_available; @@ -98,12 +107,13 @@ impl CreateCommand { } /// A function that can be called when a command is received. + /// You can return Err(string) if there's an error. /// /// See [`exec_str`] if you _only_ need to return a string on command use. /// /// [`exec_str`]: #method.exec_str pub fn exec(mut self, func: F) -> Self - where F: Fn(&Context, &Message, Vec) + Send + Sync + 'static { + where F: Fn(&Context, &Message, Vec) -> Result<(), String> + Send + Sync + 'static { self.0.exec = CommandType::Basic(Box::new(func)); self @@ -112,8 +122,10 @@ impl CreateCommand { /// Sets a function that's called when a command is called that can access /// the internal HashMap of usages, used specifically for creating a help /// command. + /// + /// You can return Err(string) if there's an error. pub fn exec_help(mut self, f: F) -> Self - where F: Fn(&Context, &Message, HashMap>, Vec) + Send + Sync + 'static { + where F: Fn(&Context, &Message, HashMap>, Vec) -> Result<(), String> + Send + Sync + 'static { self.0.exec = CommandType::WithCommands(Box::new(f)); self @@ -165,11 +177,12 @@ impl Default for Command { fn default() -> Command { Command { checks: Vec::default(), - exec: CommandType::Basic(Box::new(|_, _, _| {})), + exec: CommandType::Basic(Box::new(|_, _, _| Ok(()))), desc: None, usage: None, use_quotes: true, min_args: None, + bucket: None, max_args: None, required_permissions: Permissions::empty(), dm_only: false, -- cgit v1.2.3