From d50b12931404946e219d3ff0878f0632445ef35f Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Sun, 29 Oct 2017 09:53:10 -0700 Subject: Add logging and dotenv to example 07 --- examples/07_sample_bot_structure/src/main.rs | 35 ++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'examples/07_sample_bot_structure/src') diff --git a/examples/07_sample_bot_structure/src/main.rs b/examples/07_sample_bot_structure/src/main.rs index 8962d55..01f7a61 100644 --- a/examples/07_sample_bot_structure/src/main.rs +++ b/examples/07_sample_bot_structure/src/main.rs @@ -9,18 +9,43 @@ //! features = ["framework", "standard_framework"] //! ``` -#[macro_use] -extern crate serenity; +#[macro_use] extern crate log; +#[macro_use] extern crate serenity; + +extern crate env_logger; +extern crate kankyo; mod commands; -use serenity::prelude::*; use serenity::framework::StandardFramework; +use serenity::model::event::ResumedEvent; +use serenity::model::Ready; +use serenity::prelude::*; use std::env; -struct Handler; impl EventHandler for Handler {} +struct Handler; + +impl EventHandler for Handler { + fn on_ready(&self, _: Context, ready: Ready) { + info!("Connected as {}", ready.user.name); + } + + fn on_resume(&self, _: Context, _: ResumedEvent) { + info!("Resumed"); + } +} fn main() { + // This will load the environment variables located at `./.env`, relative to + // the CWD. See `./.env.example` for an example on how to structure this. + kankyo::load().expect("Failed to load .env file"); + + // Initialize the logger to use environment variables. + // + // In this case, a good default is setting the environment variable + // `RUST_LOG` to debug`. + env_logger::init().expect("Failed to initialize env_logger"); + let mut client = Client::new(&env::var("DISCORD_TOKEN").unwrap(), Handler); client.with_framework(StandardFramework::new() @@ -30,6 +55,6 @@ fn main() { .command("multiply", |c| c.exec(commands::math::multiply))); if let Err(why) = client.start() { - println!("Client error: {:?}", why); + error!("Client error: {:?}", why); } } -- cgit v1.2.3 From 41f26b3757c7a5fba1f09f34e3192e2fd9702a4a Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Sun, 29 Oct 2017 11:56:25 -0700 Subject: Add owner + quit functionality to example 07 --- .../07_sample_bot_structure/src/commands/mod.rs | 1 + .../07_sample_bot_structure/src/commands/owner.rs | 10 ++++++++++ examples/07_sample_bot_structure/src/main.rs | 21 +++++++++++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 examples/07_sample_bot_structure/src/commands/owner.rs (limited to 'examples/07_sample_bot_structure/src') diff --git a/examples/07_sample_bot_structure/src/commands/mod.rs b/examples/07_sample_bot_structure/src/commands/mod.rs index bf58dba..9c5dfaa 100644 --- a/examples/07_sample_bot_structure/src/commands/mod.rs +++ b/examples/07_sample_bot_structure/src/commands/mod.rs @@ -1,2 +1,3 @@ pub mod math; pub mod meta; +pub mod owner; diff --git a/examples/07_sample_bot_structure/src/commands/owner.rs b/examples/07_sample_bot_structure/src/commands/owner.rs new file mode 100644 index 0000000..e80c19d --- /dev/null +++ b/examples/07_sample_bot_structure/src/commands/owner.rs @@ -0,0 +1,10 @@ +command!(quit(ctx, msg, _args) { + match ctx.quit() { + Ok(()) => { + let _ = msg.reply("Shutting down!"); + }, + Err(why) => { + let _ = msg.reply(&format!("Failed to shutdown: {:?}", why)); + }, + } +}); diff --git a/examples/07_sample_bot_structure/src/main.rs b/examples/07_sample_bot_structure/src/main.rs index 01f7a61..37f6c98 100644 --- a/examples/07_sample_bot_structure/src/main.rs +++ b/examples/07_sample_bot_structure/src/main.rs @@ -21,6 +21,8 @@ use serenity::framework::StandardFramework; use serenity::model::event::ResumedEvent; use serenity::model::Ready; use serenity::prelude::*; +use serenity::http; +use std::collections::HashSet; use std::env; struct Handler; @@ -48,11 +50,26 @@ fn main() { let mut client = Client::new(&env::var("DISCORD_TOKEN").unwrap(), Handler); + let owners = match http::get_current_application_info() { + Ok(info) => { + let mut set = HashSet::new(); + set.insert(info.owner.id); + + set + }, + Err(why) => panic!("Couldn't get application info: {:?}", why), + }; + client.with_framework(StandardFramework::new() - .configure(|c| c.prefix("~")) + .configure(|c| c + .owners(owners) + .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))); + .command("multiply", |c| c.exec(commands::math::multiply)) + .command("quit", |c| c + .exec(commands::owner::quit) + .owners_only(true))); if let Err(why) = client.start() { error!("Client error: {:?}", why); -- cgit v1.2.3