aboutsummaryrefslogtreecommitdiff
path: root/examples/07_sample_bot_structure/src
diff options
context:
space:
mode:
authoracdenisSK <[email protected]>2017-11-04 20:06:40 +0100
committeracdenisSK <[email protected]>2017-11-04 20:06:40 +0100
commit0bd519f4ef9784d0fb5663d74db0d567f0bb1ae5 (patch)
treecc2a6f44e97e42420507964dab4662fcccd9beb3 /examples/07_sample_bot_structure/src
parentFix Help-Commands to list all eligible commands in DMs. (#212) (diff)
parentBump to v0.4.3 (diff)
downloadserenity-0bd519f4ef9784d0fb5663d74db0d567f0bb1ae5.tar.xz
serenity-0bd519f4ef9784d0fb5663d74db0d567f0bb1ae5.zip
Merge v0.4.3
Diffstat (limited to 'examples/07_sample_bot_structure/src')
-rw-r--r--examples/07_sample_bot_structure/src/commands/mod.rs1
-rw-r--r--examples/07_sample_bot_structure/src/commands/owner.rs10
-rw-r--r--examples/07_sample_bot_structure/src/main.rs57
3 files changed, 61 insertions, 7 deletions
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 7a0c216..4c0ff8b 100644
--- a/examples/07_sample_bot_structure/src/main.rs
+++ b/examples/07_sample_bot_structure/src/main.rs
@@ -9,29 +9,72 @@
//! 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 serenity::http;
+use std::collections::HashSet;
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 token = env::var("DISCORD_TOKEN")
.expect("Expected a token in the environment");
+
let mut client = Client::new(&token, Handler).expect("Err creating client");
+ 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() {
- println!("Client error: {:?}", why);
+ error!("Client error: {:?}", why);
}
}