diff options
| author | Austin Hellyer <[email protected]> | 2016-12-05 07:46:22 -0800 |
|---|---|---|
| committer | Austin Hellyer <[email protected]> | 2016-12-05 07:46:22 -0800 |
| commit | ccb9d16e5dbe965e5a604e1cb402cd3bc7de0df5 (patch) | |
| tree | b7eed1952ae14f215074452c3e3d1bc081a89b0a /examples | |
| parent | Add more Context docs+permission requirements (diff) | |
| download | serenity-ccb9d16e5dbe965e5a604e1cb402cd3bc7de0df5.tar.xz serenity-ccb9d16e5dbe965e5a604e1cb402cd3bc7de0df5.zip | |
Add a ShareMap across contexts
The context now exposes, through the Client, a `data` field, which can
be accessed safely across contexts. This allows for a custom "shared
state" without the need for (ab)using lazy-static.rs.
Refer to example 06 for an example on how to use shared data.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/06_command_framework/Cargo.toml | 3 | ||||
| -rw-r--r-- | examples/06_command_framework/src/main.rs | 44 |
2 files changed, 45 insertions, 2 deletions
diff --git a/examples/06_command_framework/Cargo.toml b/examples/06_command_framework/Cargo.toml index 481dd6f..a73f940 100644 --- a/examples/06_command_framework/Cargo.toml +++ b/examples/06_command_framework/Cargo.toml @@ -3,6 +3,9 @@ name = "06_command_framework" version = "0.1.0" authors = ["my name <[email protected]>"] +[dependencies] +typemap = "0.3" + [dependencies.serenity] features = ["framework", "methods"] version = "0.1" diff --git a/examples/06_command_framework/src/main.rs b/examples/06_command_framework/src/main.rs index b399dff..0943d81 100644 --- a/examples/06_command_framework/src/main.rs +++ b/examples/06_command_framework/src/main.rs @@ -10,11 +10,21 @@ #[macro_use] extern crate serenity; +extern crate typemap; use serenity::client::Context; use serenity::Client; use serenity::model::Message; +use std::collections::HashMap; use std::env; +use std::fmt::Write; +use typemap::Key; + +struct CommandCounter; + +impl Key for CommandCounter { + type Value = HashMap<String, u64>; +} fn main() { // Configure the client with your Discord bot token in the environment. @@ -22,6 +32,11 @@ fn main() { .expect("Expected a token in the environment"); let mut client = Client::login_bot(&token); + { + let mut data = client.data.lock().unwrap(); + data.insert::<CommandCounter>(HashMap::default()); + } + client.on_ready(|_context, ready| { println!("{} is connected!", ready.user.name); }); @@ -51,16 +66,26 @@ fn main() { // You can not use this to determine whether a command should be // executed. Instead, `set_check` is provided to give you this // functionality. - .before(|_context, message, command_name| { + .before(|context, message, command_name| { println!("Got command '{}' by user '{}'", command_name, message.author.name); + + // Increment the number of times this command has been run once. If + // the command's name does not exist in the counter, add a default + // value of 0. + let mut data = context.data.lock().unwrap(); + let counter = data.get_mut::<CommandCounter>().unwrap(); + let entry = counter.entry(command_name.clone()).or_insert(0); + *entry += 1; }) // Very similar to `before`, except this will be called directly _after_ // command execution. .after(|_context, _message, command_name| { println!("Processed command '{}'", command_name) }) + .on("commands", commands) + .set_check("commands", owner_check) .on("ping", ping_command) .set_check("ping", owner_check) // Ensure only the owner can run this .on("emoji cat", cat_command) @@ -84,12 +109,27 @@ fn main() { // This may bring more features available for commands in the future. See the // "multiply" command below for some of the power that the `command!` macro can // bring. -command!(cat_command(context, _msg, _arg) { +command!(cat_command(context, _msg, _args) { if let Err(why) = context.say(":cat:") { println!("Eror sending message: {:?}", why); } }); +command!(commands(context, _msg, _args) { + let mut contents = "Commands used:\n".to_owned(); + + let data = context.data.lock().unwrap(); + let counter = data.get::<CommandCounter>().unwrap(); + + for (k, v) in counter { + let _ = write!(contents, "- {name}: {amount}\n", name=k, amount=v); + } + + if let Err(why) = context.say(&contents) { + println!("Error sending message: {:?}", why); + } +}); + fn dog_command(context: &Context, _msg: &Message, _args: Vec<String>) { if let Err(why) = context.say(":dog:") { println!("Error sending message: {:?}", why); |