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 /examples/06_command_framework/src | |
| 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 'examples/06_command_framework/src')
| -rw-r--r-- | examples/06_command_framework/src/main.rs | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/examples/06_command_framework/src/main.rs b/examples/06_command_framework/src/main.rs index f4a3eea..494a453 100644 --- a/examples/06_command_framework/src/main.rs +++ b/examples/06_command_framework/src/main.rs @@ -15,6 +15,7 @@ extern crate typemap; use serenity::client::Context; use serenity::Client; use serenity::model::{Message, permissions}; +use serenity::ext::framework::help_commands; use std::collections::HashMap; use std::env; use std::fmt::Write; @@ -58,6 +59,7 @@ fn main() { .configure(|c| c .allow_whitespace(true) .on_mention(true) + .rate_limit_message("Try this again in `%time%` seconds.") .prefix("~")) // Set a function to be called prior to each command execution. This // provides the context of the command, the message that was received, @@ -81,17 +83,35 @@ fn main() { }) // Very similar to `before`, except this will be called directly _after_ // command execution. - .after(|_, _, command_name| { - println!("Processed command '{}'", command_name) + .after(|_, _, command_name, error| { + if let Some(why) = error { + println!("Command '{}' returned error {:?}", command_name, why); + } else { + println!("Processed command '{}'", command_name); + } }) + // Can't be used more than once per 5 seconds: + .simple_bucket("emoji", 5) + // Can't be used more than 2 times per 30 seconds, with a 5 second delay + .bucket("complicated", 5, 30, 2) .command("about", |c| c.exec_str("A test bot")) + .command("help", |c| c.exec_help(help_commands::plain)) .command("commands", |c| c - .check(owner_check) + // Make this command use the "complicated" bucket. + .bucket("complicated") .exec(commands)) - .command("emoji cat", |c| c - .exec_str(":cat:") - .required_permissions(permissions::SEND_MESSAGES)) - .command("emoji dog", |c| c.exec_str(":dog:")) + .group("Emoji", |g| g + .prefix("emoji") + .command("cat", |c| c + .desc("Sends an emoji with a cat.") + .bucket("emoji") // Make this command use the "emoji" bucket. + .exec_str(":cat:") + // Allow only administrators to call this: + .required_permissions(permissions::ADMINISTRATOR)) + .command("dog", |c| c + .desc("Sends an emoji with a dog.") + .bucket("emoji") + .exec_str(":dog:"))) .command("multiply", |c| c.exec(multiply)) .command("ping", |c| c .check(owner_check) @@ -134,11 +154,11 @@ fn owner_check(_: &Context, message: &Message) -> bool { message.author.id == 7 } -fn some_long_command(context: &Context, _: &Message, args: Vec<String>) { +command!(some_long_command(context, _msg, args) { if let Err(why) = context.say(&format!("Arguments: {:?}", args)) { println!("Error sending message: {:?}", why); } -} +}); // Using the `command!` macro, commands can be created with a certain type of // "dynamic" type checking. This is a method of requiring that the arguments |