Skip to content

Commit

Permalink
Also update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis committed Jun 29, 2017
1 parent e3db2f0 commit 3582691
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 50 deletions.
33 changes: 19 additions & 14 deletions examples/01_basic_ping_bot/src/main.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -28,17 +22,28 @@ 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
// contains data like the current user's guild Ids, current user data,
// 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.
//
Expand Down
30 changes: 18 additions & 12 deletions examples/02_transparent_guild_sharding/src/main.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand All @@ -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,
Expand Down
25 changes: 15 additions & 10 deletions examples/03_struct_utilities/src/main.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
Expand Down
24 changes: 14 additions & 10 deletions examples/04_message_builder/src/main.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
Expand Down
15 changes: 12 additions & 3 deletions examples/06_voice/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub use ::error::{Error as SerenityError};
pub use ::model::Mentionable;

#[cfg(feature="client")]
pub use ::client::{Client, ClientError as ClientError, EventHandler};
pub use ::client::{Context, Client, ClientError as ClientError, EventHandler};
#[cfg(feature="gateway")]
pub use ::gateway::GatewayError;
#[cfg(feature="http")]
Expand Down

0 comments on commit 3582691

Please sign in to comment.