diff options
| author | Austin Hellyer <[email protected]> | 2016-11-21 11:15:03 -0800 |
|---|---|---|
| committer | Austin Hellyer <[email protected]> | 2016-11-21 11:15:03 -0800 |
| commit | d5a1985f8bd57b3fb487b979188a56dccababa2b (patch) | |
| tree | fb4513b69f2cf60eda2b6e22f9ab1efa225e9da8 | |
| parent | No-run on context doc example (diff) | |
| download | serenity-d5a1985f8bd57b3fb487b979188a56dccababa2b.tar.xz serenity-d5a1985f8bd57b3fb487b979188a56dccababa2b.zip | |
Add framework command named arguments
| -rw-r--r-- | examples/06_command_framework.rs | 10 | ||||
| -rw-r--r-- | src/ext/framework/mod.rs | 23 |
2 files changed, 33 insertions, 0 deletions
diff --git a/examples/06_command_framework.rs b/examples/06_command_framework.rs index c7300aa..1324a2e 100644 --- a/examples/06_command_framework.rs +++ b/examples/06_command_framework.rs @@ -1,3 +1,4 @@ +#[macro_use] extern crate serenity; use serenity::client::Context; @@ -34,6 +35,7 @@ fn main() { .set_check("ping", owner_check) // Ensure only the owner can run this .on("emoji cat", cat_command) .on("emoji dog", dog_command) + .on("multiply", multiply) .on("some complex command", some_complex_command) // Commands can be in closure-form as well .on("about", |context, _message, _args| drop(context.say("A test bot")))); @@ -61,3 +63,11 @@ fn owner_check(_context: &Context, message: &Message) -> bool { fn some_complex_command(context: Context, _msg: Message, args: Vec<String>) { let _ = context.say(&format!("Arguments: {:?}", args)); } + +command!(multiply(context, _message, args, first: f64, second: f64) { + let res = first * second; + + let _ = context.say(&res.to_string()); + + println!("{:?}", args); +}); diff --git a/src/ext/framework/mod.rs b/src/ext/framework/mod.rs index 2c1ffb5..8c807dc 100644 --- a/src/ext/framework/mod.rs +++ b/src/ext/framework/mod.rs @@ -11,6 +11,29 @@ use std::thread; use ::client::Context; use ::model::Message; +#[macro_export] +macro_rules! command { + ($fname:ident($c:ident, $m:ident, $a:ident, $($name:ident: $t:ty),*) $b:block) => { + fn $fname($c: Context, $m: Message, $a: Vec<String>) { + let mut i = $a.iter(); + + $( + let $name = match i.next() { + Some(v) => match v.parse::<$t>() { + Ok(v) => v, + Err(_why) => return, + }, + None => return, + }; + )* + + drop(i); + + $b + } + } +} + /// The type of command being received. /// /// The [`Mention`] variant is emitted if the bot is being commanded via a |