diff options
| author | Zeyla Hellyer <[email protected]> | 2017-02-25 11:50:27 -0800 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-02-25 11:50:27 -0800 |
| commit | aaf4f2eb89ef90db195da9fcc0db225f7858d952 (patch) | |
| tree | 5fb6b1c027ae6284cdf5982e3b7b912a14d2e8cb /examples | |
| parent | Make logo more better (diff) | |
| download | serenity-aaf4f2eb89ef90db195da9fcc0db225f7858d952.tar.xz serenity-aaf4f2eb89ef90db195da9fcc0db225f7858d952.zip | |
Update examples for Context changes
Due to many of the channel methods being removed from the Context (due
to basically duplicating methods and code from `ChannelId`), update
all of the examples to use methods on `ChannelId` instead.
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 | 6 | ||||
| -rw-r--r-- | examples/03_struct_utilities/src/main.rs | 6 | ||||
| -rw-r--r-- | examples/04_message_builder/src/main.rs | 17 | ||||
| -rw-r--r-- | examples/06_command_framework/src/main.rs | 22 | ||||
| -rw-r--r-- | examples/07_voice/src/main.rs | 100 | ||||
| -rw-r--r-- | examples/08_search/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/08_search/src/main.rs | 28 |
8 files changed, 94 insertions, 93 deletions
diff --git a/examples/01_basic_ping_bot/src/main.rs b/examples/01_basic_ping_bot/src/main.rs index 991b537..b2c739a 100644 --- a/examples/01_basic_ping_bot/src/main.rs +++ b/examples/01_basic_ping_bot/src/main.rs @@ -18,13 +18,13 @@ fn main() { // // Event handlers are dispatched through multi-threading, and so multiple // of a single event can be dispatched simultaneously. - client.on_message(|ctx, message| { - if message.content == "!ping" { + client.on_message(|_ctx, msg| { + 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 // channel, so log to stdout when some error happens, with a // description of it. - if let Err(why) = ctx.say("Pong!") { + if let Err(why) = msg.channel_id.say("Pong!") { println!("Error sending message: {:?}", why); } } diff --git a/examples/02_transparent_guild_sharding/src/main.rs b/examples/02_transparent_guild_sharding/src/main.rs index b0a639a..513a574 100644 --- a/examples/02_transparent_guild_sharding/src/main.rs +++ b/examples/02_transparent_guild_sharding/src/main.rs @@ -27,8 +27,8 @@ fn main() { .expect("Expected a token in the environment"); let mut client = Client::login_bot(&token); - client.on_message(|ctx, message| { - if message.content == "!ping" { + client.on_message(|ctx, msg| { + 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. @@ -40,7 +40,7 @@ fn main() { } } - if let Err(why) = ctx.say("Pong!") { + if let Err(why) = msg.channel_id.say("Pong!") { println!("Error sending message: {:?}", why); } } diff --git a/examples/03_struct_utilities/src/main.rs b/examples/03_struct_utilities/src/main.rs index d31b840..4c3a0bb 100644 --- a/examples/03_struct_utilities/src/main.rs +++ b/examples/03_struct_utilities/src/main.rs @@ -18,8 +18,8 @@ fn main() { .expect("Expected a token in the environment"); let mut client = Client::login_bot(&token); - client.on_message(|_ctx, message| { - if message.content == "!messageme" { + client.on_message(|_ctx, msg| { + 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 // often otherwise bulky Context, or even much lower-level `rest` @@ -28,7 +28,7 @@ fn main() { // In this case, you can direct message a User directly by simply // calling a method on its instance, with the content of the // message. - if let Err(why) = message.author.dm("Hello!") { + if let Err(why) = msg.author.dm("Hello!") { println!("Error when direct messaging user: {:?}", why); } } diff --git a/examples/04_message_builder/src/main.rs b/examples/04_message_builder/src/main.rs index 8817065..55c8592 100644 --- a/examples/04_message_builder/src/main.rs +++ b/examples/04_message_builder/src/main.rs @@ -10,9 +10,9 @@ fn main() { .expect("Expected a token in the environment"); let mut client = Client::login_bot(&token); - client.on_message(|ctx, message| { - if message.content == "!ping" { - let channel = match ctx.get_channel() { + client.on_message(|_ctx, msg| { + if msg.content == "!ping" { + let channel = match msg.channel_id.get() { Ok(channel) => channel, Err(why) => { println!("Error getting channel: {:?}", why); @@ -21,18 +21,19 @@ fn main() { }, }; - // The message builder allows for creating a message by mentioning - // users dynamically, pushing "safe" versions of content (such as - // bolding normalized content), displaying emojis, and more. + // The message builder allows for creating a message by + // mentioning users dynamically, pushing "safe" versions of + // content (such as bolding normalized content), displaying + // emojis, and more. let response = MessageBuilder::new() .push("User ") - .push_bold_safe(&message.author.name) + .push_bold_safe(&msg.author.name) .push(" used the 'ping' command in the ") .mention(channel) .push(" channel") .build(); - if let Err(why) = ctx.say(&response) { + if let Err(why) = msg.channel_id.say(&response) { println!("Error sending message: {:?}", why); } } diff --git a/examples/06_command_framework/src/main.rs b/examples/06_command_framework/src/main.rs index 0561f56..1eb1b05 100644 --- a/examples/06_command_framework/src/main.rs +++ b/examples/06_command_framework/src/main.rs @@ -68,15 +68,15 @@ fn main() { // You can not use this to determine whether a command should be // executed. Instead, `set_check` is provided to give you this // functionality. - .before(|context, message, command_name| { + .before(|ctx, msg, command_name| { println!("Got command '{}' by user '{}'", command_name, - message.author.name); + msg.author.name); // Increment the number of times this command has been run once. If // the command's name does not exist in the counter, add a default // value of 0. - let mut data = context.data.lock().unwrap(); + let mut data = ctx.data.lock().unwrap(); let counter = data.get_mut::<CommandCounter>().unwrap(); let entry = counter.entry(command_name.clone()).or_insert(0); *entry += 1; @@ -133,7 +133,7 @@ fn main() { // This may bring more features available for commands in the future. See the // "multiply" command below for some of the power that the `command!` macro can // bring. -command!(commands(ctx, _msg, _args) { +command!(commands(ctx, msg, _args) { let mut contents = "Commands used:\n".to_owned(); let data = ctx.data.lock().unwrap(); @@ -143,7 +143,7 @@ command!(commands(ctx, _msg, _args) { let _ = write!(contents, "- {name}: {amount}\n", name=k, amount=v); } - if let Err(why) = ctx.say(&contents) { + if let Err(why) = msg.channel_id.say(&contents) { println!("Error sending message: {:?}", why); } }); @@ -153,13 +153,13 @@ command!(commands(ctx, _msg, _args) { // In this case, this command checks to ensure you are the owner of the message // in order for the command to be executed. If the check fails, the command is // not called. -fn owner_check(_: &mut Context, message: &Message) -> bool { +fn owner_check(_: &mut Context, msg: &Message) -> bool { // Replace 7 with your ID - message.author.id == 7 + msg.author.id == 7 } -command!(some_long_command(ctx, _msg, args) { - if let Err(why) = ctx.say(&format!("Arguments: {:?}", args)) { +command!(some_long_command(_ctx, msg, args) { + if let Err(why) = msg.channel_id.say(&format!("Arguments: {:?}", args)) { println!("Error sending message: {:?}", why); } }); @@ -184,10 +184,10 @@ command!(some_long_command(ctx, _msg, args) { // will be ignored. // // Argument type overloading is currently not supported. -command!(multiply(ctx, _msg, args, first: f64, second: f64) { +command!(multiply(_ctx, msg, args, first: f64, second: f64) { let res = first * second; - if let Err(why) = ctx.say(&res.to_string()) { + if let Err(why) = msg.channel_id.say(&res.to_string()) { println!("Err sending product of {} and {}: {:?}", first, second, why); } }); diff --git a/examples/07_voice/src/main.rs b/examples/07_voice/src/main.rs index c70a7c3..834cb13 100644 --- a/examples/07_voice/src/main.rs +++ b/examples/07_voice/src/main.rs @@ -42,156 +42,156 @@ fn main() { let _ = client.start().map_err(|why| println!("Client ended: {:?}", why)); } -command!(deafen(context, message) { - let guild_id = match CACHE.read().unwrap().get_guild_channel(message.channel_id) { +command!(deafen(ctx, msg) { + let guild_id = match CACHE.read().unwrap().get_guild_channel(msg.channel_id) { Some(channel) => channel.read().unwrap().guild_id, None => { - check_msg(context.say("Groups and DMs not supported")); + check_msg(msg.channel_id.say("Groups and DMs not supported")); return Ok(()); }, }; - let mut shard = context.shard.lock().unwrap(); + let mut shard = ctx.shard.lock().unwrap(); let handler = match shard.manager.get(guild_id) { Some(handler) => handler, None => { - check_msg(message.reply("Not in a voice channel")); + check_msg(msg.reply("Not in a voice channel")); return Ok(()); }, }; if handler.self_deaf { - check_msg(context.say("Already deafened")); + check_msg(msg.channel_id.say("Already deafened")); } else { handler.deafen(true); - check_msg(context.say("Deafened")); + check_msg(msg.channel_id.say("Deafened")); } }); -command!(join(context, message, args) { +command!(join(ctx, msg, args) { let connect_to = match args.get(0) { Some(arg) => match arg.parse::<u64>() { Ok(id) => ChannelId(id), Err(_why) => { - check_msg(message.reply("Invalid voice channel ID given")); + check_msg(msg.reply("Invalid voice channel ID given")); return Ok(()); }, }, None => { - check_msg(message.reply("Requires a voice channel ID be given")); + check_msg(msg.reply("Requires a voice channel ID be given")); return Ok(()); }, }; - let guild_id = match CACHE.read().unwrap().get_guild_channel(message.channel_id) { + let guild_id = match CACHE.read().unwrap().get_guild_channel(msg.channel_id) { Some(channel) => channel.read().unwrap().guild_id, None => { - check_msg(context.say("Groups and DMs not supported")); + check_msg(msg.channel_id.say("Groups and DMs not supported")); return Ok(()); }, }; - let mut shard = context.shard.lock().unwrap(); + let mut shard = ctx.shard.lock().unwrap(); shard.manager.join(Some(guild_id), connect_to); - check_msg(context.say(&format!("Joined {}", connect_to.mention()))); + check_msg(msg.channel_id.say(&format!("Joined {}", connect_to.mention()))); }); -command!(leave(context, message) { - let guild_id = match CACHE.read().unwrap().get_guild_channel(message.channel_id) { +command!(leave(ctx, msg) { + let guild_id = match CACHE.read().unwrap().get_guild_channel(msg.channel_id) { Some(channel) => channel.read().unwrap().guild_id, None => { - check_msg(context.say("Groups and DMs not supported")); + check_msg(msg.channel_id.say("Groups and DMs not supported")); return Ok(()); }, }; - let mut shard = context.shard.lock().unwrap(); + let mut shard = ctx.shard.lock().unwrap(); let has_handler = shard.manager.get(guild_id).is_some(); if has_handler { shard.manager.remove(guild_id); - check_msg(context.say("Left voice channel")); + check_msg(msg.channel_id.say("Left voice channel")); } else { - check_msg(message.reply("Not in a voice channel")); + check_msg(msg.reply("Not in a voice channel")); } }); -command!(mute(context, message) { - let guild_id = match CACHE.read().unwrap().get_guild_channel(message.channel_id) { +command!(mute(ctx, msg) { + let guild_id = match CACHE.read().unwrap().get_guild_channel(msg.channel_id) { Some(channel) => channel.read().unwrap().guild_id, None => { - check_msg(context.say("Groups and DMs not supported")); + check_msg(msg.channel_id.say("Groups and DMs not supported")); return Ok(()); }, }; - let mut shard = context.shard.lock().unwrap(); + let mut shard = ctx.shard.lock().unwrap(); let handler = match shard.manager.get(guild_id) { Some(handler) => handler, None => { - check_msg(message.reply("Not in a voice channel")); + check_msg(msg.reply("Not in a voice channel")); return Ok(()); }, }; if handler.self_mute { - check_msg(context.say("Already muted")); + check_msg(msg.channel_id.say("Already muted")); } else { handler.mute(true); - check_msg(context.say("Now muted")); + check_msg(msg.channel_id.say("Now muted")); } }); -command!(ping(context) { - check_msg(context.say("Pong!")); +command!(ping(_context, msg) { + check_msg(msg.channel_id.say("Pong!")); }); -command!(play(context, message, args) { +command!(play(ctx, msg, args) { let url = match args.get(0) { Some(url) => url, None => { - check_msg(context.say("Must provide a URL to a video or audio")); + check_msg(msg.channel_id.say("Must provide a URL to a video or audio")); return Ok(()); }, }; if !url.starts_with("http") { - check_msg(context.say("Must provide a valid URL")); + check_msg(msg.channel_id.say("Must provide a valid URL")); return Ok(()); } - let guild_id = match CACHE.read().unwrap().get_guild_channel(message.channel_id) { + let guild_id = match CACHE.read().unwrap().get_guild_channel(msg.channel_id) { Some(channel) => channel.read().unwrap().guild_id, None => { - check_msg(context.say("Error finding channel info")); + check_msg(msg.channel_id.say("Error finding channel info")); return Ok(()); }, }; - if let Some(handler) = context.shard.lock().unwrap().manager.get(guild_id) { + if let Some(handler) = ctx.shard.lock().unwrap().manager.get(guild_id) { let source = match voice::ytdl(url) { Ok(source) => source, Err(why) => { println!("Err starting source: {:?}", why); - check_msg(context.say("Error sourcing ffmpeg")); + check_msg(msg.channel_id.say("Error sourcing ffmpeg")); return Ok(()); }, @@ -199,47 +199,47 @@ command!(play(context, message, args) { handler.play(source); - check_msg(context.say("Playing song")); + check_msg(msg.channel_id.say("Playing song")); } else { - check_msg(context.say("Not in a voice channel to play in")); + check_msg(msg.channel_id.say("Not in a voice channel to play in")); } }); -command!(undeafen(context, message) { - let guild_id = match CACHE.read().unwrap().get_guild_channel(message.channel_id) { +command!(undeafen(ctx, msg) { + let guild_id = match CACHE.read().unwrap().get_guild_channel(msg.channel_id) { Some(channel) => channel.read().unwrap().guild_id, None => { - check_msg(context.say("Error finding channel info")); + check_msg(msg.channel_id.say("Error finding channel info")); return Ok(()); }, }; - if let Some(handler) = context.shard.lock().unwrap().manager.get(guild_id) { + if let Some(handler) = ctx.shard.lock().unwrap().manager.get(guild_id) { handler.deafen(false); - check_msg(context.say("Undeafened")); + check_msg(msg.channel_id.say("Undeafened")); } else { - check_msg(context.say("Not in a voice channel to undeafen in")); + check_msg(msg.channel_id.say("Not in a voice channel to undeafen in")); } }); -command!(unmute(context, message) { - let guild_id = match CACHE.read().unwrap().get_guild_channel(message.channel_id) { +command!(unmute(ctx, msg) { + let guild_id = match CACHE.read().unwrap().get_guild_channel(msg.channel_id) { Some(channel) => channel.read().unwrap().guild_id, None => { - check_msg(context.say("Error finding channel info")); + check_msg(msg.channel_id.say("Error finding channel info")); return Ok(()); }, }; - if let Some(handler) = context.shard.lock().unwrap().manager.get(guild_id) { + if let Some(handler) = ctx.shard.lock().unwrap().manager.get(guild_id) { handler.mute(false); - check_msg(context.say("Unmuted")); + check_msg(msg.channel_id.say("Unmuted")); } else { - check_msg(context.say("Not in a voice channel to undeafen in")); + check_msg(msg.channel_id.say("Not in a voice channel to undeafen in")); } }); diff --git a/examples/08_search/Cargo.toml b/examples/08_search/Cargo.toml index 961f9cf..32f9bc3 100644 --- a/examples/08_search/Cargo.toml +++ b/examples/08_search/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "08_search" version = "0.1.0" -authors = ["Austin Hellyer <[email protected]>"] +authors = ["my name <[email protected]>"] [dependencies] serenity = { path = "../../" } diff --git a/examples/08_search/src/main.rs b/examples/08_search/src/main.rs index c4c9230..72059f6 100644 --- a/examples/08_search/src/main.rs +++ b/examples/08_search/src/main.rs @@ -30,7 +30,7 @@ fn main() { .exec(search) .check(self_check))); - client.on_ready(|_context, ready| { + client.on_ready(|_ctx, ready| { println!("{} is connected!", ready.user.name); }); @@ -39,23 +39,23 @@ fn main() { } } -fn self_check(_context: &mut Context, message: &Message) -> bool { - message.author.id == CACHE.read().unwrap().user.id +fn self_check(_ctx: &mut Context, msg: &Message) -> bool { + msg.author.id == CACHE.read().unwrap().user.id } -command!(search(context, message, args) { +command!(search(_ctx, msg, args) { let query = args.join(" "); if query.is_empty() { - let _ = context.say("You must provide a query"); + let _ = msg.channel_id.say("You must provide a query"); return Ok(()); } - let guild_id = match message.guild_id() { + let guild_id = match msg.guild_id() { Some(guild_id) => guild_id, None => { - let _ = context.say("Only supports guilds"); + let _ = msg.channel_id.say("Only supports guilds"); return Ok(()); }, @@ -67,7 +67,7 @@ command!(search(context, message, args) { let guild = match cache.get_guild(guild_id) { Some(guild) => guild, None => { - let _ = context.say("Guild data not found"); + let _ = msg.channel_id.say("Guild data not found"); return Ok(()); }, @@ -88,7 +88,7 @@ command!(search(context, message, args) { .context_size(0) .has_attachment(true) .has_embed(true) - .max_id(message.id.0 - 1) + .max_id(msg.id.0 - 1) .sort_by(SortingMode::Timestamp) .sort_order(SortingOrder::Descending)); @@ -97,22 +97,22 @@ command!(search(context, message, args) { Err(why) => { println!("Error performing search '{}': {:?}", query, why); - let _ = context.say("Error occurred while searching"); + let _ = msg.channel_id.say("Error occurred while searching"); return Ok(()); }, }; - let _ = context.send_message(|m| m + let _ = msg.channel_id.send_message(|m| m .content(&format!("Found {} total results", messages.total)) .embed(move |mut e| { for (i, mut messages) in messages.results.into_iter().enumerate() { - let mut message = &mut messages[0]; - message.content.truncate(1000); + let mut msg = &mut messages[0]; + msg.content.truncate(1000); e = e.field(|f| f .name(&format!("Result {}", i)) - .value(&message.content)); + .value(&msg.content)); } e |