diff options
| author | acdenisSK <[email protected]> | 2017-06-29 12:31:05 +0200 |
|---|---|---|
| committer | acdenisSK <[email protected]> | 2017-06-29 12:31:05 +0200 |
| commit | 35826915a174c7f3e5d82bbc320d3238ae308d8c (patch) | |
| tree | 7f59e067e778aa2eb6228eb0034bdcae46f7c1b1 /examples | |
| parent | Whoops, and add a fail-safe to an upcomming pr to the compiler (diff) | |
| download | serenity-35826915a174c7f3e5d82bbc320d3238ae308d8c.tar.xz serenity-35826915a174c7f3e5d82bbc320d3238ae308d8c.zip | |
Also update examples
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/01_basic_ping_bot/src/main.rs | 33 | ||||
| -rw-r--r-- | examples/02_transparent_guild_sharding/src/main.rs | 30 | ||||
| -rw-r--r-- | examples/03_struct_utilities/src/main.rs | 25 | ||||
| -rw-r--r-- | examples/04_message_builder/src/main.rs | 24 | ||||
| -rw-r--r-- | examples/06_voice/src/main.rs | 15 |
5 files changed, 78 insertions, 49 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. // diff --git a/examples/02_transparent_guild_sharding/src/main.rs b/examples/02_transparent_guild_sharding/src/main.rs index a4f6172..0fd5a60 100644 --- a/examples/02_transparent_guild_sharding/src/main.rs +++ b/examples/02_transparent_guild_sharding/src/main.rs @@ -1,6 +1,7 @@ extern crate serenity; -use serenity::Client; +use serenity::prelude::*; +use serenity::model::*; use std::env; // Serenity implements transparent sharding in a way that you do not need to @@ -21,14 +22,11 @@ use std::env; // print either "0" or "1" in the console. Saying "!ping" in the other guild, // it should cache the other number in the console. This confirms that guild // sharding works. -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"); - let mut client = Client::new(&token); +struct Handler; - client.on_message(|ctx, msg| { - if msg.content == "!ping" { +impl EventHandler for Handler { + fn on_message(&self, _: Context, msg: Message) { + if msg.content == "!ping" { // The current shard needs to be unlocked so it can be read from, as // multiple threads may otherwise attempt to read from or mutate it // concurrently. @@ -43,12 +41,20 @@ fn main() { if let Err(why) = msg.channel_id.say("Pong!") { println!("Error sending message: {:?}", why); } - } - }); + } + } - 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"); + let mut client = Client::new(&token, Handler); // The total number of shards to use. The "current shard number" of a // shard - that is, the shard it is assigned to - is indexed at 0, diff --git a/examples/03_struct_utilities/src/main.rs b/examples/03_struct_utilities/src/main.rs index 8802fba..5f777bd 100644 --- a/examples/03_struct_utilities/src/main.rs +++ b/examples/03_struct_utilities/src/main.rs @@ -1,15 +1,13 @@ 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"); - let mut client = Client::new(&token); +struct Handler; - client.on_message(|_ctx, msg| { +impl EventHandler for Handler { + fn on_message(&self, _: Context, msg: Message) { if msg.content == "!messageme" { // If the `methods` feature is enabled, then model structs will // have a lot of useful methods implemented, to avoid using an @@ -23,11 +21,18 @@ fn main() { println!("Error when direct messaging user: {:?}", why); } } - }); + } - 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"); + let mut client = Client::new(&token, Handler); if let Err(why) = client.start() { println!("Client error: {:?}", why); diff --git a/examples/04_message_builder/src/main.rs b/examples/04_message_builder/src/main.rs index e82d6b9..4fecb93 100644 --- a/examples/04_message_builder/src/main.rs +++ b/examples/04_message_builder/src/main.rs @@ -1,16 +1,14 @@ extern crate serenity; -use serenity::Client; +use serenity::prelude::*; +use serenity::model::*; use serenity::utils::MessageBuilder; 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"); - let mut client = Client::new(&token); +struct Handler; - client.on_message(|_ctx, msg| { +impl EventHandler for Handler { + fn on_message(&self, _: Context, msg: Message) { if msg.content == "!ping" { let channel = match msg.channel_id.get() { Ok(channel) => channel, @@ -37,11 +35,17 @@ fn main() { println!("Error sending message: {:?}", why); } } - }); + } - client.on_ready(|_context, 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"); + let mut client = Client::new(&token, Handler); if let Err(why) = client.start() { println!("Client error: {:?}", why); diff --git a/examples/06_voice/src/main.rs b/examples/06_voice/src/main.rs index b194548..d1a0c11 100644 --- a/examples/06_voice/src/main.rs +++ b/examples/06_voice/src/main.rs @@ -10,12 +10,21 @@ #[macro_use] extern crate serenity; -use serenity::client::{CACHE, Client}; -use serenity::ext::voice; -use serenity::model::{ChannelId, Message, Mentionable}; +use serenity::prelude::*; +use serenity::client::CACHE; +use serenity::voice; +use serenity::model::*; use serenity::Result as SerenityResult; use std::env; +struct Handler; + +impl EventHandler for Handler { + 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") |