diff --git a/src/builder/create_embed.rs b/src/builder/create_embed.rs index 17f164bd82d..6d1d3840618 100644 --- a/src/builder/create_embed.rs +++ b/src/builder/create_embed.rs @@ -193,18 +193,22 @@ 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: @@ -212,41 +216,44 @@ impl CreateEmbed { /// 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>(mut self, timestamp: T) -> Self { self.0.insert("timestamp".to_owned(), Value::String(timestamp.into().ts)); diff --git a/src/builder/create_invite.rs b/src/builder/create_invite.rs index 62f86574328..8253205de6f 100644 --- a/src/builder/create_invite.rs +++ b/src/builder/create_invite.rs @@ -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 diff --git a/src/builder/edit_profile.rs b/src/builder/edit_profile.rs index 7a3a5224e95..2b54f68df45 100644 --- a/src/builder/edit_profile.rs +++ b/src/builder/edit_profile.rs @@ -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 diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 203d48bbabe..797fc3cc637 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -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 @@ -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 @@ -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 diff --git a/src/client/context.rs b/src/client/context.rs index eaae7b3d26e..97cde50f525 100644 --- a/src/client/context.rs +++ b/src/client/context.rs @@ -45,8 +45,8 @@ impl Context { /// Create a new Context to be passed to an event handler. pub(crate) fn new(shard: Arc>, data: Arc>) -> Context { Context { - data: data, - shard: shard, + data, + shard, } } @@ -59,15 +59,19 @@ impl Context { /// Change the current user's username: /// /// ```rust,no_run - /// # use serenity::Client; + /// # use serenity::prelude::*; + /// # use serenity::model::*; /// # - /// # let mut client = Client::new(""); - /// # - /// # client.on_message(|ctx, msg| { - /// # if msg.content == "!changename" { - /// ctx.edit_profile(|p| p.username("Hakase")); - /// # } - /// # }); + /// struct Handler; + /// + /// impl EventHandler for Handler { + /// fn on_message(&self, ctx: Context, msg: Message) { + /// if msg.content == "!changename" { + /// ctx.edit_profile(|e| e.username("Edward Elric")); + /// } + /// } + /// } + /// let mut client = Client::new("token", Handler); client.start().unwrap(); /// ``` #[cfg(feature="builder")] pub fn edit_profile EditProfile>(&self, f: F) -> Result { @@ -104,14 +108,19 @@ impl Context { /// Set the current user to being online on the shard: /// /// ```rust,no_run - /// # use serenity::Client; + /// # use serenity::prelude::*; + /// # use serenity::model::*; /// # - /// # let mut client = Client::new(""); - /// client.on_message(|ctx, msg| { - /// if msg.content == "!online" { - /// ctx.online(); - /// } - /// }); + /// struct Handler; + /// + /// impl EventHandler for Handler { + /// fn on_message(&self, ctx: Context, msg: Message) { + /// if msg.content == "!online" { + /// ctx.online(); + /// } + /// } + /// } + /// let mut client = Client::new("token", Handler); client.start().unwrap(); /// ``` /// /// [`Online`]: ../model/enum.OnlineStatus.html#variant.Online @@ -128,14 +137,19 @@ impl Context { /// Set the current user to being idle on the shard: /// /// ```rust,no_run - /// # use serenity::Client; + /// # use serenity::prelude::*; + /// # use serenity::model::*; /// # - /// # let mut client = Client::new(""); - /// client.on_message(|ctx, msg| { - /// if msg.content == "!idle" { - /// ctx.idle(); - /// } - /// }); + /// struct Handler; + /// + /// impl EventHandler for Handler { + /// fn on_message(&self, ctx: Context, msg: Message) { + /// if msg.content == "!idle" { + /// ctx.idle(); + /// } + /// } + /// } + /// let mut client = Client::new("token", Handler); client.start().unwrap(); /// ``` /// /// [`Idle`]: ../model/enum.OnlineStatus.html#variant.Idle @@ -152,14 +166,19 @@ impl Context { /// Set the current user to being Do Not Disturb on the shard: /// /// ```rust,no_run - /// # use serenity::Client; + /// # use serenity::prelude::*; + /// # use serenity::model::*; /// # - /// # let mut client = Client::new(""); - /// client.on_message(|ctx, msg| { - /// if msg.content == "!dnd" { - /// ctx.dnd(); - /// } - /// }); + /// struct Handler; + /// + /// impl EventHandler for Handler { + /// fn on_message(&self, ctx: Context, msg: Message) { + /// if msg.content == "!dnd" { + /// ctx.dnd(); + /// } + /// } + /// } + /// let mut client = Client::new("token", Handler); client.start().unwrap(); /// ``` /// /// [`DoNotDisturb`]: ../model/enum.OnlineStatus.html#variant.DoNotDisturb @@ -177,12 +196,18 @@ impl Context { /// [`Event::Ready`] is received: /// /// ```rust,no_run - /// # use serenity::Client; + /// # use serenity::prelude::*; + /// # use serenity::model::*; /// # - /// # let mut client = Client::new(""); - /// client.on_ready(|ctx, _| { - /// ctx.invisible(); - /// }); + /// struct Handler; + /// + /// impl EventHandler for Handler { + /// fn on_ready(&self, ctx: Context, _: Ready) { + /// ctx.invisible(); + /// } + /// } + /// + /// let mut client = Client::new("token", Handler); client.start().unwrap(); /// ``` /// /// [`Event::Ready`]: ../model/event/enum.Event.html#variant.Ready @@ -202,12 +227,18 @@ impl Context { /// Reset the presence when an [`Event::Resumed`] is received: /// /// ```rust,no_run - /// # use serenity::Client; - /// # - /// # let mut client = Client::new(""); - /// client.on_resume(|ctx, _| { - /// ctx.reset_presence(); - /// }); + /// # use serenity::prelude::*; + /// # use serenity::model::event::ResumedEvent; + /// # + /// struct Handler; + /// + /// impl EventHandler for Handler { + /// fn on_resume(&self, ctx: Context, _: ResumedEvent) { + /// ctx.reset_presence(); + /// } + /// } + /// + /// let mut client = Client::new("token", Handler); client.start().unwrap(); /// ``` /// /// [`Event::Resumed`]: ../model/event/enum.Event.html#variant.Resumed @@ -226,21 +257,25 @@ impl Context { /// playing: /// /// ```rust,no_run - /// # use serenity::Client; - /// # - /// # let mut client = Client::new(""); + /// # use serenity::prelude::*; + /// # use serenity::model::*; /// # /// use serenity::model::Game; /// - /// client.on_message(|ctx, msg| { - /// let args = msg.content.splitn(2, ' ').collect::>(); + /// struct Handler; + /// impl EventHandler for Handler { + /// fn on_message(&self, ctx: Context, msg: Message) { + /// let args = msg.content.splitn(2, ' ').collect::>(); + /// + /// if args.len() < 2 || *unsafe { args.get_unchecked(0) } != "~setgame" { + /// return; + /// } /// - /// if args.len() < 2 || *unsafe { args.get_unchecked(0) } != "~setgame" { - /// return; - /// } + /// ctx.set_game(Game::playing(*unsafe { args.get_unchecked(1) })); + /// } + /// } /// - /// ctx.set_game(Game::playing(*unsafe { args.get_unchecked(1) })); - /// }); + /// let mut client = Client::new("token", Handler); client.start().unwrap(); /// ``` /// /// [`Online`]: ../model/enum.OnlineStatus.html#variant.Online @@ -263,13 +298,18 @@ impl Context { /// When an [`Event::Ready`] is received, set the game name to `"test"`: /// /// ```rust,no_run - /// # use serenity::Client; - /// # - /// # let mut client = Client::new(""); + /// # use serenity::prelude::*; + /// # use serenity::model::*; /// # - /// client.on_ready(|ctx, _| { - /// ctx.set_game_name("test"); - /// }); + /// struct Handler; + /// + /// impl EventHandler for Handler { + /// fn on_ready(&self, ctx: Context, _: Ready) { + /// ctx.set_game_name("test"); + /// } + /// } + /// + /// let mut client = Client::new("token", Handler); client.start().unwrap(); /// ``` /// /// [`Event::Ready`]: ../model/event/enum.Event.html#variant.Ready @@ -297,35 +337,41 @@ impl Context { /// Setting the current user as having no game and being [`Idle`]: /// /// ```rust,no_run - /// # use serenity::Client; + /// # use serenity::prelude::*; + /// # use serenity::model::*; /// # - /// # let mut client = Client::new(""); - /// # - /// # client.on_ready(|ctx, _| { - /// # - /// use serenity::model::OnlineStatus; - /// - /// ctx.set_presence(None, OnlineStatus::Idle, false); - /// # }); + /// struct Handler; + /// + /// impl EventHandler for Handler { + /// fn on_ready(&self, ctx: Context, _: Ready) { + /// use serenity::model::OnlineStatus; + /// + /// ctx.set_presence(None, OnlineStatus::Idle, false); + /// } + /// } + /// let mut client = Client::new("token", Handler); client.start().unwrap(); /// ``` /// /// Setting the current user as playing `"Heroes of the Storm"`, while being /// [`DoNotDisturb`]: /// /// ```rust,ignore - /// # use serenity::Client; - /// # - /// # let mut client = Client::new(""); + /// # use serenity::prelude::*; + /// # use serenity::model::*; /// # - /// # client.on_ready(|ctx, _| { - /// # - /// use serenity::model::{Game, OnlineStatus}; - /// - /// let game = Game::playing("Heroes of the Storm"); - /// let status = OnlineStatus::DoNotDisturb; - /// - /// context.set_presence(Some(game), status, false); - /// # }); + /// struct Handler; + /// + /// impl EventHandler for Handler { + /// fn on_ready(&self, context: Context, _: Ready) { + /// use serenity::model::{Game, OnlineStatus}; + /// + /// let game = Game::playing("Heroes of the Storm"); + /// let status = OnlineStatus::DoNotDisturb; + /// + /// context.set_presence(Some(game), status, false); + /// } + /// } + /// let mut client = Client::new("token", Handler); client.start().unwrap(); /// ``` /// /// [`DoNotDisturb`]: ../model/enum.OnlineStatus.html#variant.DoNotDisturb diff --git a/src/client/mod.rs b/src/client/mod.rs index 5f47b1f84a5..5a8bc51f198 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -192,6 +192,10 @@ impl Client { /// Create a Client, using a token from an environment variable: /// /// ```rust,no_run + /// # use serenity::prelude::EventHandler; + /// struct Handler; + /// + /// impl EventHandler for Handler {} /// # use std::error::Error; /// # /// # fn try_main() -> Result<(), Box> { @@ -199,7 +203,7 @@ impl Client { /// use std::env; /// /// let token = env::var("DISCORD_TOKEN")?; - /// let client = Client::new(&token); + /// let client = Client::new(&token, Handler); /// # Ok(()) /// # } /// # @@ -247,13 +251,17 @@ impl Client { /// Create a simple framework that responds to a `~ping` command: /// /// ```rust,no_run + /// # use serenity::prelude::EventHandler; /// # use std::error::Error; /// # + /// struct Handler; + /// + /// impl EventHandler for Handler {} /// # fn try_main() -> Result<(), Box> { /// use serenity::Client; /// use std::env; /// - /// let mut client = Client::new(&env::var("DISCORD_TOKEN")?); + /// let mut client = Client::new(&env::var("DISCORD_TOKEN")?, Handler); /// client.with_framework(|f| f /// .configure(|c| c.prefix("~")) /// .command("ping", |c| c.exec_str("Pong!"))); @@ -293,13 +301,17 @@ impl Client { /// Starting a Client with only 1 shard, out of 1 total: /// /// ```rust,no_run + /// # use serenity::prelude::EventHandler; /// # use std::error::Error; /// # + /// struct Handler; + /// + /// impl EventHandler for Handler {} /// # fn try_main() -> Result<(), Box> { /// use serenity::client::Client; /// use std::env; /// - /// let mut client = Client::new(&env::var("DISCORD_TOKEN")?); + /// let mut client = Client::new(&env::var("DISCORD_TOKEN")?, Handler); /// /// if let Err(why) = client.start() { /// println!("Err with client: {:?}", why); @@ -334,13 +346,17 @@ impl Client { /// Start as many shards as needed using autosharding: /// /// ```rust,no_run + /// # use serenity::prelude::EventHandler; /// # use std::error::Error; /// # + /// struct Handler; + /// + /// impl EventHandler for Handler {} /// # fn try_main() -> Result<(), Box> { - /// use serenity::Client; + /// use serenity::client::Client; /// use std::env; /// - /// let mut client = Client::new(&env::var("DISCORD_TOKEN")?); + /// let mut client = Client::new(&env::var("DISCORD_TOKEN")?, Handler); /// /// if let Err(why) = client.start_autosharded() { /// println!("Err with client: {:?}", why); @@ -389,13 +405,17 @@ impl Client { /// Start shard 3 of 5: /// /// ```rust,no_run + /// # use serenity::prelude::EventHandler; /// # use std::error::Error; /// # + /// struct Handler; + /// + /// impl EventHandler for Handler {} /// # fn try_main() -> Result<(), Box> { - /// use serenity::Client; + /// use serenity::client::Client; /// use std::env; /// - /// let mut client = Client::new(&env::var("DISCORD_TOKEN")?); + /// let mut client = Client::new(&env::var("DISCORD_TOKEN")?, Handler); /// /// if let Err(why) = client.start_shard(3, 5) { /// println!("Err with client: {:?}", why); @@ -412,13 +432,17 @@ impl Client { /// [`start_autosharded`]): /// /// ```rust,no_run + /// # use serenity::prelude::EventHandler; /// # use std::error::Error; /// # + /// struct Handler; + /// + /// impl EventHandler for Handler {} /// # fn try_main() -> Result<(), Box> { - /// use serenity::Client; + /// use serenity::client::Client; /// use std::env; /// - /// let mut client = Client::new(&env::var("DISCORD_TOKEN")?); + /// let mut client = Client::new(&env::var("DISCORD_TOKEN")?, Handler); /// /// if let Err(why) = client.start_shard(0, 1) { /// println!("Err with client: {:?}", why); @@ -461,13 +485,17 @@ impl Client { /// Start all of 8 shards: /// /// ```rust,no_run + /// # use serenity::prelude::EventHandler; /// # use std::error::Error; /// # + /// struct Handler; + /// + /// impl EventHandler for Handler {} /// # fn try_main() -> Result<(), Box> { - /// use serenity::Client; + /// use serenity::client::Client; /// use std::env; /// - /// let mut client = Client::new(&env::var("DISCORD_TOKEN")?); + /// let mut client = Client::new(&env::var("DISCORD_TOKEN")?, Handler); /// /// if let Err(why) = client.start_shards(8) { /// println!("Err with client: {:?}", why); @@ -511,23 +539,31 @@ impl Client { /// For a bot using a total of 10 shards, initialize shards 4 through 7: /// /// ```rust,ignore + /// # use serenity::prelude::EventHandler; + /// struct Handler; + /// + /// impl EventHandler for Handler {} /// use serenity::Client; /// use std::env; /// /// let token = env::var("DISCORD_BOT_TOKEN").unwrap(); - /// let mut client = Client::new(&token); + /// let mut client = Client::new(&token, Handler); /// /// let _ = client.start_shard_range([4, 7], 10); /// ``` /// /// ```rust,no_run + /// # use serenity::prelude::EventHandler; /// # use std::error::Error; /// # + /// struct Handler; + /// + /// impl EventHandler for Handler {} /// # fn try_main() -> Result<(), Box> { - /// use serenity::Client; + /// use serenity::client::Client; /// use std::env; /// - /// let mut client = Client::new(&env::var("DISCORD_TOKEN")?); + /// let mut client = Client::new(&env::var("DISCORD_TOKEN")?, Handler); /// /// if let Err(why) = client.start_shard_range([4, 7], 10) { /// println!("Err with client: {:?}", why); diff --git a/src/framework/configuration.rs b/src/framework/configuration.rs index bb5a20f3e8f..16c595f42d5 100644 --- a/src/framework/configuration.rs +++ b/src/framework/configuration.rs @@ -16,10 +16,14 @@ use ::model::{GuildId, Message, UserId}; /// Responding to mentions and setting a command prefix of `"~"`: /// /// ```rust,no_run +/// # use serenity::prelude::EventHandler; +/// struct Handler; +/// +/// impl EventHandler for Handler {} /// use serenity::Client; /// use std::env; /// -/// let mut client = Client::new(&env::var("DISCORD_BOT_TOKEN").unwrap()); +/// let mut client = Client::new(&env::var("DISCORD_BOT_TOKEN").unwrap(), Handler); /// /// client.with_framework(|f| f /// .configure(|c| c.on_mention(true).prefix("~"))); @@ -99,8 +103,11 @@ impl Configuration { /// Create a HashSet in-place: /// /// ```rust - /// # use serenity::Client; - /// # let mut client = Client::new("token"); + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// use serenity::model::GuildId; /// /// client.with_framework(|f| f.configure(|c| c @@ -120,8 +127,11 @@ impl Configuration { /// Create a HashSet in-place: /// /// ```rust - /// # use serenity::Client; - /// # let mut client = Client::new("token"); + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// use serenity::model::UserId; /// /// client.with_framework(|f| f.configure(|c| c @@ -154,8 +164,11 @@ impl Configuration { /// Ignore a set of commands, assuming they exist: /// /// ```rust - /// # use serenity::Client; - /// # let mut client = Client::new("token"); + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// /// let disabled = vec!["ping"].into_iter().map(|x| x.to_owned()).collect(); /// @@ -180,9 +193,11 @@ impl Configuration { /// otherwise return a prefix of `"~"`. /// /// ```rust,no_run - /// # use serenity::Client; + /// # use serenity::prelude::*; + /// # struct Handler; /// # - /// # let mut client = Client::new("token"); + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// client.with_framework(|f| f /// .command("ping", |c| c.exec_str("Pong!")) /// .configure(|c| c.dynamic_prefix(|_, msg| { @@ -261,8 +276,11 @@ impl Configuration { /// Create a HashSet in-place: /// /// ```rust - /// # use serenity::Client; - /// # let mut client = Client::new("token"); + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// use serenity::model::UserId; /// /// client.with_framework(|f| f.configure(|c| c @@ -272,8 +290,11 @@ impl Configuration { /// Create a HashSet beforehand: /// /// ```rust - /// # use serenity::Client; - /// # let mut client = Client::new("token"); + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// use serenity::model::UserId; /// use std::collections::HashSet; /// @@ -297,8 +318,11 @@ impl Configuration { /// Assign a basic prefix: /// /// ```rust - /// # use serenity::Client; - /// # let mut client = Client::new("token"); + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// # /// client.with_framework(|f| f.configure(|c| c /// .prefix("!"))); @@ -317,8 +341,11 @@ impl Configuration { /// Assign a set of prefixes the bot can respond to: /// /// ```rust - /// # use serenity::Client; - /// # let mut client = Client::new("token"); + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// # /// client.with_framework(|f| f.configure(|c| c /// .prefixes(vec!["!", ">", "+"]))); diff --git a/src/framework/create_command.rs b/src/framework/create_command.rs index 9aed52f1641..abed3eaac4f 100644 --- a/src/framework/create_command.rs +++ b/src/framework/create_command.rs @@ -34,11 +34,14 @@ impl CreateCommand { /// is the owner. /// /// ```rust,no_run + /// # use serenity::prelude::EventHandler; + /// # struct Handler; + /// # impl EventHandler for Handler {} /// use serenity::client::{Client, Context}; /// use serenity::model::Message; /// use std::env; /// - /// let mut client = Client::new(&env::var("DISCORD_TOKEN").unwrap()); + /// let mut client = Client::new(&env::var("DISCORD_TOKEN").unwrap(), Handler); /// /// client.with_framework(|f| f /// .configure(|c| c.prefix("~")) diff --git a/src/framework/help_commands.rs b/src/framework/help_commands.rs index 82fd7bff9e6..a35ce7d0345 100644 --- a/src/framework/help_commands.rs +++ b/src/framework/help_commands.rs @@ -58,9 +58,11 @@ fn remove_aliases(cmds: &HashMap) -> HashMap<&String, &I /// Use the command with `exec_help`: /// /// ```rust -/// # use serenity::Client; +/// # use serenity::prelude::*; +/// # struct Handler; /// # -/// # let mut client = Client::new("token"); +/// # impl EventHandler for Handler {} +/// # let mut client = Client::new("token", Handler); /// # /// use serenity::ext::framework::help_commands; /// @@ -208,9 +210,11 @@ pub fn with_embeds(_: &mut Context, /// Use the command with `exec_help`: /// /// ```rust -/// # use serenity::Client; +/// # use serenity::prelude::*; +/// # struct Handler; /// # -/// # let mut client = Client::new("token"); +/// # impl EventHandler for Handler {} +/// # let mut client = Client::new("token", Handler); /// # /// use serenity::ext::framework::help_commands; /// diff --git a/src/framework/mod.rs b/src/framework/mod.rs index aae518babd7..a521b1314c6 100644 --- a/src/framework/mod.rs +++ b/src/framework/mod.rs @@ -243,11 +243,11 @@ pub struct Framework { /// - a command check has been set. /// /// This is used internally to determine whether or not - in addition to - /// dispatching to the [`Client::on_message`] handler - to have the + /// dispatching to the [`EventHandler::on_message`] handler - to have the /// framework check if a [`Event::MessageCreate`] should be processed by /// itself. /// - /// [`Client::on_message`]: ../client/struct.Client.html#method.on_message + /// [`EventHandler::on_message`]: ../client/event_handler/trait.EventHandler.html#method.on_message /// [`Event::MessageCreate`]: ../model/event/enum.Event.html#variant.MessageCreate pub initialized: bool, user_info: (u64, bool), @@ -264,10 +264,13 @@ impl Framework { /// [allowing whitespace], and setting the [`prefix`] to `"~"`: /// /// ```rust,no_run + /// # use serenity::prelude::EventHandler; + /// # struct Handler; + /// # impl EventHandler for Handler {} /// use serenity::Client; /// use std::env; /// - /// let mut client = Client::new(&env::var("DISCORD_TOKEN").unwrap()); + /// let mut client = Client::new(&env::var("DISCORD_TOKEN").unwrap(), Handler); /// client.with_framework(|f| f /// .configure(|c| c /// .depth(3) @@ -296,8 +299,11 @@ impl Framework { /// a 2 second delay inbetween invocations: /// /// ```rust - /// # use serenity::Client; - /// # let mut client = Client::new("token"); + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// # /// client.with_framework(|f| f /// .bucket("basic", 2, 10, 3) @@ -325,8 +331,11 @@ impl Framework { /// Create and use a simple bucket that has a 2 second delay between invocations: /// /// ```rust - /// # use serenity::Client; - /// # let mut client = Client::new("token"); + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// # /// client.with_framework(|f| f /// .simple_bucket("simple", 2) @@ -352,10 +361,13 @@ impl Framework { /// /// # Examples /// ```rust,no_run - /// # use serenity::Client; /// use serenity::model::ReactionType; /// use serenity::framework::ReactionAction; - /// # let mut client = Client::new("token"); + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// # /// client.with_framework(|f| f /// .action(ReactionAction::Add(ReactionType::Unicode("❤".to_string())), |_, _, channel_id| { @@ -374,10 +386,13 @@ impl Framework { /// /// # Examples /// ```rust,no_run - /// # use serenity::Client; /// use serenity::model::ReactionType; /// use serenity::framework::ReactionAction; - /// # let mut client = Client::new("token"); + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// # /// let action = ReactionAction::Add(ReactionType::Unicode("❤".to_string())); /// client.with_framework(|f| f @@ -649,8 +664,11 @@ impl Framework { /// # #[macro_use] extern crate serenity; /// # /// # fn main() { - /// # use serenity::Client; - /// # let mut client = Client::new("token"); + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// # /// client.with_framework(|f| f.on("ping", ping)); /// @@ -727,8 +745,11 @@ impl Framework { /// Creating a simple group: /// /// ```rust - /// # use serenity::Client; - /// # let mut client = Client::new("token"); + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// # /// client.with_framework(|f| f /// .group("ping-pong", |g| g @@ -755,8 +776,11 @@ impl Framework { /// Making a simple argument error responder: /// /// ```rust - /// # use serenity::Client; - /// # let mut client = Client::new("token"); + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// use serenity::framework::DispatchError::{NotEnoughArguments, TooManyArguments}; /// /// client.with_framework(|f| f @@ -791,8 +815,11 @@ impl Framework { /// Using `before` to log command usage: /// /// ```rust - /// # use serenity::Client; - /// # let mut client = Client::new("token"); + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// # /// client.with_framework(|f| f /// .before(|ctx, msg, cmd_name| { @@ -804,8 +831,11 @@ impl Framework { /// Using before to prevent command usage: /// /// ```rust - /// # use serenity::Client; - /// # let mut client = Client::new("token"); + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// # /// client.with_framework(|f| f /// .before(|_, msg, cmd_name| { @@ -837,8 +867,11 @@ impl Framework { /// Using `after` to log command usage: /// /// ```rust - /// # use serenity::Client; - /// # let mut client = Client::new("token"); + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// # /// client.with_framework(|f| f /// .after(|ctx, msg, cmd_name, error| { diff --git a/src/gateway/shard.rs b/src/gateway/shard.rs index cb2462f2e08..4c06944bd15 100644 --- a/src/gateway/shard.rs +++ b/src/gateway/shard.rs @@ -519,25 +519,28 @@ impl Shard { /// message handled through [`Client::on_message`]. /// /// ```rust,no_run - /// # use serenity::client::Client; - /// # - /// # let mut client = Client::new("hello source code viewer <3"); - /// # - /// client.on_message(|ctx, msg| { - /// if msg.content == "~ping" { - /// if let Some(latency) = ctx.shard.lock().unwrap().latency() { - /// let s = format!("{}.{}s", latency.as_secs(), latency.subsec_nanos()); - /// - /// let _ = msg.channel_id.say(&s); - /// } else { - /// let _ = msg.channel_id.say("N/A"); + /// # use serenity::prelude::*; + /// # use serenity::model::*; + /// struct Handler; + /// + /// impl EventHandler for Handler { + /// fn on_message(&self, ctx: Context, msg: Message) { + /// if msg.content == "~ping" { + /// if let Some(latency) = ctx.shard.lock().unwrap().latency() { + /// let s = format!("{}.{}s", latency.as_secs(), latency.subsec_nanos()); + /// + /// let _ = msg.channel_id.say(&s); + /// } else { + /// let _ = msg.channel_id.say("N/A"); + /// } /// } /// } - /// }); + /// } + /// let mut client = Client::new("token", Handler); client.start().unwrap(); /// ``` /// /// [`Client`]: ../struct.Client.html - /// [`Client::on_message`]: ../struct.Client.html#method.on_message + /// [`EventHandler::on_message`]: ../event_handler/trait.EventHandler.html#method.on_message // Shamelessly stolen from brayzure's commit in eris: // pub fn latency(&self) -> Option { diff --git a/src/model/channel/attachment.rs b/src/model/channel/attachment.rs index 015c18e46c3..223817423b6 100644 --- a/src/model/channel/attachment.rs +++ b/src/model/channel/attachment.rs @@ -46,52 +46,55 @@ impl Attachment { /// Download all of the attachments associated with a [`Message`]: /// /// ```rust,no_run - /// use serenity::Client; + /// use serenity::prelude::*; + /// use serenity::model::*; /// use std::env; /// use std::fs::File; /// use std::io::Write; /// use std::path::Path; /// - /// let token = env::var("DISCORD_TOKEN").expect("token in environment"); - /// let mut client = Client::new(&token); + /// struct Handler; /// - /// client.on_message(|_, message| { - /// for attachment in message.attachments { - /// let content = match attachment.download() { - /// Ok(content) => content, - /// Err(why) => { - /// println!("Error downloading attachment: {:?}", why); - /// let _ = message.channel_id.say("Error downloading attachment"); /// - /// return; - /// }, - /// }; + /// impl EventHandler for Handler { + /// fn on_message(&self, _: Context, message: Message) { + /// for attachment in message.attachments { + /// let content = match attachment.download() { + /// Ok(content) => content, + /// Err(why) => { + /// println!("Error downloading attachment: {:?}", why); + /// let _ = message.channel_id.say("Error downloading attachment"); /// - /// let mut file = match File::create(&attachment.filename) { - /// Ok(file) => file, - /// Err(why) => { - /// println!("Error creating file: {:?}", why); - /// let _ = message.channel_id.say("Error creating file"); + /// return; + /// }, + /// }; /// - /// return; - /// }, - /// }; + /// let mut file = match File::create(&attachment.filename) { + /// Ok(file) => file, + /// Err(why) => { + /// println!("Error creating file: {:?}", why); + /// let _ = message.channel_id.say("Error creating file"); /// - /// if let Err(why) = file.write(&content) { - /// println!("Error writing to file: {:?}", why); + /// return; + /// }, + /// }; /// - /// return; - /// } + /// if let Err(why) = file.write(&content) { + /// println!("Error writing to file: {:?}", why); /// - /// let _ = message.channel_id.say(&format!("Saved {:?}", attachment.filename)); - /// } - /// }); + /// return; + /// } /// - /// client.on_ready(|_context, ready| { - /// println!("{} is connected!", ready.user.name); - /// }); + /// let _ = message.channel_id.say(&format!("Saved {:?}", attachment.filename)); + /// } + /// } /// - /// let _ = client.start(); + /// fn on_ready(&self, _: Context, ready: Ready) { + /// println!("{} is connected!", ready.user.name); + /// } + /// } + /// let token = env::var("DISCORD_TOKEN").expect("token in environment"); + /// let mut client = Client::new(&token, Handler); client.start().unwrap(); /// ``` /// /// # Errors diff --git a/src/model/channel/guild_channel.rs b/src/model/channel/guild_channel.rs index 09e76a4e22f..15a068eb17c 100644 --- a/src/model/channel/guild_channel.rs +++ b/src/model/channel/guild_channel.rs @@ -413,22 +413,25 @@ impl GuildChannel { /// channel: /// /// ```rust,no_run - /// # use serenity::Client; - /// # - /// # let mut client = Client::new(""); - /// # + /// use serenity::prelude::*; + /// use serenity::model::*; + /// struct Handler; + /// /// use serenity::CACHE; + /// + /// impl EventHandler for Handler { + /// fn on_message(&self, _: Context, msg: Message) { + /// let channel = match CACHE.read().unwrap().guild_channel(msg.channel_id) { + /// Some(channel) => channel, + /// None => return, + /// }; /// - /// client.on_message(|_, msg| { - /// let channel = match CACHE.read().unwrap().guild_channel(msg.channel_id) { - /// Some(channel) => channel, - /// None => return, - /// }; - /// - /// let permissions = channel.read().unwrap().permissions_for(&msg.author).unwrap(); + /// let permissions = channel.read().unwrap().permissions_for(&msg.author).unwrap(); /// - /// println!("The user's permissions: {:?}", permissions); - /// }); + /// println!("The user's permissions: {:?}", permissions); + /// } + /// } + /// let mut client = Client::new("token", Handler); client.start().unwrap(); /// ``` /// /// Check if the current user has the [Attach Files] and [Send Messages] @@ -436,38 +439,42 @@ impl GuildChannel { /// for demonstrative purposes): /// /// ```rust,no_run - /// # use serenity::Client; - /// # - /// # let mut client = Client::new(""); - /// # + /// use serenity::prelude::*; + /// use serenity::model::*; /// use serenity::CACHE; /// use serenity::model::permissions; /// use std::fs::File; /// - /// client.on_message(|_, msg| { - /// let channel = match CACHE.read().unwrap().guild_channel(msg.channel_id) { - /// Some(channel) => channel, - /// None => return, - /// }; + /// struct Handler; /// - /// let current_user_id = CACHE.read().unwrap().user.id; - /// let permissions = channel.read().unwrap().permissions_for(current_user_id).unwrap(); + /// impl EventHandler for Handler { + /// fn on_message(&self, _: Context, msg: Message) { + /// let channel = match CACHE.read().unwrap().guild_channel(msg.channel_id) { + /// Some(channel) => channel, + /// None => return, + /// }; /// - /// if !permissions.contains(permissions::ATTACH_FILES | permissions::SEND_MESSAGES) { - /// return; - /// } - /// - /// let file = match File::open("./cat.png") { - /// Ok(file) => file, - /// Err(why) => { - /// println!("Err opening file: {:?}", why); + /// let current_user_id = CACHE.read().unwrap().user.id; + /// let permissions = channel.read().unwrap().permissions_for(current_user_id).unwrap(); /// + /// if !permissions.contains(permissions::ATTACH_FILES | permissions::SEND_MESSAGES) { /// return; - /// }, - /// }; + /// } + /// + /// let file = match File::open("./cat.png") { + /// Ok(file) => file, + /// Err(why) => { + /// println!("Err opening file: {:?}", why); + /// + /// return; + /// }, + /// }; + /// + /// let _ = msg.channel_id.send_file(file, "cat.png", |m| m.content("here's a cat")); + /// } + /// } /// - /// let _ = msg.channel_id.send_file(file, "cat.png", |m| m.content("here's a cat")); - /// }); + /// let mut client = Client::new("token", Handler); client.start().unwrap(); /// ``` /// /// # Errors diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs index 3fa9de46d5b..6314caee5a8 100644 --- a/src/model/channel/message.rs +++ b/src/model/channel/message.rs @@ -80,10 +80,12 @@ impl Message { /// ```rust,no_run /// # #[macro_use] extern crate serenity; /// # - /// # use serenity::Client; - /// # /// # fn main() { - /// # let mut client = Client::new(""); + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); /// # /// use serenity::model::Channel; /// diff --git a/src/model/error.rs b/src/model/error.rs index 3fffca6cafd..eb52bd8b0f3 100644 --- a/src/model/error.rs +++ b/src/model/error.rs @@ -18,31 +18,36 @@ use super::Permissions; /// # /// # #[cfg(feature="client")] /// # fn try_main() -> Result<(), Box> { -/// use serenity::{Client, Error}; +/// use serenity::prelude::*; +/// use serenity::model::*; +/// use serenity::Error; /// use serenity::model::ModelError; /// use std::env; /// -/// let token = env::var("DISCORD_BOT_TOKEN")?; -/// let mut client = Client::new(&token); +/// struct Handler; /// -/// client.on_member_unban(|context, guild_id, user| { -/// // If the user has an even discriminator, don't re-ban them. -/// if user.discriminator % 2 == 0 { -/// return; -/// } +/// impl EventHandler for Handler { +/// fn on_guild_ban_removal(&self, context: Context, guild_id: GuildId, user: User) { +/// // If the user has an even discriminator, don't re-ban them. +/// if user.discriminator % 2 == 0 { +/// return; +/// } /// -/// match guild_id.ban(user, 8) { -/// Ok(()) => { -/// // Ban successful. -/// }, -/// Err(Error::Model(ModelError::DeleteMessageDaysAmount(amount))) => { -/// println!("Failed deleting {} days' worth of messages", amount); -/// }, -/// Err(why) => { -/// println!("Unexpected error: {:?}", why); -/// }, -/// } -/// }); +/// match guild_id.ban(user, 8) { +/// Ok(()) => { +/// // Ban successful. +/// }, +/// Err(Error::Model(ModelError::DeleteMessageDaysAmount(amount))) => { +/// println!("Failed deleting {} days' worth of messages", amount); +/// }, +/// Err(why) => { +/// println!("Unexpected error: {:?}", why); +/// }, +/// } +/// } +/// } +/// let token = env::var("DISCORD_BOT_TOKEN")?; +/// let mut client = Client::new(&token, Handler); client.start()?; /// # Ok(()) /// # } /// # diff --git a/src/model/user.rs b/src/model/user.rs index 364b978441f..c53b823ffe8 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -67,6 +67,7 @@ impl CurrentUser { /// Returns the formatted URL to the user's default avatar URL. /// /// This will produce a PNG URL. + #[inline] pub fn default_avatar_url(&self) -> String { default_avatar_url(self.discriminator) } @@ -429,46 +430,51 @@ impl User { /// help message, and then react with `'👌'` to verify message sending: /// /// ```rust,no_run - /// # use serenity::Client; - /// # - /// # let mut client = Client::new(""); + /// # use serenity::prelude::*; + /// # use serenity::model::*; /// # /// use serenity::model::Permissions; /// use serenity::CACHE; /// - /// client.on_message(|_, msg| { - /// if msg.content == "~help" { - /// let url = match CACHE.read() { - /// Ok(v) => { - /// match v.user.invite_url(Permissions::empty()) { - /// Ok(v) => v, - /// Err(why) => { - /// println!("Error creating invite url: {:?}", why); - /// - /// return; - /// }, + /// struct Handler; + /// + /// impl EventHandler for Handler { + /// fn on_message(&self, _: Context, msg: Message) { + /// if msg.content == "~help" { + /// let url = match CACHE.read() { + /// Ok(v) => { + /// match v.user.invite_url(Permissions::empty()) { + /// Ok(v) => v, + /// Err(why) => { + /// println!("Error creating invite url: {:?}", why); + /// + /// return; + /// }, + /// } + /// }, + /// Err(why) => { + /// println!("Error reading from CACHE: {:?}", why); + /// + /// return; /// } - /// }, - /// Err(why) => { - /// println!("Error reading from CACHE: {:?}", why); + /// }; + /// let help = format!("Helpful info here. Invite me with this link: <{}>", url); + /// + /// match msg.author.direct_message(|m| m.content(&help)) { + /// Ok(_) => { + /// let _ = msg.react('👌'); + /// }, + /// Err(why) => { + /// println!("Err sending help: {:?}", why); + /// + /// let _ = msg.reply("There was an error DMing you help."); + /// }, + /// }; + /// } + /// } + /// } /// - /// return; - /// } - /// }; - /// let help = format!("Helpful info here. Invite me with this link: <{}>", url); - /// - /// match msg.author.direct_message(|m| m.content(&help)) { - /// Ok(_) => { - /// let _ = msg.react('👌'); - /// }, - /// Err(why) => { - /// println!("Err sending help: {:?}", why); - /// - /// let _ = msg.reply("There was an error DMing you help."); - /// }, - /// }; - /// } - /// }); + /// let mut client = Client::new("token", Handler); /// ``` /// /// # Examples @@ -652,9 +658,17 @@ impl User { /// out-of-sync: /// /// ```rust,no_run - /// # use serenity::Client; + /// # use serenity::prelude::*; + /// # use serenity::model::*; /// # - /// # let mut client = Client::new(""); + /// struct Handler; + /// + /// impl EventHandler for Handler { + /// fn on_message(&self, _: Context, _: Message) { + /// // normal message handling here + /// } + /// } + /// let mut client = Client::new("token", Handler); /// # /// use serenity::model::UserId; /// use serenity::CACHE; @@ -663,10 +677,6 @@ impl User { /// /// let special_users = vec![UserId(114941315417899012), UserId(87600987040120832)]; /// - /// client.on_message(|_ctx, _msg| { - /// // normal message handling here - /// }); - /// /// // start a new thread to periodically refresh the special users' data /// // every 12 hours /// let handle = thread::spawn(move || { @@ -716,24 +726,29 @@ impl User { /// Make a command to tell the user what their tag is: /// /// ```rust,no_run - /// # use serenity::Client; - /// # - /// # let mut client = Client::new(""); + /// # use serenity::prelude::*; + /// # use serenity::model::*; /// # /// use serenity::utils::MessageBuilder; /// use serenity::utils::ContentModifier::Bold; /// - /// client.on_message(|_, msg| { - /// if msg.content == "!mytag" { - /// let content = MessageBuilder::new() - /// .push("Your tag is ") - /// .push(Bold + msg.author.tag()) - /// .build(); + /// struct Handler; /// - /// let _ = msg.channel_id.say(&content); - /// } - /// }); + /// impl EventHandler for Handler { + /// fn on_message(&self, _: Context, msg: Message) { + /// if msg.content == "!mytag" { + /// let content = MessageBuilder::new() + /// .push("Your tag is ") + /// .push(Bold + msg.author.tag()) + /// .build(); + /// + /// let _ = msg.channel_id.say(&content); + /// } + /// } + /// } + /// let mut client = Client::new("token", Handler); client.start().unwrap(); /// ``` + #[inline] pub fn tag(&self) -> String { tag(&self.name, self.discriminator) } diff --git a/tests/test_prelude.rs b/tests/test_prelude.rs index 8c20540a75d..c62e5260e5d 100644 --- a/tests/test_prelude.rs +++ b/tests/test_prelude.rs @@ -3,5 +3,6 @@ extern crate serenity; #[allow(unused_imports)] use serenity::prelude::{Mentionable, SerenityError}; +#[allow(unused_imports)] #[cfg(feature="client")] use serenity::prelude::{Client, ClientError};