Skip to content

Commit

Permalink
Add logging and dotenv to example 07
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeyla Hellyer committed Oct 29, 2017
1 parent e694766 commit d50b129
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Cargo.lock
.idea/

*.iml
.env
10 changes: 10 additions & 0 deletions examples/07_sample_bot_structure/.env.example
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions examples/07_sample_bot_structure/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ name = "07_sample_bot_structure"
version = "0.1.0"
authors = ["my name <[email protected]>"]

[dependencies]
env_logger = "~0.4"
kankyo = "~0.1"
log = "~0.3"

[dependencies.serenity]
features = ["cache", "framework", "standard_framework"]
path = "../../"
35 changes: 30 additions & 5 deletions examples/07_sample_bot_structure/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}
}

0 comments on commit d50b129

Please sign in to comment.