diff options
| author | Zeyla Hellyer <[email protected]> | 2017-03-25 21:42:39 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-03-25 21:42:39 -0700 |
| commit | 40d16be431ed00367e5056c8393b0bfe6f6bb8d4 (patch) | |
| tree | fb584617b548414a774fff3e031cb8c5677f6b2e /examples | |
| parent | Improve some framework docs (diff) | |
| download | serenity-40d16be431ed00367e5056c8393b0bfe6f6bb8d4.tar.xz serenity-40d16be431ed00367e5056c8393b0bfe6f6bb8d4.zip | |
Add a sample bot structure example
Diffstat (limited to 'examples')
5 files changed, 61 insertions, 0 deletions
diff --git a/examples/08_sample_bot_structure/Cargo.toml b/examples/08_sample_bot_structure/Cargo.toml new file mode 100644 index 0000000..f9dacfe --- /dev/null +++ b/examples/08_sample_bot_structure/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "08_sample_bot_structure" +version = "0.1.0" +authors = ["my name <[email protected]>"] + +[dependencies.serenity] +features = ["cache", "framework"] +path = "../../" diff --git a/examples/08_sample_bot_structure/src/commands/math.rs b/examples/08_sample_bot_structure/src/commands/math.rs new file mode 100644 index 0000000..79bfc52 --- /dev/null +++ b/examples/08_sample_bot_structure/src/commands/math.rs @@ -0,0 +1,5 @@ +command!(multiply(_ctx, msg, _args, one: f64, two: f64) { + let product = one * two; + + let _ = msg.channel_id.say(&product.to_string()); +}); diff --git a/examples/08_sample_bot_structure/src/commands/meta.rs b/examples/08_sample_bot_structure/src/commands/meta.rs new file mode 100644 index 0000000..a4036fc --- /dev/null +++ b/examples/08_sample_bot_structure/src/commands/meta.rs @@ -0,0 +1,14 @@ +command!(latency(ctx, msg) { + let latency = ctx.shard.lock() + .unwrap() + .latency() + .map_or_else(|| "N/A".to_owned(), |s| { + format!("{}.{}s", s.as_secs(), s.subsec_nanos()) + }); + + let _ = msg.channel_id.say(&latency); +}); + +command!(ping(_ctx, msg) { + let _ = msg.channel_id.say("Pong!"); +}); diff --git a/examples/08_sample_bot_structure/src/commands/mod.rs b/examples/08_sample_bot_structure/src/commands/mod.rs new file mode 100644 index 0000000..bf58dba --- /dev/null +++ b/examples/08_sample_bot_structure/src/commands/mod.rs @@ -0,0 +1,2 @@ +pub mod math; +pub mod meta; diff --git a/examples/08_sample_bot_structure/src/main.rs b/examples/08_sample_bot_structure/src/main.rs new file mode 100644 index 0000000..6228782 --- /dev/null +++ b/examples/08_sample_bot_structure/src/main.rs @@ -0,0 +1,32 @@ +//! Requires the 'framework' feature flag be enabled in your project's +//! `Cargo.toml`. +//! +//! This can be enabled by specifying the feature in the dependency section: +//! +//! ```toml +//! [dependencies.serenity] +//! git = "https://github.com/zeyla/serenity.git" +//! features = ["framework"] +//! ``` + +#[macro_use] +extern crate serenity; + +mod commands; + +use serenity::Client; +use std::env; + +fn main() { + let mut client = Client::login_bot(&env::var("DISCORD_TOKEN").unwrap()); + + client.with_framework(|f| f + .configure(|c| c.prefix("~")) + .command("ping", |c| c.exec(commands::meta::ping)) + .command("latency", |c| c.exec(commands::meta::latency)) + .command("multiply", |c| c.exec(commands::math::multiply))); + + if let Err(why) = client.start() { + println!("Client error: {:?}", why); + } +} |