aboutsummaryrefslogtreecommitdiff
path: root/examples/05_command_framework/src
diff options
context:
space:
mode:
authorIllia <[email protected]>2017-04-20 00:51:34 +0300
committerZeyla Hellyer <[email protected]>2017-04-19 14:51:34 -0700
commit31aae7d12763f94a7a08ea9fd0102921e8402241 (patch)
tree76e0b54954ee02140ad6bd409a73e16943bda5f8 /examples/05_command_framework/src
parentFix example in README (diff)
downloadserenity-31aae7d12763f94a7a08ea9fd0102921e8402241.tar.xz
serenity-31aae7d12763f94a7a08ea9fd0102921e8402241.zip
Update the way errors are handled in dispatch
Diffstat (limited to 'examples/05_command_framework/src')
-rw-r--r--examples/05_command_framework/src/main.rs15
1 files changed, 13 insertions, 2 deletions
diff --git a/examples/05_command_framework/src/main.rs b/examples/05_command_framework/src/main.rs
index fbab6ec..c8abb9d 100644
--- a/examples/05_command_framework/src/main.rs
+++ b/examples/05_command_framework/src/main.rs
@@ -16,7 +16,7 @@ extern crate typemap;
use serenity::client::Context;
use serenity::Client;
use serenity::model::{Message, permissions};
-use serenity::ext::framework::help_commands;
+use serenity::ext::framework::{DispatchError, help_commands};
use std::collections::HashMap;
use std::env;
use std::fmt::Write;
@@ -60,7 +60,6 @@ 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,
@@ -92,6 +91,18 @@ fn main() {
Err(why) => println!("Command '{}' returned error {:?}", command_name, why),
}
})
+ // Set a function that's called whenever a command's execution didn't complete for one
+ // reason or another. For example, when a user has exceeded a rate-limit or a command
+ // can only be performed by the bot owner.
+ .on_dispatch_error(|_ctx, msg, error| {
+ match error {
+ DispatchError::RateLimited(seconds) => {
+ let _ = msg.channel_id.say(&format!("Try this again in {} seconds.", seconds));
+ },
+ // Any other error would be silently ignored.
+ _ => {},
+ }
+ })
// 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: