diff --git a/README.md b/README.md index a4783ecf19c..05e1b7cea66 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ You may also check your tokens prior to login via the use of [`validate_token`]. Once logged in, you may add handlers to your client to dispatch [`Event`]s, -such as [`Client::on_message`]. This will cause your handler to be called +by implementing the handlers in a trait, such as [`EventHandler::on_message`]. This will cause your handler to be called when a [`Event::MessageCreate`] is received. Each handler is given a [`Context`], giving information about the event. See the [client's module-level documentation]. @@ -138,7 +138,7 @@ Voice+youtube-dl: [`Cache`]: https://docs.rs/serenity/*/serenity/cache/struct.Cache.html [`Client::new`]: https://docs.rs/serenity/*/serenity/client/struct.Client.html#method.new -[`Client::on_message`]: https://docs.rs/serenity/*/serenity/client/struct.Client.html#method.on_message +[`EventHandler::on_message`]: https://docs.rs/serenity/*/serenity/client/struct.EventHandler.html#method.on_message [`Context`]: https://docs.rs/serenity/*/serenity/client/struct.Context.html [`Event`]: https://docs.rs/serenity/*/serenity/model/event/enum.Event.html [`Event::MessageCreate`]: https://docs.rs/serenity/*/serenity/model/event/enum.Event.html#variant.MessageCreatef diff --git a/src/client/mod.rs b/src/client/mod.rs index 9895fe9ac5c..66723416b0f 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -58,7 +58,7 @@ use ::framework::Framework; /// # Event Handlers /// /// Event handlers can be configured. For example, the event handler -/// [`on_message`] will be dispatched to whenever a [`Event::MessageCreate`] is +/// [`EventHandler::on_message`] will be dispatched to whenever a [`Event::MessageCreate`] is /// received over the connection. /// /// Note that you do not need to manually handle events, as they are handled @@ -70,15 +70,20 @@ use ::framework::Framework; /// receive, acting as a "ping-pong" bot is simple: /// /// ```rust,ignore -/// use serenity::Client; +/// use serenity::prelude::*; +/// use serenity::model::*; /// -/// let mut client = Client::new("my token here"); +/// struct Handler; /// -/// client.on_message(|context, message| { -/// if message.content == "!ping" { -/// message.channel_id.say("Pong!"); +/// impl EventHandler for Handler { +/// fn on_message(&self, _: Context, msg: Message) { +/// if msg.content == "!ping" { +/// let _ = msg.channel_id.say("Pong!"); +/// } /// } -/// }); +/// } +/// +/// let mut client = Client::new("my token here", Handler); /// /// client.start(); /// ``` @@ -113,7 +118,8 @@ pub struct Client { /// extern crate serenity; /// extern crate typemap; /// - /// use serenity::Client; + /// use serenity::prelude::*; + /// use serenity::model::*; /// use std::collections::HashMap; /// use std::env; /// use typemap::Key; @@ -124,13 +130,6 @@ pub struct Client { /// type Value = HashMap; /// } /// - /// let mut client = Client::new(&env::var("DISCORD_TOKEN").unwrap()); - /// - /// { - /// let mut data = client.data.lock().unwrap(); - /// data.insert::(HashMap::default()); - /// } - /// /// macro_rules! reg { /// ($ctx:ident $name:expr) => { /// { @@ -142,10 +141,23 @@ pub struct Client { /// }; /// } /// - /// client.on_message(|ctx, _| reg!(ctx "MessageCreate")); - /// client.on_message_delete(|ctx, _| reg!(ctx "MessageDelete")); - /// client.on_message_delete_bulk(|ctx, _| reg!(ctx "MessageDeleteBulk")); - /// client.on_message_update(|ctx, _| reg!(ctx "MessageUpdate")); + /// struct Handler; + /// + /// impl EventHandler for Handler { + /// fn on_message(&self, ctx: Context, _: Message) { reg!(ctx "MessageCreate") } + /// fn on_message_delete(&self, ctx: Context, _: Message) { reg!(ctx "MessageDelete") } + /// fn on_message_delete_bulk(&self, ctx: Context, _: Vec) { reg!(ctx "MessageDeleteBulk") } + /// fn on_message_update(&self, ctx: Context, _: Message) { reg!(ctx "MessageUpdate") } + /// } + /// + /// let mut client = Client::new(&env::var("DISCORD_TOKEN").unwrap(), Handler); + /// + /// { + /// let mut data = client.data.lock().unwrap(); + /// data.insert::(HashMap::default()); + /// } + /// + /// client.start().unwrap(); /// ``` /// /// Refer to [example 05] for an example on using the `data` field. diff --git a/src/prelude.rs b/src/prelude.rs index 3c9ef85b2b2..3c0d0a813d5 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -18,7 +18,7 @@ pub use ::error::{Error as SerenityError}; pub use ::model::Mentionable; #[cfg(feature="client")] -pub use ::client::{Client, ClientError as ClientError}; +pub use ::client::{Client, ClientError as ClientError, EventHandler}; #[cfg(feature="gateway")] pub use ::gateway::GatewayError; #[cfg(feature="http")]