Skip to content

Commit

Permalink
Fix doc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis committed Jul 2, 2017
1 parent 561b0e3 commit 511ec87
Show file tree
Hide file tree
Showing 17 changed files with 626 additions and 416 deletions.
81 changes: 44 additions & 37 deletions src/builder/create_embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,60 +193,67 @@ impl CreateEmbed {
/// Passing a string timestamp:
///
/// ```rust,no_run
/// # use serenity::Client;
/// #
/// # let mut client = Client::new("");
/// #
/// client.on_message(|_, msg| {
/// if msg.content == "~embed" {
/// let _ = msg.channel_id.send_message(|m| m
/// .embed(|e| e
/// .title("hello")
/// .timestamp("2004-06-08T16:04:23")));
/// # use serenity::prelude::*;
/// # use serenity::model::*;
/// #
/// struct Handler;
/// impl EventHandler for Handler {
/// fn on_message(&self, _: Context, msg: Message) {
/// if msg.content == "~embed" {
/// let _ = msg.channel_id.send_message(|m| m
/// .embed(|e| e
/// .title("hello")
/// .timestamp("2004-06-08T16:04:23")));
/// }
/// }
/// });
/// }
///
/// let mut client = Client::new("token", Handler); client.start().unwrap();
/// ```
///
/// Creating a join-log:
///
/// Note: this example isn't efficient and is for demonstrative purposes.
///
/// ```rust,no_run
/// # use serenity::Client;
/// # use serenity::prelude::*;
/// # use serenity::model::*;
/// #
/// # let mut client = Client::new("");
/// #
/// use serenity::CACHE;
///
/// client.on_guild_member_add(|_, guild_id, member| {
/// let cache = CACHE.read().unwrap();
/// struct Handler;
/// impl EventHandler for Handler {
/// fn on_guild_member_addition(&self, _: Context, guild_id: GuildId, member: Member) {
/// use serenity::client::CACHE;
/// let cache = CACHE.read().unwrap();
///
/// if let Some(guild) = cache.guild(guild_id) {
/// let guild = guild.read().unwrap();
/// if let Some(guild) = cache.guild(guild_id) {
/// let guild = guild.read().unwrap();
///
/// let channel_search = guild
/// .channels
/// .values()
/// .find(|c| c.read().unwrap().name == "join-log");
/// let channel_search = guild
/// .channels
/// .values()
/// .find(|c| c.read().unwrap().name == "join-log");
///
/// if let Some(channel) = channel_search {
/// let user = member.user.read().unwrap();
/// if let Some(channel) = channel_search {
/// let user = member.user.read().unwrap();
///
/// let _ = channel.read().unwrap().send_message(|m| m
/// .embed(|e| {
/// let mut e = e
/// .author(|a| a.icon_url(&user.face()).name(&user.name))
/// .title("Member Join");
/// let _ = channel.read().unwrap().send_message(|m| m
/// .embed(|e| {
/// let mut e = e
/// .author(|a| a.icon_url(&user.face()).name(&user.name))
/// .title("Member Join");
///
/// if let Some(ref joined_at) = member.joined_at {
/// e = e.timestamp(joined_at);
/// }
/// if let Some(ref joined_at) = member.joined_at {
/// e = e.timestamp(joined_at);
/// }
///
/// e
/// }));
/// e
/// }));
/// }
/// }
/// }
/// });
/// }
///
/// let mut client = Client::new("token", Handler); client.start().unwrap();
/// ```
pub fn timestamp<T: Into<Timestamp>>(mut self, timestamp: T) -> Self {
self.0.insert("timestamp".to_owned(), Value::String(timestamp.into().ts));
Expand Down
65 changes: 34 additions & 31 deletions src/builder/create_invite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,47 @@ use ::internal::prelude::*;
/// Create an invite with a max age of 3600 seconds and 10 max uses:
///
/// ```rust,no_run
/// # use serenity::Client;
/// #
/// # let mut client = Client::new("");
/// #
/// use serenity::client::CACHE;
/// # use serenity::prelude::*;
/// # use serenity::model::*;
///
/// struct Handler;
///
/// impl EventHandler for Handler {
/// fn on_message(&self, _: Context, msg: Message) {
/// use serenity::client::CACHE;
/// if msg.content == "!createinvite" {
/// let channel = match CACHE.read().unwrap().guild_channel(msg.channel_id) {
/// Some(channel) => channel,
/// None => {
/// let _ = msg.channel_id.say("Error creating invite");
///
/// client.on_message(|_, msg| {
/// if msg.content == "!createinvite" {
/// let channel = match CACHE.read().unwrap().guild_channel(msg.channel_id) {
/// Some(channel) => channel,
/// None => {
/// let _ = msg.channel_id.say("Error creating invite");
/// return;
/// },
/// };
///
/// return;
/// },
/// };
/// let reader = channel.read().unwrap();
///
/// let reader = channel.read().unwrap();
/// let invite = match reader.create_invite(|i| i.max_age(3600).max_uses(10)) {
/// Ok(invite) => invite,
/// Err(why) => {
/// println!("Err creating invite: {:?}", why);
///
/// let invite = match reader.create_invite(|i| i.max_age(3600).max_uses(10)) {
/// Ok(invite) => invite,
/// Err(why) => {
/// println!("Err creating invite: {:?}", why);
/// if let Err(why) = msg.channel_id.say("Error creating invite") {
/// println!("Err sending err msg: {:?}", why);
/// }
///
/// if let Err(why) = msg.channel_id.say("Error creating invite") {
/// println!("Err sending err msg: {:?}", why);
/// }
/// return;
/// },
/// };
///
/// return;
/// },
/// };
/// drop(reader);
///
/// drop(reader);
///
/// let content = format!("Here's your invite: {}", invite.url());
/// let _ = msg.channel_id.say(&content);
/// }
/// });
/// let content = format!("Here's your invite: {}", invite.url());
/// let _ = msg.channel_id.say(&content);
/// }
/// }
/// }
/// let mut client = Client::new("token", Handler); client.start().unwrap();
/// ```
///
/// [`GuildChannel::create_invite`]: ../model/struct.GuildChannel.html#method.create_invite
Expand Down
29 changes: 16 additions & 13 deletions src/builder/edit_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,26 @@ impl EditProfile {
/// image from a file and return its contents in base64-encoded form:
///
/// ```rust,no_run
/// # use serenity::Client;
/// # use serenity::prelude::*;
/// # use serenity::model::*;
/// #
/// # let mut client = Client::new("");
/// #
/// # client.on_message(|context, _| {
/// #
/// use serenity::utils;
/// # struct Handler;
/// # impl EventHandler for Handler {
/// # fn on_message(&self, context: Context, _: Message) {
/// use serenity::utils;
///
/// // assuming a `context` has been bound
/// // assuming a `context` has been bound
///
/// let base64 = utils::read_image("./my_image.jpg")
/// .expect("Failed to read image");
/// let base64 = utils::read_image("./my_image.jpg")
/// .expect("Failed to read image");
///
/// let _ = context.edit_profile(|profile| {
/// profile.avatar(Some(&base64))
/// });
/// # });
/// let _ = context.edit_profile(|profile| {
/// profile.avatar(Some(&base64))
/// });
/// # }
/// }

/// # let mut client = Client::new("token", Handler); client.start().unwrap();
/// ```
///
/// [`utils::read_image`]: ../fn.read_image.html
Expand Down
92 changes: 52 additions & 40 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,28 +163,33 @@ impl Cache {
/// used to determine how many members have not yet been received.
///
/// ```rust,no_run
/// # use serenity::Client;
/// #
/// # let mut client = Client::new("");
/// # use serenity::prelude::*;
/// # use serenity::model::*;
/// #
/// use serenity::client::CACHE;
/// use std::thread;
/// use std::time::Duration;
///
/// struct Handler;
///
/// impl EventHandler for Handler {
/// fn on_ready(&self, ctx: Context, _: Ready) {
/// // Wait some time for guilds to be received.
/// //
/// // You should keep track of this in a better fashion by tracking how
/// // many guilds each `ready` has, and incrementing a counter on
/// // GUILD_CREATEs. Once the number is equal, print the number of
/// // unknown members.
/// //
/// // For demonstrative purposes we're just sleeping the thread for 5
/// // seconds.
/// thread::sleep(Duration::from_secs(5));
///
/// println!("{} unknown members", CACHE.read().unwrap().unknown_members());
/// }
/// }
///
/// client.on_ready(|ctx, _| {
/// // Wait some time for guilds to be received.
/// //
/// // You should keep track of this in a better fashion by tracking how
/// // many guilds each `ready` has, and incrementing a counter on
/// // GUILD_CREATEs. Once the number is equal, print the number of
/// // unknown members.
/// //
/// // For demonstrative purposes we're just sleeping the thread for 5
/// // seconds.
/// thread::sleep(Duration::from_secs(5));
///
/// println!("{} unknown members", CACHE.read().unwrap().unknown_members());
/// });
/// let mut client = Client::new("token", Handler); client.start().unwrap();
/// ```
///
/// [`Member`]: ../model/struct.Member.html
Expand Down Expand Up @@ -245,15 +250,18 @@ impl Cache {
/// Print all of the Ids of guilds in the Cache:
///
/// ```rust,no_run
/// # use serenity::Client;
/// #
/// # let mut client = Client::new("");
/// # use serenity::prelude::*;
/// # use serenity::model::*;
/// #
/// use serenity::client::CACHE;
///
/// client.on_ready(|_, _| {
/// println!("Guilds in the Cache: {:?}", CACHE.read().unwrap().all_guilds());
/// });
/// struct Handler;
/// impl EventHandler for Handler {
/// fn on_ready(&self, _: Context, _: Ready) {
/// println!("Guilds in the Cache: {:?}", CACHE.read().unwrap().all_guilds());
/// }
/// }
/// let mut client = Client::new("token", Handler);
/// ```
///
/// [`Context`]: ../client/struct.Context.html
Expand Down Expand Up @@ -352,27 +360,31 @@ impl Cache {
/// [`Client::on_message`] event dispatch:
///
/// ```rust,no_run
/// # use serenity::Client;
/// #
/// # let mut client = Client::new("");
/// #
/// # client.on_message(|ctx, message| {
/// # use serenity::prelude::*;
/// # use serenity::model::*;
/// #
/// use serenity::client::CACHE;
///
/// let cache = CACHE.read().unwrap();
///
/// let channel = match cache.guild_channel(message.channel_id) {
/// Some(channel) => channel,
/// None => {
/// if let Err(why) = message.channel_id.say("Could not find guild's channel data") {
/// println!("Error sending message: {:?}", why);
/// }
/// struct Handler;
///
/// impl EventHandler for Handler {
/// fn on_message(&self, ctx: Context, message: Message) {
/// let cache = CACHE.read().unwrap();
///
/// let channel = match cache.guild_channel(message.channel_id) {
/// Some(channel) => channel,
/// None => {
/// if let Err(why) = message.channel_id.say("Could not find guild's channel data") {
/// println!("Error sending message: {:?}", why);
/// }
///
/// return;
/// },
/// };
/// }
/// }
///
/// return;
/// },
/// };
/// # });
/// let mut client = Client::new("token", Handler); client.start().unwrap();
/// ```
///
/// [`ChannelId`]: ../model/struct.ChannelId.html
Expand Down
Loading

0 comments on commit 511ec87

Please sign in to comment.