diff options
| author | Illia <[email protected]> | 2016-12-13 21:26:29 +0200 |
|---|---|---|
| committer | zeyla <[email protected]> | 2016-12-13 11:26:29 -0800 |
| commit | daf92eda815b8f539f6d759ab48cf7a70513915f (patch) | |
| tree | 36145f5095e7af6fb725635dd104e9d9d3f0ea62 /src/ext/framework/create_group.rs | |
| parent | Fix readme typo (diff) | |
| download | serenity-daf92eda815b8f539f6d759ab48cf7a70513915f.tar.xz serenity-daf92eda815b8f539f6d759ab48cf7a70513915f.zip | |
Implement command groups and buckets
* 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
Diffstat (limited to 'src/ext/framework/create_group.rs')
| -rw-r--r-- | src/ext/framework/create_group.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/ext/framework/create_group.rs b/src/ext/framework/create_group.rs new file mode 100644 index 0000000..db2832b --- /dev/null +++ b/src/ext/framework/create_group.rs @@ -0,0 +1,58 @@ +pub use ext::framework::command::{Command, CommandType, CommandGroup}; +pub use ext::framework::create_command::CreateCommand; + +use std::default::Default; +use std::sync::Arc; +use ::client::Context; +use ::model::Message; + +#[derive(Default)] +pub struct CreateGroup(pub CommandGroup); + +/// Used to create command groups +/// +/// # Examples +/// +/// Create group named Information where all commands are prefixed with info, +/// and add one command named "name". For example, if prefix is "~", we say "~info name" +/// to call the "name" command. +/// +/// ```rust,ignore +/// framework.group("Information", |g| g +/// .prefix("info") +/// .command("name", |c| c +/// .exec_str("meew0"))) +/// ``` +impl CreateGroup { + /// If prefix is set, it will be required before all command names. + /// For example, if bot prefix is "~" and group prefix is "image" + /// we'd call a subcommand named "hibiki" by sending "~image hibiki". + /// + /// **Note**: serenity automatically puts a space after group prefix. + pub fn prefix(mut self, desc: &str) -> Self { + self.0.prefix = Some(desc.to_owned()); + + self + } + + /// Adds a command to group. + pub fn command<F, S>(mut self, command_name: S, f: F) -> Self + where F: FnOnce(CreateCommand) -> CreateCommand, + S: Into<String> { + let cmd = f(CreateCommand(Command::default())).0; + + self.0.commands.insert(command_name.into(), Arc::new(cmd)); + + self + } + + /// Adds a command to group with simplified API. + /// You can return Err(string) if there's an error. + pub fn on<F, S>(mut self, command_name: S, f: F) -> Self + where F: Fn(&Context, &Message, Vec<String>) -> Result<(), String> + Send + Sync + 'static, + S: Into<String> { + self.0.commands.insert(command_name.into(), Arc::new(Command::new(f))); + + self + } +} |