aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorIllia K <[email protected]>2016-11-28 19:21:33 +0000
committerAustin Hellyer <[email protected]>2016-11-28 12:13:46 -0800
commit83515e1a5ccc850a8c352060a19394a53aed8454 (patch)
treee5531327c82e37ae8b6874ebcba4b81746b4dd1e /examples
parentImprove docs and add new message builder methods (diff)
downloadserenity-83515e1a5ccc850a8c352060a19394a53aed8454.tar.xz
serenity-83515e1a5ccc850a8c352060a19394a53aed8454.zip
Add before/after framework command hooks
These hooks will each be run prior to or after the command, and will finish execution before executing the command. These can be configured in a Framework via: ```rs client.with_framework(|f| f .before(|_context, message, _command_name| { println!("Got command '{}'", command_name); }) .after(|_context, _message, command_name| { println!("Finished command '{}'", command_name); })); ``` This does introduce a backwards compatibility break, by requiring commands' Context/Message to be borrowed Upgrade path: If not using the `command!` macro, modify command signatures from: ```rs fn some_command(context: Context, message: Message, args: Vec<String>) ``` to ```rs fn some_command(context: &Context, message: &Message, args: Vec<String>) ```
Diffstat (limited to 'examples')
-rw-r--r--examples/06_command_framework.rs20
1 files changed, 12 insertions, 8 deletions
diff --git a/examples/06_command_framework.rs b/examples/06_command_framework.rs
index af8781c..38f756d 100644
--- a/examples/06_command_framework.rs
+++ b/examples/06_command_framework.rs
@@ -12,10 +12,6 @@ fn main() {
.expect("Expected a token in the environment");
let mut client = Client::login_bot(&token);
- client.on_message(|_context, message| {
- println!("Received message: {:?}", message);
- });
-
client.on_ready(|_context, ready| {
println!("{} is connected!", ready.user.name);
});
@@ -28,9 +24,17 @@ fn main() {
// "~some complex command"
client.with_framework(|f| f
.configure(|c| c
- .on_mention(true)
.allow_whitespace(true)
+ .on_mention(true)
.prefix("~"))
+ .before(|_context, message, command_name| {
+ println!("Got command '{}' by user '{}'",
+ command_name,
+ message.author.name);
+ })
+ .after(|_context, _message, command_name| {
+ println!("Processed command '{}'", command_name)
+ })
.on("ping", ping_command)
.set_check("ping", owner_check) // Ensure only the owner can run this
.on("emoji cat", cat_command)
@@ -47,11 +51,11 @@ command!(cat_command(context, _msg, _arg) {
let _ = context.say(":cat:");
});
-fn dog_command(context: Context, _msg: Message, _args: Vec<String>) {
+fn dog_command(context: &Context, _msg: &Message, _args: Vec<String>) {
let _ = context.say(":dog:");
}
-fn ping_command(_context: Context, message: Message, _args: Vec<String>) {
+fn ping_command(_context: &Context, message: &Message, _args: Vec<String>) {
let _ = message.reply("Pong!");
}
@@ -60,7 +64,7 @@ fn owner_check(_context: &Context, message: &Message) -> bool {
message.author.id == 7
}
-fn some_complex_command(context: Context, _msg: Message, args: Vec<String>) {
+fn some_complex_command(context: &Context, _msg: &Message, args: Vec<String>) {
let _ = context.say(&format!("Arguments: {:?}", args));
}