diff options
| author | Austin Hellyer <[email protected]> | 2016-11-07 13:52:50 -0800 |
|---|---|---|
| committer | Austin Hellyer <[email protected]> | 2016-11-07 13:52:50 -0800 |
| commit | bcdd4f125d029c6d4c8a3ca79ba3ebecdb9515ab (patch) | |
| tree | 77e6edee15c415ec176f507ba44a3a00068c17e1 | |
| parent | Re-order MessageType definition (diff) | |
| download | serenity-bcdd4f125d029c6d4c8a3ca79ba3ebecdb9515ab.tar.xz serenity-bcdd4f125d029c6d4c8a3ca79ba3ebecdb9515ab.zip | |
Add arguments to framework commands
| -rw-r--r-- | examples/06_command_framework.rs | 12 | ||||
| -rw-r--r-- | src/ext/framework/command.rs | 2 | ||||
| -rw-r--r-- | src/ext/framework/mod.rs | 9 |
3 files changed, 14 insertions, 9 deletions
diff --git a/examples/06_command_framework.rs b/examples/06_command_framework.rs index 1bd3ffd..cb838fd 100644 --- a/examples/06_command_framework.rs +++ b/examples/06_command_framework.rs @@ -36,20 +36,20 @@ fn main() { .on("emoji dog", dog_command) .on("some complex command", some_complex_command) // Commands can be in closure-form as well - .on("about", |context, _message| drop(context.say("A test bot")))); + .on("about", |context, _message, _args| drop(context.say("A test bot")))); let _ = client.start(); } -fn cat_command(context: Context, _message: Message) { +fn cat_command(context: Context, _msg: Message, _args: Vec<String>) { let _ = context.say(":cat:"); } -fn dog_command(context: Context, _message: Message) { +fn dog_command(context: Context, _msg: Message, _args: Vec<String>) { let _ = context.say(":dog:"); } -fn ping_command(_context: Context, message: Message) { +fn ping_command(_context: Context, message: Message, _args: Vec<String>) { let _ = message.reply("Pong!"); } @@ -58,6 +58,6 @@ fn owner_check(_context: &Context, message: &Message) -> bool { message.author.id.0 == 7u64 } -fn some_complex_command(context: Context, _message: Message) { - let _ = context.say("This is a command in a complex group"); +fn some_complex_command(context: Context, _msg: Message, args: Vec<String>) { + let _ = context.say(&format!("Arguments: {:?}", args)); } diff --git a/src/ext/framework/command.rs b/src/ext/framework/command.rs index 31b2520..43d3c9a 100644 --- a/src/ext/framework/command.rs +++ b/src/ext/framework/command.rs @@ -2,6 +2,6 @@ use std::sync::Arc; use ::client::Context; use ::model::Message; -pub type Command = Fn(Context, Message) + Send + Sync; +pub type Command = Fn(Context, Message, Vec<String>) + Send + Sync; #[doc(hidden)] pub type InternalCommand = Arc<Command>; diff --git a/src/ext/framework/mod.rs b/src/ext/framework/mod.rs index c504d53..1949e24 100644 --- a/src/ext/framework/mod.rs +++ b/src/ext/framework/mod.rs @@ -113,7 +113,12 @@ impl Framework { let command = command.clone(); thread::spawn(move || { - (command)(context, message) + let args = message.content[built.len() + 1..] + .split_whitespace() + .map(|arg| arg.to_owned()) + .collect::<Vec<String>>(); + + (command)(context, message, args) }); return; @@ -123,7 +128,7 @@ impl Framework { } pub fn on<F, S>(mut self, command_name: S, f: F) -> Self - where F: Fn(Context, Message) + Send + Sync + 'static, + where F: Fn(Context, Message, Vec<String>) + Send + Sync + 'static, S: Into<String> { self.commands.insert(command_name.into(), Arc::new(f)); self.initialized = true; |