From d50b12931404946e219d3ff0878f0632445ef35f Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Sun, 29 Oct 2017 09:53:10 -0700 Subject: [PATCH] Add logging and dotenv to example 07 --- .gitignore | 1 + examples/07_sample_bot_structure/.env.example | 10 ++++++ examples/07_sample_bot_structure/Cargo.toml | 5 +++ examples/07_sample_bot_structure/src/main.rs | 35 ++++++++++++++++--- 4 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 examples/07_sample_bot_structure/.env.example diff --git a/.gitignore b/.gitignore index af3b590a19c..721444af625 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ Cargo.lock .idea/ *.iml +.env diff --git a/examples/07_sample_bot_structure/.env.example b/examples/07_sample_bot_structure/.env.example new file mode 100644 index 00000000000..3b3b4acfd01 --- /dev/null +++ b/examples/07_sample_bot_structure/.env.example @@ -0,0 +1,10 @@ +# This declares an environment variable named "DISCORD_TOKEN" with the given +# value. When calling `kankyo::load()`, it will read the `.env` file and parse +# these key-value pairs and insert them into the environment. +# +# Environment variables are separated by newlines and must not have space +# around the equals sign (`=`). +DISCORD_TOKEN=put your token here +# Declares the level of logging to use. Read the documentation for the `log` +# and `env_logger` crates for more information. +RUST_LOG=debug diff --git a/examples/07_sample_bot_structure/Cargo.toml b/examples/07_sample_bot_structure/Cargo.toml index 0bebb46d74c..b1dba53e3a5 100644 --- a/examples/07_sample_bot_structure/Cargo.toml +++ b/examples/07_sample_bot_structure/Cargo.toml @@ -3,6 +3,11 @@ name = "07_sample_bot_structure" version = "0.1.0" authors = ["my name "] +[dependencies] +env_logger = "~0.4" +kankyo = "~0.1" +log = "~0.3" + [dependencies.serenity] features = ["cache", "framework", "standard_framework"] path = "../../" diff --git a/examples/07_sample_bot_structure/src/main.rs b/examples/07_sample_bot_structure/src/main.rs index 8962d55b8dd..01f7a610e30 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); } }