aboutsummaryrefslogtreecommitdiff
path: root/examples/07_sample_bot_structure
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-10-29 09:53:10 -0700
committerZeyla Hellyer <[email protected]>2017-10-29 09:53:10 -0700
commitd50b12931404946e219d3ff0878f0632445ef35f (patch)
treee411e56cdd94522dae108a86b12494069bd7f5a9 /examples/07_sample_bot_structure
parentFix #206 (#207) (diff)
downloadserenity-d50b12931404946e219d3ff0878f0632445ef35f.tar.xz
serenity-d50b12931404946e219d3ff0878f0632445ef35f.zip
Add logging and dotenv to example 07
Diffstat (limited to 'examples/07_sample_bot_structure')
-rw-r--r--examples/07_sample_bot_structure/.env.example10
-rw-r--r--examples/07_sample_bot_structure/Cargo.toml5
-rw-r--r--examples/07_sample_bot_structure/src/main.rs35
3 files changed, 45 insertions, 5 deletions
diff --git a/examples/07_sample_bot_structure/.env.example b/examples/07_sample_bot_structure/.env.example
new file mode 100644
index 0000000..3b3b4ac
--- /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 0bebb46..b1dba53 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 <[email protected]>"]
+[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 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);
}
}