Skip to content

Commit

Permalink
Update readme, docs and add EventHandler to the prelude
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis committed Jun 28, 2017
1 parent ea432af commit fdfd5bc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down Expand Up @@ -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
Expand Down
50 changes: 31 additions & 19 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
/// ```
Expand Down Expand Up @@ -113,7 +118,8 @@ pub struct Client<H: EventHandler + Send + Sync + 'static> {
/// 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;
Expand All @@ -124,13 +130,6 @@ pub struct Client<H: EventHandler + Send + Sync + 'static> {
/// type Value = HashMap<String, u64>;
/// }
///
/// let mut client = Client::new(&env::var("DISCORD_TOKEN").unwrap());
///
/// {
/// let mut data = client.data.lock().unwrap();
/// data.insert::<MessageEventCounter>(HashMap::default());
/// }
///
/// macro_rules! reg {
/// ($ctx:ident $name:expr) => {
/// {
Expand All @@ -142,10 +141,23 @@ pub struct Client<H: EventHandler + Send + Sync + 'static> {
/// };
/// }
///
/// 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<Message>) { 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::<MessageEventCounter>(HashMap::default());
/// }
///
/// client.start().unwrap();
/// ```
///
/// Refer to [example 05] for an example on using the `data` field.
Expand Down
2 changes: 1 addition & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit fdfd5bc

Please sign in to comment.