diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/01_basic_ping_bot/src/main.rs | 6 | ||||
| -rw-r--r-- | examples/02_transparent_guild_sharding/src/main.rs | 17 | ||||
| -rw-r--r-- | examples/03_struct_utilities/src/main.rs | 6 | ||||
| -rw-r--r-- | examples/04_message_builder/src/main.rs | 6 | ||||
| -rw-r--r-- | examples/05_command_framework/src/main.rs | 6 | ||||
| -rw-r--r-- | examples/06_voice/src/main.rs | 32 | ||||
| -rw-r--r-- | examples/07_sample_bot_structure/src/main.rs | 5 | ||||
| -rw-r--r-- | examples/08_env_logging/src/main.rs | 6 |
8 files changed, 40 insertions, 44 deletions
diff --git a/examples/01_basic_ping_bot/src/main.rs b/examples/01_basic_ping_bot/src/main.rs index 4c2f35c..a900cc2 100644 --- a/examples/01_basic_ping_bot/src/main.rs +++ b/examples/01_basic_ping_bot/src/main.rs @@ -12,7 +12,7 @@ impl EventHandler for Handler { // // Event handlers are dispatched through multi-threading, and so multiple // of a single event can be dispatched simultaneously. - fn on_message(&self, _: Context, msg: Message) { + fn 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 @@ -30,7 +30,7 @@ impl EventHandler for Handler { // private channels, and more. // // In this case, just print what the current user's username is. - fn on_ready(&self, _: Context, ready: Ready) { + fn ready(&self, _: Context, ready: Ready) { println!("{} is connected!", ready.user.name); } } @@ -43,7 +43,7 @@ fn main() { // 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); + let mut client = Client::new(&token, Handler).expect("Err creating client"); // 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 55b20c1..c1a5bbb 100644 --- a/examples/02_transparent_guild_sharding/src/main.rs +++ b/examples/02_transparent_guild_sharding/src/main.rs @@ -25,24 +25,17 @@ use std::env; struct Handler; impl EventHandler for Handler { - fn on_message(&self, ctx: Context, msg: Message) { + fn message(&self, ctx: 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. - { - let shard = ctx.shard.lock(); - let shard_info = shard.shard_info(); - println!("Shard {}", shard_info[0]); - } + println!("Shard {}", ctx.shard_id); if let Err(why) = msg.channel_id.say("Pong!") { println!("Error sending message: {:?}", why); } - } + } } - fn on_ready(&self, _: Context, ready: Ready) { + fn ready(&self, _: Context, ready: Ready) { println!("{} is connected!", ready.user.name); } } @@ -52,7 +45,7 @@ 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); + let mut client = Client::new(&token, Handler).expect("Err creating client"); // 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 e08cf00..4aafb07 100644 --- a/examples/03_struct_utilities/src/main.rs +++ b/examples/03_struct_utilities/src/main.rs @@ -7,7 +7,7 @@ use std::env; struct Handler; impl EventHandler for Handler { - fn on_message(&self, _: Context, msg: Message) { + fn 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,7 +23,7 @@ impl EventHandler for Handler { } } - fn on_ready(&self, _: Context, ready: Ready) { + fn ready(&self, _: Context, ready: Ready) { println!("{} is connected!", ready.user.name); } } @@ -32,7 +32,7 @@ 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); + let mut client = Client::new(&token, Handler).expect("Err creating client"); 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 713224b..1ecd302 100644 --- a/examples/04_message_builder/src/main.rs +++ b/examples/04_message_builder/src/main.rs @@ -8,7 +8,7 @@ use std::env; struct Handler; impl EventHandler for Handler { - fn on_message(&self, _: Context, msg: Message) { + fn message(&self, _: Context, msg: Message) { if msg.content == "!ping" { let channel = match msg.channel_id.get() { Ok(channel) => channel, @@ -37,7 +37,7 @@ impl EventHandler for Handler { } } - fn on_ready(&self, _: Context, ready: Ready) { + fn ready(&self, _: Context, ready: Ready) { println!("{} is connected!", ready.user.name); } } @@ -45,7 +45,7 @@ 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); + let mut client = Client::new(&token, Handler).expect("Err creating client"); if let Err(why) = client.start() { println!("Client error: {:?}", why); diff --git a/examples/05_command_framework/src/main.rs b/examples/05_command_framework/src/main.rs index 437c44e..02a4f41 100644 --- a/examples/05_command_framework/src/main.rs +++ b/examples/05_command_framework/src/main.rs @@ -31,7 +31,7 @@ impl Key for CommandCounter { struct Handler; impl EventHandler for Handler { - fn on_ready(&self, _: Context, ready: Ready) { + fn ready(&self, _: Context, ready: Ready) { println!("{} is connected!", ready.user.name); } } @@ -41,7 +41,7 @@ fn main() { let token = env::var("DISCORD_TOKEN").expect( "Expected a token in the environment", ); - let mut client = Client::new(&token, Handler); + let mut client = Client::new(&token, Handler).expect("Err creating client"); { let mut data = client.data.lock(); @@ -199,7 +199,7 @@ command!(about_role(_ctx, msg, args) { if let Some(guild) = msg.guild() { // `role_by_name()` allows us to attempt attaining a reference to a role // via its name. - if let Some(role) = guild.read().unwrap().role_by_name(&potential_role_name) { + if let Some(role) = guild.read().role_by_name(&potential_role_name) { if let Err(why) = msg.channel_id.say(&format!("Role-ID: {}", role.id)) { println!("Error sending message: {:?}", why); } diff --git a/examples/06_voice/src/main.rs b/examples/06_voice/src/main.rs index 8de2c3b..64738a3 100644 --- a/examples/06_voice/src/main.rs +++ b/examples/06_voice/src/main.rs @@ -21,7 +21,7 @@ use std::env; struct Handler; impl EventHandler for Handler { - fn on_ready(&self, _: Context, ready: Ready) { + fn ready(&self, _: Context, ready: Ready) { println!("{} is connected!", ready.user.name); } } @@ -30,7 +30,7 @@ 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); + let mut client = Client::new(&token, Handler).expect("Err creating client"); client.with_framework(StandardFramework::new() .configure(|c| c @@ -49,8 +49,8 @@ fn main() { } command!(deafen(ctx, msg) { - let guild_id = match CACHE.read().unwrap().guild_channel(msg.channel_id) { - Some(channel) => channel.read().unwrap().guild_id, + let guild_id = match CACHE.read().guild_channel(msg.channel_id) { + Some(channel) => channel.read().guild_id, None => { check_msg(msg.channel_id.say("Groups and DMs not supported")); @@ -95,8 +95,8 @@ command!(join(ctx, msg, args) { }, }; - let guild_id = match CACHE.read().unwrap().guild_channel(msg.channel_id) { - Some(channel) => channel.read().unwrap().guild_id, + let guild_id = match CACHE.read().guild_channel(msg.channel_id) { + Some(channel) => channel.read().guild_id, None => { check_msg(msg.channel_id.say("Groups and DMs not supported")); @@ -111,8 +111,8 @@ command!(join(ctx, msg, args) { }); command!(leave(ctx, msg) { - let guild_id = match CACHE.read().unwrap().guild_channel(msg.channel_id) { - Some(channel) => channel.read().unwrap().guild_id, + let guild_id = match CACHE.read().guild_channel(msg.channel_id) { + Some(channel) => channel.read().guild_id, None => { check_msg(msg.channel_id.say("Groups and DMs not supported")); @@ -133,8 +133,8 @@ command!(leave(ctx, msg) { }); command!(mute(ctx, msg) { - let guild_id = match CACHE.read().unwrap().guild_channel(msg.channel_id) { - Some(channel) => channel.read().unwrap().guild_id, + let guild_id = match CACHE.read().guild_channel(msg.channel_id) { + Some(channel) => channel.read().guild_id, None => { check_msg(msg.channel_id.say("Groups and DMs not supported")); @@ -182,8 +182,8 @@ command!(play(ctx, msg, args) { return Ok(()); } - let guild_id = match CACHE.read().unwrap().guild_channel(msg.channel_id) { - Some(channel) => channel.read().unwrap().guild_id, + let guild_id = match CACHE.read().guild_channel(msg.channel_id) { + Some(channel) => channel.read().guild_id, None => { check_msg(msg.channel_id.say("Error finding channel info")); @@ -212,8 +212,8 @@ command!(play(ctx, msg, args) { }); command!(undeafen(ctx, msg) { - let guild_id = match CACHE.read().unwrap().guild_channel(msg.channel_id) { - Some(channel) => channel.read().unwrap().guild_id, + let guild_id = match CACHE.read().guild_channel(msg.channel_id) { + Some(channel) => channel.read().guild_id, None => { check_msg(msg.channel_id.say("Error finding channel info")); @@ -231,8 +231,8 @@ command!(undeafen(ctx, msg) { }); command!(unmute(ctx, msg) { - let guild_id = match CACHE.read().unwrap().guild_channel(msg.channel_id) { - Some(channel) => channel.read().unwrap().guild_id, + let guild_id = match CACHE.read().guild_channel(msg.channel_id) { + Some(channel) => channel.read().guild_id, None => { check_msg(msg.channel_id.say("Error finding channel info")); diff --git a/examples/07_sample_bot_structure/src/main.rs b/examples/07_sample_bot_structure/src/main.rs index 37f6c98..4c0ff8b 100644 --- a/examples/07_sample_bot_structure/src/main.rs +++ b/examples/07_sample_bot_structure/src/main.rs @@ -48,7 +48,10 @@ fn main() { // `RUST_LOG` to debug`. env_logger::init().expect("Failed to initialize env_logger"); - let mut client = Client::new(&env::var("DISCORD_TOKEN").unwrap(), Handler); + 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) => { diff --git a/examples/08_env_logging/src/main.rs b/examples/08_env_logging/src/main.rs index db34037..ca6af8f 100644 --- a/examples/08_env_logging/src/main.rs +++ b/examples/08_env_logging/src/main.rs @@ -11,12 +11,12 @@ use std::env; struct Handler; impl EventHandler for Handler { - fn on_ready(&self, _: Context, ready: Ready) { + fn ready(&self, _: Context, ready: Ready) { // Log at the INFO level. This is a macro from the `log` crate. info!("{} is connected!", ready.user.name); } - fn on_resume(&self, _: Context, resume: ResumedEvent) { + fn resume(&self, _: Context, resume: ResumedEvent) { // Log at the DEBUG level. // // In this example, this will not show up in the logs because DEBUG is @@ -37,7 +37,7 @@ fn main() { let token = env::var("DISCORD_TOKEN") .expect("Expected a token in the environment"); - let mut client = Client::new(&token, Handler); + let mut client = Client::new(&token, Handler).expect("Err creating client"); if let Err(why) = client.start() { error!("Client error: {:?}", why); |