aboutsummaryrefslogtreecommitdiff
path: root/examples/01_basic_ping_bot/src
diff options
context:
space:
mode:
authoracdenisSK <[email protected]>2017-06-29 12:31:05 +0200
committeracdenisSK <[email protected]>2017-06-29 12:31:05 +0200
commit35826915a174c7f3e5d82bbc320d3238ae308d8c (patch)
tree7f59e067e778aa2eb6228eb0034bdcae46f7c1b1 /examples/01_basic_ping_bot/src
parentWhoops, and add a fail-safe to an upcomming pr to the compiler (diff)
downloadserenity-35826915a174c7f3e5d82bbc320d3238ae308d8c.tar.xz
serenity-35826915a174c7f3e5d82bbc320d3238ae308d8c.zip
Also update examples
Diffstat (limited to 'examples/01_basic_ping_bot/src')
-rw-r--r--examples/01_basic_ping_bot/src/main.rs33
1 files changed, 19 insertions, 14 deletions
diff --git a/examples/01_basic_ping_bot/src/main.rs b/examples/01_basic_ping_bot/src/main.rs
index 25bc52a..4c2f35c 100644
--- a/examples/01_basic_ping_bot/src/main.rs
+++ b/examples/01_basic_ping_bot/src/main.rs
@@ -1,24 +1,18 @@
extern crate serenity;
-use serenity::Client;
+use serenity::prelude::*;
+use serenity::model::*;
use std::env;
-fn main() {
- // Configure the client with your Discord bot token in the environment.
- let token = env::var("DISCORD_TOKEN")
- .expect("Expected a token in the environment");
-
- // Create a new instance of the Client, logging in as a bot. This will
- // automatically prepend your bot token with "Bot ", which is a requirement
- // by Discord for bot users.
- let mut client = Client::new(&token);
+struct Handler;
+impl EventHandler for Handler {
// Set a handler for the `on_message` event - so that whenever a new message
// is received - the closure (or function) passed will be called.
//
// Event handlers are dispatched through multi-threading, and so multiple
// of a single event can be dispatched simultaneously.
- client.on_message(|_ctx, msg| {
+ fn on_message(&self, _: Context, msg: Message) {
if msg.content == "!ping" {
// Sending a message can fail, due to a network error, an
// authentication error, or lack of permissions to post in the
@@ -28,7 +22,7 @@ fn main() {
println!("Error sending message: {:?}", why);
}
}
- });
+ }
// Set a handler to be called on the `on_ready` event. This is called when a
// shard is booted, and a READY payload is sent by Discord. This payload
@@ -36,9 +30,20 @@ fn main() {
// private channels, and more.
//
// In this case, just print what the current user's username is.
- client.on_ready(|_ctx, ready| {
+ fn on_ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
- });
+ }
+}
+
+fn main() {
+ // Configure the client with your Discord bot token in the environment.
+ let token = env::var("DISCORD_TOKEN")
+ .expect("Expected a token in the environment");
+
+ // Create a new instance of the Client, logging in as a bot. This will
+ // automatically prepend your bot token with "Bot ", which is a requirement
+ // by Discord for bot users.
+ let mut client = Client::new(&token, Handler);
// Finally, start a single shard, and start listening to events.
//