diff options
| author | Austin Hellyer <[email protected]> | 2016-11-25 20:37:44 -0800 |
|---|---|---|
| committer | Austin Hellyer <[email protected]> | 2016-11-25 20:37:44 -0800 |
| commit | 65ccfa5686f63074907bcea934172e19e188f088 (patch) | |
| tree | 4fbd948363986e2c9ac85feab3a6d8fba8a703e5 /src | |
| parent | Rename PublicChannel to GuildChannel (diff) | |
| download | serenity-65ccfa5686f63074907bcea934172e19e188f088.tar.xz serenity-65ccfa5686f63074907bcea934172e19e188f088.zip | |
Move events into their own module
The events were cluttering the `model` module, and so are now moved into
their own `model::event` module.
As users should not usually have to work with events all that much -
only currently in some rarely used event handlers - this change should
not be much more effort to import from.
i.e.:
```rs
use serenity::model::event::ChannelPinsAckEvent;
```
vs. the now-old:
```rs
use serenity::model::ChannelPinsAckEvent;
```
Diffstat (limited to 'src')
| -rw-r--r-- | src/client/dispatch.rs | 3 | ||||
| -rw-r--r-- | src/client/event_store.rs | 10 | ||||
| -rw-r--r-- | src/client/gateway/prep.rs | 2 | ||||
| -rw-r--r-- | src/client/gateway/shard.rs | 11 | ||||
| -rw-r--r-- | src/client/mod.rs | 143 | ||||
| -rw-r--r-- | src/ext/cache/mod.rs | 1 | ||||
| -rw-r--r-- | src/ext/framework/mod.rs | 2 | ||||
| -rw-r--r-- | src/lib.rs | 10 | ||||
| -rw-r--r-- | src/model/event.rs | 761 | ||||
| -rw-r--r-- | src/model/gateway.rs | 755 | ||||
| -rw-r--r-- | src/model/mod.rs | 6 |
11 files changed, 865 insertions, 839 deletions
diff --git a/src/client/dispatch.rs b/src/client/dispatch.rs index afe9a43..1332d8e 100644 --- a/src/client/dispatch.rs +++ b/src/client/dispatch.rs @@ -4,7 +4,8 @@ use super::event_store::EventStore; use super::login_type::LoginType; use super::Context; use super::gateway::Shard; -use ::model::{ChannelId, Event, Message}; +use ::model::event::Event; +use ::model::{ChannelId, Message}; #[cfg(feature="framework")] use ::ext::framework::Framework; diff --git a/src/client/event_store.rs b/src/client/event_store.rs index 42d4305..31a1ca9 100644 --- a/src/client/event_store.rs +++ b/src/client/event_store.rs @@ -2,6 +2,16 @@ use serde_json::Value; use std::collections::{BTreeMap, HashMap}; use std::sync::Arc; use super::context::Context; +use ::model::event::{ + ChannelPinsAckEvent, + ChannelPinsUpdateEvent, + GuildSyncEvent, + MessageUpdateEvent, + PresenceUpdateEvent, + ResumedEvent, + TypingStartEvent, + VoiceServerUpdateEvent, +}; use ::model::*; // This should use type macros when stable receives the type macro diff --git a/src/client/gateway/prep.rs b/src/client/gateway/prep.rs index bf4e9b3..e3b8656 100644 --- a/src/client/gateway/prep.rs +++ b/src/client/gateway/prep.rs @@ -17,7 +17,7 @@ use websocket::stream::WebSocketStream; use ::constants::{self, OpCode}; use ::error::{Error, Result}; use ::internal::ws_impl::{ReceiverExt, SenderExt}; -use ::model::{Event, GatewayEvent, ReadyEvent}; +use ::model::event::{Event, GatewayEvent, ReadyEvent}; #[inline] pub fn parse_ready(event: GatewayEvent, diff --git a/src/client/gateway/shard.rs b/src/client/gateway/shard.rs index 61bcafc..c35fee2 100644 --- a/src/client/gateway/shard.rs +++ b/src/client/gateway/shard.rs @@ -16,15 +16,8 @@ use websocket::ws::sender::Sender as WsSender; use ::constants::OpCode; use ::internal::prelude::*; use ::internal::ws_impl::{ReceiverExt, SenderExt}; -use ::model::{ - ChannelId, - Event, - Game, - GatewayEvent, - GuildId, - OnlineStatus, - ReadyEvent, -}; +use ::model::event::{Event, GatewayEvent, ReadyEvent}; +use ::model::{ChannelId, Game, GuildId, OnlineStatus}; #[cfg(feature="voice")] use ::ext::voice::Manager as VoiceManager; diff --git a/src/client/mod.rs b/src/client/mod.rs index 2cefb19..44469b8 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -46,6 +46,19 @@ use websocket::client::Receiver; use websocket::stream::WebSocketStream; use ::internal::prelude::{Error, Result, Value}; use ::internal::ws_impl::ReceiverExt; +use ::model::event::{ + ChannelPinsAckEvent, + ChannelPinsUpdateEvent, + Event, + GatewayEvent, + GuildSyncEvent, + MessageUpdateEvent, + PresenceUpdateEvent, + ReadyEvent, + ResumedEvent, + TypingStartEvent, + VoiceServerUpdateEvent, +}; use ::model::*; #[cfg(feature = "framework")] @@ -115,14 +128,14 @@ lazy_static! { /// /// [`Shard`]: gateway/struct.Shard.html /// [`on_message`]: #method.on_message -/// [`Event::MessageCreate`]: ../model/enum.Event.html#variant.MessageCreate +/// [`Event::MessageCreate`]: ../model/event/enum.Event.html#variant.MessageCreate /// [sharding docs]: gateway/index.html#sharding pub struct Client { /// A vector of all active shards that have received their [`Event::Ready`] /// payload, and have dispatched to [`on_ready`] if an event handler was /// configured. /// - /// [`Event::Ready`]: ../model/enum.Event.html#variant.Ready + /// [`Event::Ready`]: ../model/event/enum.Event.html#variant.Ready /// [`on_ready`]: #method.on_ready event_store: Arc<Mutex<EventStore>>, #[cfg(feature="framework")] @@ -307,7 +320,7 @@ impl Client { /// Attaches a handler for when a [`CallCreate`] is received. /// - /// [`CallCreate`]: ../model/enum.Event.html#variant.CallCreate + /// [`CallCreate`]: ../model/event/enum.Event.html#variant.CallCreate pub fn on_call_create<F>(&mut self, handler: F) where F: Fn(Context, Call) + Send + Sync + 'static { self.event_store.lock() @@ -317,7 +330,7 @@ impl Client { /// Attaches a handler for when a [`ChannelCreate`] is received. /// - /// [`ChannelCreate`]: ../model/enum.Event.html#variant.ChannelCreate + /// [`ChannelCreate`]: ../model/event/enum.Event.html#variant.ChannelCreate pub fn on_channel_create<F>(&mut self, handler: F) where F: Fn(Context, Channel) + Send + Sync + 'static { self.event_store.lock() @@ -327,7 +340,7 @@ impl Client { /// Attaches a handler for when a [`ChannelDelete`] is received. /// - /// [`ChannelDelete`]: ../model/enum.Event.html#variant.ChannelDelete + /// [`ChannelDelete`]: ../model/event/enum.Event.html#variant.ChannelDelete pub fn on_channel_delete<F>(&mut self, handler: F) where F: Fn(Context, Channel) + Send + Sync + 'static { self.event_store.lock() @@ -337,7 +350,7 @@ impl Client { /// Attaches a handler for when a [`ChannelPinsAck`] is received. /// - /// [`ChannelPinsAck`]: ../model/enum.Event.html#variant.ChannelPinsAck + /// [`ChannelPinsAck`]: ../model/event/enum.Event.html#variant.ChannelPinsAck pub fn on_channel_pins_ack<F>(&mut self, handler: F) where F: Fn(Context, ChannelPinsAckEvent) + Send + Sync + 'static { self.event_store.lock() @@ -347,7 +360,7 @@ impl Client { /// Attaches a handler for when a [`ChannelPinsUpdate`] is received. /// - /// [`ChannelPinsUpdate`]: ../model/enum.Event.html#variant.ChannelPinsUpdate + /// [`ChannelPinsUpdate`]: ../model/event/enum.Event.html#variant.ChannelPinsUpdate pub fn on_channel_pins_update<F>(&mut self, handler: F) where F: Fn(Context, ChannelPinsUpdateEvent) + Send + Sync + 'static { self.event_store.lock() @@ -357,7 +370,7 @@ impl Client { /// Attaches a handler for when a [`FriendSuggestionCreate`] is received. /// - /// [`FriendSuggestionCreate`]: ../model/enum.Event.html#variant.FriendSuggestionCreate + /// [`FriendSuggestionCreate`]: ../model/event/enum.Event.html#variant.FriendSuggestionCreate pub fn on_friend_suggestion_create<F>(&mut self, handler: F) where F: Fn(Context, User, Vec<SuggestionReason>) + Send + Sync + 'static { self.event_store.lock() @@ -367,7 +380,7 @@ impl Client { /// Attaches a handler for when a [`FriendSuggestionDelete`] is received. /// - /// [`FriendSuggestionDelete`]: ../model/enum.Event.html#variant.FriendSuggestionDelete + /// [`FriendSuggestionDelete`]: ../model/event/enum.Event.html#variant.FriendSuggestionDelete pub fn on_friend_suggestion_delete<F>(&mut self, handler: F) where F: Fn(Context, UserId) + Send + Sync + 'static { self.event_store.lock() @@ -377,7 +390,7 @@ impl Client { /// Attaches a handler for when a [`GuildCreate`] is received. /// - /// [`GuildCreate`]: ../model/enum.Event.html#variant.GuildCreate + /// [`GuildCreate`]: ../model/event/enum.Event.html#variant.GuildCreate pub fn on_guild_create<F>(&mut self, handler: F) where F: Fn(Context, Guild) + Send + Sync + 'static { self.event_store.lock() @@ -389,7 +402,7 @@ impl Client { /// /// The `HashMap` of emojis is the new full list of emojis. /// - /// [`GuildEmojisUpdate`]: ../model/enum.Event.html#variant.GuildEmojisUpdate + /// [`GuildEmojisUpdate`]: ../model/event/enum.Event.html#variant.GuildEmojisUpdate pub fn on_guild_emojis_update<F>(&mut self, handler: F) where F: Fn(Context, GuildId, HashMap<EmojiId, Emoji>) + Send + Sync + 'static { self.event_store.lock() @@ -399,7 +412,7 @@ impl Client { /// Attaches a handler for when a [`GuildIntegrationsUpdate`] is received. /// - /// [`GuildIntegrationsUpdate`]: ../model/enum.Event.html#variant.GuildIntegrationsUpdate + /// [`GuildIntegrationsUpdate`]: ../model/event/enum.Event.html#variant.GuildIntegrationsUpdate pub fn on_guild_integrations_update<F>(&mut self, handler: F) where F: Fn(Context, GuildId) + Send + Sync + 'static { self.event_store.lock() @@ -409,7 +422,7 @@ impl Client { /// Attaches a handler for when a [`GuildMemberAdd`] is received. /// - /// [`GuildMemberAdd`]: ../model/enum.Event.html#variant.GuildMemberAdd + /// [`GuildMemberAdd`]: ../model/event/enum.Event.html#variant.GuildMemberAdd pub fn on_guild_member_add<F>(&mut self, handler: F) where F: Fn(Context, GuildId, Member) + Send + Sync + 'static { self.event_store.lock() @@ -419,7 +432,7 @@ impl Client { /// Attaches a handler for when a [`GuildMembersChunk`] is received. /// - /// [`GuildMembersChunk`]: ../model/enum.Event.html#variant.GuildMembersChunk + /// [`GuildMembersChunk`]: ../model/event/enum.Event.html#variant.GuildMembersChunk pub fn on_guild_members_chunk<F>(&mut self, handler: F) where F: Fn(Context, GuildId, HashMap<UserId, Member>) + Send + Sync + 'static { self.event_store.lock() @@ -429,7 +442,7 @@ impl Client { /// Attaches a handler for when a [`GuildRoleCreate`] is received. /// - /// [`GuildRoleCreate`]: ../model/enum.Event.html#variant.GuildRoleCreate + /// [`GuildRoleCreate`]: ../model/event/enum.Event.html#variant.GuildRoleCreate pub fn on_guild_role_create<F>(&mut self, handler: F) where F: Fn(Context, GuildId, Role) + Send + Sync + 'static { self.event_store.lock() @@ -439,7 +452,7 @@ impl Client { /// Attaches a handler for when a [`GuildRoleSync`] is received. /// - /// [`GuildRoleSync`]: ../model/enum.Event.html#variant.GuildRoleSync + /// [`GuildRoleSync`]: ../model/event/enum.Event.html#variant.GuildRoleSync pub fn on_guild_sync<F>(&mut self, handler: F) where F: Fn(Context, GuildSyncEvent) + Send + Sync + 'static { self.event_store.lock() @@ -449,7 +462,7 @@ impl Client { /// Attaches a handler for when a [`GuildUnavailable`] is received. /// - /// [`GuildUnavailable`]: ../model/enum.Event.html#variant.GuildUnavailable + /// [`GuildUnavailable`]: ../model/event/enum.Event.html#variant.GuildUnavailable pub fn on_guild_unavailable<F>(&mut self, handler: F) where F: Fn(Context, GuildId) + Send + Sync + 'static { self.event_store.lock() @@ -459,7 +472,7 @@ impl Client { /// Attaches a handler for when a [`GuildBan`] is received. /// - /// [`GuildBan`]: ../model/enum.Event.html#variant.GuildBan + /// [`GuildBan`]: ../model/event/enum.Event.html#variant.GuildBan pub fn on_member_ban<F>(&mut self, handler: F) where F: Fn(Context, GuildId, User) + Send + Sync + 'static { self.event_store.lock() @@ -469,7 +482,7 @@ impl Client { /// Attaches a handler for when a [`GuildUnban`] is received. /// - /// [`GuildUnban`]: ../model/enum.Event.html#variant.GuildUnban + /// [`GuildUnban`]: ../model/event/enum.Event.html#variant.GuildUnban pub fn on_member_unban<F>(&mut self, handler: F) where F: Fn(Context, GuildId, User) + Send + Sync + 'static { self.event_store.lock() @@ -479,7 +492,7 @@ impl Client { /// Attaches a handler for when a [`MessageCreate`] is received. /// - /// [`MessageCreate`]: ../model/enum.Event.html#variant.MessageCreate + /// [`MessageCreate`]: ../model/event/enum.Event.html#variant.MessageCreate pub fn on_message<F>(&mut self, handler: F) where F: Fn(Context, Message) + Send + Sync + 'static { @@ -490,7 +503,7 @@ impl Client { /// Attaches a handler for when a [`MessageAck`] is received. /// - /// [`MessageAck`]: ../model/enum.Event.html#variant.MessageAck + /// [`MessageAck`]: ../model/event/enum.Event.html#variant.MessageAck pub fn on_message_ack<F>(&mut self, handler: F) where F: Fn(Context, ChannelId, Option<MessageId>) + Send + Sync + 'static { self.event_store.lock() @@ -500,7 +513,7 @@ impl Client { /// Attaches a handler for when a [`MessageDelete`] is received. /// - /// [`MessageDelete`]: ../model/enum.Event.html#variant.MessageDelete + /// [`MessageDelete`]: ../model/event/enum.Event.html#variant.MessageDelete pub fn on_message_delete<F>(&mut self, handler: F) where F: Fn(Context, ChannelId, MessageId) + Send + Sync + 'static { self.event_store.lock() @@ -510,7 +523,7 @@ impl Client { /// Attaches a handler for when a [`MessageDeleteBulk`] is received. /// - /// [`MessageDeleteBulk`]: ../model/enum.Event.html#variant.MessageDeleteBulk + /// [`MessageDeleteBulk`]: ../model/event/enum.Event.html#variant.MessageDeleteBulk pub fn on_message_delete_bulk<F>(&mut self, handler: F) where F: Fn(Context, ChannelId, Vec<MessageId>) + Send + Sync + 'static { self.event_store.lock() @@ -520,7 +533,7 @@ impl Client { /// Attaches a handler for when a [`MessageUpdate`] is received. /// - /// [`MessageUpdate`]: ../model/enum.Event.html#variant.MessageUpdate + /// [`MessageUpdate`]: ../model/event/enum.Event.html#variant.MessageUpdate pub fn on_message_update<F>(&mut self, handler: F) where F: Fn(Context, MessageUpdateEvent) + Send + Sync + 'static { self.event_store.lock() @@ -530,7 +543,7 @@ impl Client { /// Attaches a handler for when a [`PresencesReplace`] is received. /// - /// [`PresencesReplace`]: ../model/enum.Event.html#variant.PresencesReplace + /// [`PresencesReplace`]: ../model/event/enum.Event.html#variant.PresencesReplace pub fn on_presence_replace<F>(&mut self, handler: F) where F: Fn(Context, Vec<Presence>) + Send + Sync + 'static { self.event_store.lock() @@ -540,7 +553,7 @@ impl Client { /// Attaches a handler for when a [`PresenceUpdate`] is received. /// - /// [`PresenceUpdate`]: ../model/enum.Event.html#variant.PresenceUpdate + /// [`PresenceUpdate`]: ../model/event/enum.Event.html#variant.PresenceUpdate pub fn on_presence_update<F>(&mut self, handler: F) where F: Fn(Context, PresenceUpdateEvent) + Send + Sync + 'static { self.event_store.lock() @@ -550,7 +563,7 @@ impl Client { /// Attached a handler for when a [`ReactionAdd`] is received. /// - /// [`ReactionAdd`]: ../model/enum.Event.html#variant.ReactionAdd + /// [`ReactionAdd`]: ../model/event/enum.Event.html#variant.ReactionAdd pub fn on_reaction_add<F>(&mut self, handler: F) where F: Fn(Context, Reaction) + Send + Sync + 'static { self.event_store.lock() @@ -560,7 +573,7 @@ impl Client { /// Attached a handler for when a [`ReactionRemove`] is received. /// - /// [`ReactionRemove`]: ../model/enum.Event.html#variant.ReactionRemove + /// [`ReactionRemove`]: ../model/event/enum.Event.html#variant.ReactionRemove pub fn on_reaction_remove<F>(&mut self, handler: F) where F: Fn(Context, Reaction) + Send + Sync + 'static { self.event_store.lock() @@ -570,7 +583,7 @@ impl Client { /// Attached a handler for when a [`ReactionRemoveAll`] is received. /// - /// [`ReactionRemoveAll`]: ../model/enum.Event.html#variant.ReactionRemoveAll + /// [`ReactionRemoveAll`]: ../model/event/enum.Event.html#variant.ReactionRemoveAll pub fn on_reaction_remove_all<F>(&mut self, handler: F) where F: Fn(Context, ChannelId, MessageId) + Send + Sync + 'static { self.event_store.lock() @@ -613,7 +626,7 @@ impl Client { /// Attaches a handler for when a [`ChannelRecipientAdd`] is received. /// - /// [`ChannelRecipientAdd`]: ../model/enum.Event.html#variant.ChannelRecipientAdd + /// [`ChannelRecipientAdd`]: ../model/event/enum.Event.html#variant.ChannelRecipientAdd pub fn on_recipient_add<F>(&mut self, handler: F) where F: Fn(Context, ChannelId, User) + Send + Sync + 'static { self.event_store.lock() @@ -623,7 +636,7 @@ impl Client { /// Attaches a handler for when a [`ChannelRecipientRemove`] is received. /// - /// [`ChannelRecipientRemove`]: ../model/enum.Event.html#variant.ChannelRecipientRemove + /// [`ChannelRecipientRemove`]: ../model/event/enum.Event.html#variant.ChannelRecipientRemove pub fn on_recipient_remove<F>(&mut self, handler: F) where F: Fn(Context, ChannelId, User) + Send + Sync + 'static { self.event_store.lock() @@ -633,7 +646,7 @@ impl Client { /// Attaches a handler for when a [`RelationshipAdd`] is received. /// - /// [`RelationshipAdd`]: ../model/enum.Event.html#variant.RelationshipAdd + /// [`RelationshipAdd`]: ../model/event/enum.Event.html#variant.RelationshipAdd pub fn on_relationship_add<F>(&mut self, handler: F) where F: Fn(Context, Relationship) + Send + Sync + 'static { self.event_store.lock() @@ -643,7 +656,7 @@ impl Client { /// Attaches a handler for when a [`RelationshipRemove`] is received. /// - /// [`RelationshipRemove`]: ../model/enum.Event.html#variant.RelationshipRemove + /// [`RelationshipRemove`]: ../model/event/enum.Event.html#variant.RelationshipRemove pub fn on_relationship_remove<F>(&mut self, handler: F) where F: Fn(Context, UserId, RelationshipType) + Send + Sync + 'static { self.event_store.lock() @@ -653,7 +666,7 @@ impl Client { /// Attaches a handler for when a [`Resumed`] is received. /// - /// [`Resumed`]: ../model/enum.Event.html#variant.Resumed + /// [`Resumed`]: ../model/event/enum.Event.html#variant.Resumed pub fn on_resume<F>(&mut self, handler: F) where F: Fn(Context, ResumedEvent) + Send + Sync + 'static { self.event_store.lock() @@ -663,7 +676,7 @@ impl Client { /// Attaches a handler for when a [`TypingStart`] is received. /// - /// [`TypingStart`]: ../model/enum.Event.html#variant.TypingStart + /// [`TypingStart`]: ../model/event/enum.Event.html#variant.TypingStart pub fn on_typing_start<F>(&mut self, handler: F) where F: Fn(Context, TypingStartEvent) + Send + Sync + 'static { self.event_store.lock() @@ -673,7 +686,7 @@ impl Client { /// Attaches a handler for when an [`Unknown`] is received. /// - /// [`Unknown`]: ../model/enum.Event.html#variant.Unknown + /// [`Unknown`]: ../model/event/enum.Event.html#variant.Unknown pub fn on_unknown<F>(&mut self, handler: F) where F: Fn(Context, String, BTreeMap<String, Value>) + Send + Sync + 'static { self.event_store.lock() @@ -683,7 +696,7 @@ impl Client { /// Attaches a handler for when a [`VoiceServerUpdate`] is received. /// - /// [`VoiceServerUpdate`]: ../model/enum.Event.html#variant.VoiceServerUpdate + /// [`VoiceServerUpdate`]: ../model/event/enum.Event.html#variant.VoiceServerUpdate pub fn on_voice_server_update<F>(&mut self, handler: F) where F: Fn(Context, VoiceServerUpdateEvent) + Send + Sync + 'static { self.event_store.lock() @@ -693,7 +706,7 @@ impl Client { /// Attaches a handler for when a [`VoiceStateUpdate`] is received. /// - /// [`VoiceStateUpdate`]: ../model/enum.Event.html#variant.VoiceStateUpdate + /// [`VoiceStateUpdate`]: ../model/event/enum.Event.html#variant.VoiceStateUpdate pub fn on_voice_state_update<F>(&mut self, handler: F) where F: Fn(Context, Option<GuildId>, VoiceState) + Send + Sync + 'static { self.event_store.lock() @@ -703,7 +716,7 @@ impl Client { /// Attaches a handler for when a [`WebhookUpdate`] is received. /// - /// [`WebhookUpdate`]: ../model/enum.Event.html#variant.WebhookUpdate + /// [`WebhookUpdate`]: ../model/event/enum.Event.html#variant.WebhookUpdate pub fn on_webhook_update<F>(&mut self, handler: F) where F: Fn(Context, GuildId, ChannelId) + Send + Sync + 'static { self.event_store.lock() @@ -808,7 +821,7 @@ impl Client { /// The `ChannelId` is the Id of the channel hosting the call. Returns the /// call from the cache - optionally - if the call was in it. /// - /// [`CallDelete`]: ../model/enum.Event.html#variant.CallDelete + /// [`CallDelete`]: ../model/event/enum.Event.html#variant.CallDelete pub fn on_call_delete<F>(&mut self, handler: F) where F: Fn(Context, ChannelId, Option<Call>) + Send + Sync + 'static { self.event_store.lock() @@ -818,7 +831,7 @@ impl Client { /// Attaches a handler for when a [`CallUpdate`] is received. /// - /// [`CallUpdate`]: ../model/enum.Event.html#variant.CallUpdate + /// [`CallUpdate`]: ../model/event/enum.Event.html#variant.CallUpdate pub fn on_call_update<F>(&mut self, handler: F) where F: Fn(Context, Option<Call>, Option<Call>) + Send + Sync + 'static { self.event_store.lock() @@ -830,7 +843,7 @@ impl Client { /// /// Optionally provides the version of the channel before the update. /// - /// [`ChannelUpdate`]: ../model/enum.Event.html#variant.ChannelUpdate + /// [`ChannelUpdate`]: ../model/event/enum.Event.html#variant.ChannelUpdate pub fn on_channel_update<F>(&mut self, handler: F) where F: Fn(Context, Option<Channel>, Channel) + Send + Sync + 'static { self.event_store.lock() @@ -848,7 +861,7 @@ impl Client { /// is received. If you need to keep it, you can either re-insert it /// yourself back into the Cache or manage it in another way. /// - /// [`GuildDelete`]: ../model/enum.Event.html#variant.GuildDelete + /// [`GuildDelete`]: ../model/event/enum.Event.html#variant.GuildDelete /// [`Role`]: ../model/struct.Role.html /// [`Cache`]: ../ext/cache/struct.Cache.html pub fn on_guild_delete<F>(&mut self, handler: F) @@ -863,7 +876,7 @@ impl Client { /// Returns the user's associated `Member` object, _if_ it existed in the /// cache. /// - /// [`GuildMemberRemove`]: ../model/enum.Event.html#variant.GuildMemberRemove + /// [`GuildMemberRemove`]: ../model/event/enum.Event.html#variant.GuildMemberRemove pub fn on_guild_member_remove<F>(&mut self, handler: F) where F: Fn(Context, GuildId, User, Option<Member>) + Send + Sync + 'static { self.event_store.lock() @@ -873,7 +886,7 @@ impl Client { /// Attaches a handler for when a [`GuildMemberUpdate`] is received. /// - /// [`GuildMemberUpdate`]: ../model/enum.Event.html#variant.GuildMemberUpdate + /// [`GuildMemberUpdate`]: ../model/event/enum.Event.html#variant.GuildMemberUpdate pub fn on_guild_member_update<F>(&mut self, handler: F) where F: Fn(Context, Option<Member>, Member) + Send + Sync + 'static { self.event_store.lock() @@ -883,7 +896,7 @@ impl Client { /// Attaches a handler for when a [`GuildRoleDelete`] is received. /// - /// [`GuildRoleDelete`]: ../model/enum.Event.html#variant.GuildRoleDelete + /// [`GuildRoleDelete`]: ../model/event/enum.Event.html#variant.GuildRoleDelete pub fn on_guild_role_delete<F>(&mut self, handler: F) where F: Fn(Context, GuildId, RoleId, Option<Role>) + Send + Sync + 'static { self.event_store.lock() @@ -896,7 +909,7 @@ impl Client { /// The optional `Role` is the role prior to updating. This can be `None` if /// it did not exist in the [`Cache`] before the update. /// - /// [`GuildRoleUpdate`]: ../model/enum.Event.html#variant.GuildRoleUpdate + /// [`GuildRoleUpdate`]: ../model/event/enum.Event.html#variant.GuildRoleUpdate /// [`Cache`]: ../ext/cache/struct.Cache.html pub fn on_guild_role_update<F>(&mut self, handler: F) where F: Fn(Context, GuildId, Option<Role>, Role) + Send + Sync + 'static { @@ -907,7 +920,7 @@ impl Client { /// Attaches a handler for when a [`UserGuildSettingsUpdate`] is received. /// - /// [`UserGuildSettingsUpdate`]: ../model/enum.Event.html#variant.UserGuildSettingsUpdate + /// [`UserGuildSettingsUpdate`]: ../model/event/enum.Event.html#variant.UserGuildSettingsUpdate pub fn on_user_guild_settings_update<F>(&mut self, handler: F) where F: Fn(Context, Option<UserGuildSettings>, UserGuildSettings) + Send + Sync + 'static { self.event_store.lock() @@ -917,7 +930,7 @@ impl Client { /// Attaches a handler for when a [`GuildUpdate`] is received. /// - /// [`GuildUpdate`]: ../model/enum.Event.html#variant.GuildUpdate + /// [`GuildUpdate`]: ../model/event/enum.Event.html#variant.GuildUpdate pub fn on_guild_update<F>(&mut self, handler: F) where F: Fn(Context, Option<Guild>, PartialGuild) + Send + Sync + 'static { self.event_store.lock() @@ -930,7 +943,7 @@ impl Client { /// Optionally returns the old note for the [`User`], if one existed. /// /// [`User`]: ../model/struct.User.html - /// [`UserNoteUpdate`]: ../model/enum.Event.html#variant.UserNoteUpdate + /// [`UserNoteUpdate`]: ../model/event/enum.Event.html#variant.UserNoteUpdate pub fn on_note_update<F>(&mut self, handler: F) where F: Fn(Context, UserId, Option<String>, String) + Send + Sync + 'static { self.event_store.lock() @@ -942,7 +955,7 @@ impl Client { /// /// The old user settings will be provided as well. /// - /// [`UserSettingsUpdate`]: ../model/enum.Event.html#variant.UserSettingsUpdate + /// [`UserSettingsUpdate`]: ../model/event/enum.Event.html#variant.UserSettingsUpdate pub fn on_user_settings_update<F>(&mut self, handler: F) where F: Fn(Context, UserSettings, UserSettings) + Send + Sync + 'static { self.event_store.lock() @@ -954,7 +967,7 @@ impl Client { /// /// The old current user will be provided as well. /// - /// [`UserUpdate`]: ../model/enum.Event.html#variant.UserUpdate + /// [`UserUpdate`]: ../model/event/enum.Event.html#variant.UserUpdate pub fn on_user_update<F>(&mut self, handler: F) where F: Fn(Context, CurrentUser, CurrentUser) + Send + Sync + 'static { self.event_store.lock() @@ -967,7 +980,7 @@ impl Client { impl Client { /// Attaches a handler for when a [`CallDelete`] is received. /// - /// [`CallDelete`]: ../model/enum.Event.html#variant.CallDelete + /// [`CallDelete`]: ../model/event/enum.Event.html#variant.CallDelete pub fn on_call_delete<F>(&mut self, handler: F) where F: Fn(Context, ChannelId) + Send + Sync + 'static { self.event_store.lock() @@ -977,7 +990,7 @@ impl Client { /// Attaches a handler for when a [`CallUpdate`] is received. /// - /// [`CallUpdate`]: ../model/enum.Event.html#variant.CallUpdate + /// [`CallUpdate`]: ../model/event/enum.Event.html#variant.CallUpdate pub fn on_call_update<F>(&mut self, handler: F) where F: Fn(Context, CallUpdateEvent) + Send + Sync + 'static { self.event_store.lock() @@ -987,7 +1000,7 @@ impl Client { /// Attaches a handler for when a [`ChannelUpdate`] is received. /// - /// [`ChannelUpdate`]: ../model/enum.Event.html#variant.ChannelUpdate + /// [`ChannelUpdate`]: ../model/event/enum.Event.html#variant.ChannelUpdate pub fn on_channel_update<F>(&mut self, handler: F) where F: Fn(Context, Channel) + Send + Sync + 'static { self.event_store.lock() @@ -997,7 +1010,7 @@ impl Client { /// Attaches a handler for when a [`GuildDelete`] is received. /// - /// [`GuildDelete`]: ../model/enum.Event.html#variant.GuildDelete + /// [`GuildDelete`]: ../model/event/enum.Event.html#variant.GuildDelete /// [`Role`]: ../model/struct.Role.html /// [`Cache`]: ../ext/cache/struct.Cache.html pub fn on_guild_delete<F>(&mut self, handler: F) @@ -1012,7 +1025,7 @@ impl Client { /// Returns the user's associated `Member` object, _if_ it existed in the /// cache. /// - /// [`GuildMemberRemove`]: ../model/enum.Event.html#variant.GuildMemberRemove + /// [`GuildMemberRemove`]: ../model/event/enum.Event.html#variant.GuildMemberRemove pub fn on_guild_member_remove<F>(&mut self, handler: F) where F: Fn(Context, GuildId, User) + Send + Sync + 'static { self.event_store.lock() @@ -1022,7 +1035,7 @@ impl Client { /// Attaches a handler for when a [`GuildMemberUpdate`] is received. /// - /// [`GuildMemberUpdate`]: ../model/enum.Event.html#variant.GuildMemberUpdate + /// [`GuildMemberUpdate`]: ../model/event/enum.Event.html#variant.GuildMemberUpdate pub fn on_guild_member_update<F>(&mut self, handler: F) where F: Fn(Context, GuildMemberUpdateEvent) + Send + Sync + 'static { self.event_store.lock() @@ -1032,7 +1045,7 @@ impl Client { /// Attaches a handler for when a [`GuildRoleDelete`] is received. /// - /// [`GuildRoleDelete`]: ../model/enum.Event.html#variant.GuildRoleDelete + /// [`GuildRoleDelete`]: ../model/event/enum.Event.html#variant.GuildRoleDelete pub fn on_guild_role_delete<F>(&mut self, handler: F) where F: Fn(Context, GuildId, RoleId) + Send + Sync + 'static { self.event_store.lock() @@ -1042,7 +1055,7 @@ impl Client { /// Attaches a handler for when a [`GuildRoleUpdate`] is received. /// - /// [`GuildRoleUpdate`]: ../model/enum.Event.html#variant.GuildRoleUpdate + /// [`GuildRoleUpdate`]: ../model/event/enum.Event.html#variant.GuildRoleUpdate /// [`Cache`]: ../ext/cache/struct.Cache.html pub fn on_guild_role_update<F>(&mut self, handler: F) where F: Fn(Context, GuildId, Role) + Send + Sync + 'static { @@ -1053,7 +1066,7 @@ impl Client { /// Attaches a handler for when a [`UserGuildSettingsUpdate`] is received. /// - /// [`UserGuildSettingsUpdate`]: ../model/enum.Event.html#variant.UserGuildSettingsUpdate + /// [`UserGuildSettingsUpdate`]: ../model/event/enum.Event.html#variant.UserGuildSettingsUpdate pub fn on_user_guild_settings_update<F>(&mut self, handler: F) where F: Fn(Context, UserGuildSettings) + Send + Sync + 'static { self.event_store.lock() @@ -1063,7 +1076,7 @@ impl Client { /// Attaches a handler for when a [`GuildUpdate`] is received. /// - /// [`GuildUpdate`]: ../model/enum.Event.html#variant.GuildUpdate + /// [`GuildUpdate`]: ../model/event/enum.Event.html#variant.GuildUpdate pub fn on_guild_update<F>(&mut self, handler: F) where F: Fn(Context, PartialGuild) + Send + Sync + 'static { self.event_store.lock() @@ -1076,7 +1089,7 @@ impl Client { /// Optionally returns the old note for the [`User`], if one existed. /// /// [`User`]: ../model/struct.User.html - /// [`UserNoteUpdate`]: ../model/enum.Event.html#variant.UserNoteUpdate + /// [`UserNoteUpdate`]: ../model/event/enum.Event.html#variant.UserNoteUpdate pub fn on_note_update<F>(&mut self, handler: F) where F: Fn(Context, UserId, String) + Send + Sync + 'static { self.event_store.lock() @@ -1086,7 +1099,7 @@ impl Client { /// Attaches a handler for when a [`UserSettingsUpdate`] is received. /// - /// [`UserSettingsUpdate`]: ../model/enum.Event.html#variant.UserSettingsUpdate + /// [`UserSettingsUpdate`]: ../model/event/enum.Event.html#variant.UserSettingsUpdate pub fn on_user_settings_update<F>(&mut self, handler: F) where F: Fn(Context, UserSettingsUpdateEvent) + Send + Sync + 'static { self.event_store.lock() @@ -1096,7 +1109,7 @@ impl Client { /// Attaches a handler for when a [`UserUpdate`] is received. /// - /// [`UserUpdate`]: ../model/enum.Event.html#variant.UserUpdate + /// [`UserUpdate`]: ../model/event/enum.Event.html#variant.UserUpdate pub fn on_user_update<F>(&mut self, handler: F) where F: Fn(Context, CurrentUser) + Send + Sync + 'static { self.event_store.lock() diff --git a/src/ext/cache/mod.rs b/src/ext/cache/mod.rs index 28a8a6c..7c4d3a5 100644 --- a/src/ext/cache/mod.rs +++ b/src/ext/cache/mod.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use std::default::Default; use std::mem; use ::model::*; +use ::model::event::*; /// A cache of all events received over a [`Connection`], where storing at least /// some data from the event is possible. diff --git a/src/ext/framework/mod.rs b/src/ext/framework/mod.rs index 8c807dc..39f2148 100644 --- a/src/ext/framework/mod.rs +++ b/src/ext/framework/mod.rs @@ -82,7 +82,7 @@ pub struct Framework { /// itself. /// /// [`Client::on_message`]: ../../client/struct.Client.html#method.on_message - /// [`Event::MessageCreate`]: ../../model/enum.Event.html#variant.MessageCreate + /// [`Event::MessageCreate`]: ../../model/event/enum.Event.html#variant.MessageCreate pub initialized: bool, } @@ -61,19 +61,19 @@ //! } //! ``` //! +//! [`Cache`]: ext/cache/struct.Cache.html //! [`Client::login_bot`]: client/struct.Client.html#method.login_bot //! [`Client::login_user`]: client/struct.Client.html#method.login_user //! [`Client::on_message`]: client/struct.Client.html#method.on_message -//! [`validate_token`]: client/fn.validate_token.html //! [`Connection`]: client/struct.Connection.html //! [`Context`]: client/struct.Context.html -//! [`Event`]: model/enum.Event.html -//! [`Event::MessageCreate`]: model/enum.Event.html#variant.MessageCreate -//! [`Cache`]: ext/cache/struct.Cache.html +//! [`Event`]: model/event/enum.Event.html +//! [`Event::MessageCreate`]: model/event/enum.Event.html#variant.MessageCreate +//! [cache docs]: ext/cache/index.html //! [client's module-level documentation]: client/index.html //! [docs]: https://discordapp.com/developers/docs/intro //! [examples]: https://github.com/zeyla/serenity.rs/tree/master/examples -//! [cache docs]: ext/cache/index.html +//! [`validate_token`]: client/fn.validate_token.html #![allow(doc_markdown, inline_always, unknown_lints)] #![warn(dead_code, enum_glob_use, if_not_else)] diff --git a/src/model/event.rs b/src/model/event.rs new file mode 100644 index 0000000..673a3e5 --- /dev/null +++ b/src/model/event.rs @@ -0,0 +1,761 @@ +use std::collections::{BTreeMap, HashMap}; +use super::utils::*; +use super::*; +use ::constants::OpCode; +use ::internal::prelude::*; +use ::utils::decode_array; + +#[derive(Clone, Debug)] +pub struct CallCreateEvent { + pub call: Call, +} + +#[derive(Clone, Debug)] +pub struct CallDeleteEvent { + pub channel_id: ChannelId, +} + +#[derive(Clone, Debug)] +pub struct CallUpdateEvent { + pub channel_id: ChannelId, + pub message_id: MessageId, + pub region: String, + pub ringing: Vec<UserId>, +} + +#[derive(Clone, Debug)] +pub struct ChannelCreateEvent { + pub channel: Channel, +} + +#[derive(Clone, Debug)] +pub struct ChannelDeleteEvent { + pub channel: Channel, +} + +#[derive(Clone, Debug)] +pub struct ChannelPinsAckEvent { + pub channel_id: ChannelId, + pub timestamp: String, +} + +#[derive(Clone, Debug)] +pub struct ChannelPinsUpdateEvent { + pub channel_id: ChannelId, + pub last_pin_timestamp: Option<String>, +} + +#[derive(Clone, Debug)] +pub struct ChannelRecipientAddEvent { + pub channel_id: ChannelId, + pub user: User, +} + +#[derive(Clone, Debug)] +pub struct ChannelRecipientRemoveEvent { + pub channel_id: ChannelId, + pub user: User, +} + +#[derive(Clone, Debug)] +pub struct ChannelUpdateEvent { + pub channel: Channel, +} + +#[derive(Clone, Debug)] +pub struct FriendSuggestionCreateEvent { + pub reasons: Vec<SuggestionReason>, + pub suggested_user: User, +} + +#[derive(Clone, Copy, Debug)] +pub struct FriendSuggestionDeleteEvent { + pub suggested_user_id: UserId, +} + +#[derive(Clone, Debug)] +pub struct GuildBanAddEvent { + pub guild_id: GuildId, + pub user: User, +} + +#[derive(Clone, Debug)] +pub struct GuildBanRemoveEvent { + pub guild_id: GuildId, + pub user: User, +} + +#[derive(Clone, Debug)] +pub struct GuildCreateEvent { + pub guild: Guild, +} + +#[derive(Clone, Debug)] +pub struct GuildDeleteEvent { + pub guild: PartialGuild, +} + +#[derive(Clone, Debug)] +pub struct GuildEmojisUpdateEvent { + pub emojis: HashMap<EmojiId, Emoji>, + pub guild_id: GuildId, +} + +#[derive(Clone, Debug)] +pub struct GuildIntegrationsUpdateEvent { + pub guild_id: GuildId, +} + +#[derive(Clone, Debug)] +pub struct GuildMemberAddEvent { + pub guild_id: GuildId, + pub member: Member, +} + +#[derive(Clone, Debug)] +pub struct GuildMemberRemoveEvent { + pub guild_id: GuildId, + pub user: User, +} + +#[derive(Clone, Debug)] +pub struct GuildMemberUpdateEvent { + pub guild_id: GuildId, + pub nick: Option<String>, + pub roles: Vec<RoleId>, + pub user: User, +} + +#[derive(Clone, Debug)] +pub struct GuildMembersChunkEvent { + pub guild_id: GuildId, + pub members: HashMap<UserId, Member>, +} + +#[derive(Clone, Debug)] +pub struct GuildRoleCreateEvent { + pub guild_id: GuildId, + pub role: Role, +} + +#[derive(Clone, Debug)] +pub struct GuildRoleDeleteEvent { + pub guild_id: GuildId, + pub role_id: RoleId, +} + +#[derive(Clone, Debug)] +pub struct GuildRoleUpdateEvent { + pub guild_id: GuildId, + pub role: Role, +} + +#[derive(Clone, Debug)] +pub struct GuildSyncEvent { + pub guild_id: GuildId, + pub large: bool, + pub members: HashMap<UserId, Member>, + pub presences: HashMap<UserId, Presence>, +} + +#[derive(Clone, Debug)] +pub struct GuildUnavailableEvent { + pub guild_id: GuildId, +} + +#[derive(Clone, Debug)] +pub struct GuildUpdateEvent { + pub guild: PartialGuild, +} + +#[derive(Clone, Copy, Debug)] +pub struct MessageAckEvent { + pub channel_id: ChannelId, + /// May be `None` if a private channel with no messages has closed. + pub message_id: Option<MessageId>, +} + +#[derive(Clone, Debug)] +pub struct MessageCreateEvent { + pub message: Message, +} + +#[derive(Clone, Debug)] +pub struct MessageDeleteBulkEvent { + pub channel_id: ChannelId, + pub ids: Vec<MessageId>, +} + +#[derive(Clone, Copy, Debug)] +pub struct MessageDeleteEvent { + pub channel_id: ChannelId, + pub message_id: MessageId, +} + +#[derive(Clone, Debug)] +pub struct MessageUpdateEvent { + pub id: MessageId, + pub channel_id: ChannelId, + pub kind: Option<MessageType>, + pub content: Option<String>, + pub nonce: Option<String>, + pub tts: Option<bool>, + pub pinned: Option<bool>, + pub timestamp: Option<String>, + pub edited_timestamp: Option<String>, + pub author: Option<User>, + pub mention_everyone: Option<bool>, + pub mentions: Option<Vec<User>>, + pub mention_roles: Option<Vec<RoleId>>, + pub attachments: Option<Vec<Attachment>>, + pub embeds: Option<Vec<Value>>, +} + +#[derive(Clone, Debug)] +pub struct PresenceUpdateEvent { + pub guild_id: Option<GuildId>, + pub presence: Presence, + pub roles: Option<Vec<RoleId>>, +} + +#[derive(Clone, Debug)] +pub struct PresencesReplaceEvent { + pub presences: Vec<Presence>, +} + +#[derive(Clone, Debug)] +pub struct ReactionAddEvent { + pub reaction: Reaction, +} + +#[derive(Clone, Debug)] +pub struct ReactionRemoveEvent { + pub reaction: Reaction, +} + +#[derive(Clone, Copy, Debug)] +pub struct ReactionRemoveAllEvent { + pub channel_id: ChannelId, + pub message_id: MessageId, +} + +/// The "Ready" event, containing initial ready cache +#[derive(Clone, Debug)] +pub struct ReadyEvent { + pub ready: Ready, +} + +#[derive(Clone, Debug)] +pub struct RelationshipAddEvent { + pub relationship: Relationship, +} + +#[derive(Clone, Copy, Debug)] +pub struct RelationshipRemoveEvent { + pub kind: RelationshipType, + pub user_id: UserId, +} + +#[derive(Clone, Debug)] +pub struct ResumedEvent { + pub heartbeat_interval: u64, + pub trace: Vec<Option<String>>, +} + +#[derive(Clone, Debug)] +pub struct TypingStartEvent { + pub channel_id: ChannelId, + pub timestamp: u64, + pub user_id: UserId, +} + +#[derive(Clone, Debug)] +pub struct UnknownEvent { + pub kind: String, + pub value: BTreeMap<String, Value> +} + +#[derive(Clone, Debug)] +pub struct UserGuildSettingsUpdateEvent { + pub settings: UserGuildSettings, +} + +#[derive(Clone, Debug)] +pub struct UserNoteUpdateEvent { + pub note: String, + pub user_id: UserId, +} + +#[derive(Clone, Debug)] +pub struct UserUpdateEvent { + pub current_user: CurrentUser, +} + +#[derive(Clone, Debug)] +pub struct UserSettingsUpdateEvent { + pub enable_tts_command: Option<bool>, + pub inline_attachment_media: Option<bool>, + pub inline_embed_media: Option<bool>, + pub locale: Option<String>, + pub message_display_compact: Option<bool>, + pub render_embeds: Option<bool>, + pub show_current_game: Option<bool>, + pub theme: Option<String>, + pub convert_emoticons: Option<bool>, + pub friend_source_flags: Option<FriendSourceFlags>, +} + +#[derive(Clone, Debug)] +pub struct VoiceServerUpdateEvent { + pub channel_id: Option<ChannelId>, + pub endpoint: Option<String>, + pub guild_id: Option<GuildId>, + pub token: String, +} + +#[derive(Clone, Debug)] +pub struct VoiceStateUpdateEvent { + pub guild_id: Option<GuildId>, + pub voice_state: VoiceState, +} + +#[derive(Clone, Debug)] +pub struct WebhookUpdateEvent { + pub channel_id: ChannelId, + pub guild_id: GuildId, +} + +#[derive(Debug, Clone)] +pub enum GatewayEvent { + Dispatch(u64, Event), + Heartbeat(u64), + Reconnect, + InvalidateSession, + Hello(u64), + HeartbeatAck, +} + +impl GatewayEvent { + pub fn decode(value: Value) -> Result<Self> { + let mut value = try!(into_map(value)); + + let op = req!(value.get("op").and_then(|x| x.as_u64())); + + match try!(OpCode::from_num(op).ok_or(Error::Client(ClientError::InvalidOpCode))) { + OpCode::Event => Ok(GatewayEvent::Dispatch( + req!(try!(remove(&mut value, "s")).as_u64()), + try!(Event::decode( + try!(remove(&mut value, "t").and_then(into_string)), + try!(remove(&mut value, "d")) + )) + )), + OpCode::Heartbeat => { + Ok(GatewayEvent::Heartbeat(req!(try!(remove(&mut value, "s")) + .as_u64()))) + }, + OpCode::Reconnect => Ok(GatewayEvent::Reconnect), + OpCode::InvalidSession => Ok(GatewayEvent::InvalidateSession), + OpCode::Hello => { + let mut data = try!(remove(&mut value, "d").and_then(into_map)); + let interval = req!(try!(remove(&mut data, "heartbeat_interval")).as_u64()); + Ok(GatewayEvent::Hello(interval)) + }, + OpCode::HeartbeatAck => Ok(GatewayEvent::HeartbeatAck), + _ => Err(Error::Decode("Unexpected opcode", Value::Object(value))), + } + } +} + +/// Event received over a websocket connection +#[derive(Clone, Debug)] +pub enum Event { + /// A new group call has been created + CallCreate(CallCreateEvent), + /// A group call has been deleted (the call ended) + CallDelete(CallDeleteEvent), + /// A group call has been updated + CallUpdate(CallUpdateEvent), + ChannelCreate(ChannelCreateEvent), + ChannelDelete(ChannelDeleteEvent), + ChannelPinsAck(ChannelPinsAckEvent), + ChannelPinsUpdate(ChannelPinsUpdateEvent), + /// A user has been added to a group + ChannelRecipientAdd(ChannelRecipientAddEvent), + /// A user has been removed from a group + ChannelRecipientRemove(ChannelRecipientRemoveEvent), + ChannelUpdate(ChannelUpdateEvent), + /// When a suggestion for a friend is created, due to a connection like + /// [`Skype`]. + /// + /// [`Connection::Skype`]: enum.Connection.html#variant.Skype + FriendSuggestionCreate(FriendSuggestionCreateEvent), + /// When a suggestion for a friend is removed. + FriendSuggestionDelete(FriendSuggestionDeleteEvent), + GuildBanAdd(GuildBanAddEvent), + GuildBanRemove(GuildBanRemoveEvent), + GuildCreate(GuildCreateEvent), + GuildDelete(GuildDeleteEvent), + GuildEmojisUpdate(GuildEmojisUpdateEvent), + GuildIntegrationsUpdate(GuildIntegrationsUpdateEvent), + GuildMemberAdd(GuildMemberAddEvent), + GuildMemberRemove(GuildMemberRemoveEvent), + /// A member's roles have changed + GuildMemberUpdate(GuildMemberUpdateEvent), + GuildMembersChunk(GuildMembersChunkEvent), + GuildRoleCreate(GuildRoleCreateEvent), + GuildRoleDelete(GuildRoleDeleteEvent), + GuildRoleUpdate(GuildRoleUpdateEvent), + GuildSync(GuildSyncEvent), + /// When a guild is unavailable, such as due to a Discord server outage. + GuildUnavailable(GuildUnavailableEvent), + GuildUpdate(GuildUpdateEvent), + /// Another logged-in device acknowledged this message + MessageAck(MessageAckEvent), + MessageCreate(MessageCreateEvent), + MessageDelete(MessageDeleteEvent), + MessageDeleteBulk(MessageDeleteBulkEvent), + /// A message has been edited, either by the user or the system + MessageUpdate(MessageUpdateEvent), + /// A member's presence state (or username or avatar) has changed + PresenceUpdate(PresenceUpdateEvent), + /// The precense list of the user's friends should be replaced entirely + PresencesReplace(PresencesReplaceEvent), + /// A reaction was added to a message. + /// + /// Fires the [`on_message_reaction_add`] event handler. + /// + /// [`on_message_reaction_add`]: ../client/struct.Client.html#method.on_message_reaction_add + ReactionAdd(ReactionAddEvent), + /// A reaction was removed to a message. + /// + /// Fires the [`on_message_reaction_remove`] event handler. + /// + /// [`on_message_reaction_remove`]: ../client/struct.Client.html#method.on_message_reaction_remove + ReactionRemove(ReactionRemoveEvent), + /// A request was issued to remove all [`Reaction`]s from a [`Message`]. + /// + /// Fires the [`on_reaction_remove_all`] event handler. + /// + /// [`Message`]: struct.Message.html + /// [`Reaction`]: struct.Reaction.html + /// [`on_reaction_remove_all`]: ../client/struct.Clint.html#method.on_reaction_remove_all + ReactionRemoveAll(ReactionRemoveAllEvent), + /// The first event in a connection, containing the initial ready cache. + /// + /// May also be received at a later time in the event of a reconnect. + Ready(ReadyEvent), + RelationshipAdd(RelationshipAddEvent), + RelationshipRemove(RelationshipRemoveEvent), + /// The connection has successfully resumed after a disconnect. + Resumed(ResumedEvent), + /// A user is typing; considered to last 5 seconds + TypingStart(TypingStartEvent), + /// Update to the logged-in user's guild-specific notification settings + UserGuildSettingsUpdate(UserGuildSettingsUpdateEvent), + /// Update to a note that the logged-in user has set for another user. + UserNoteUpdate(UserNoteUpdateEvent), + /// Update to the logged-in user's information + UserUpdate(UserUpdateEvent), + /// Update to the logged-in user's preferences or client settings + UserSettingsUpdate(UserSettingsUpdateEvent), + /// A member's voice state has changed + VoiceStateUpdate(VoiceStateUpdateEvent), + /// Voice server information is available + VoiceServerUpdate(VoiceServerUpdateEvent), + /// A webhook for a [channel][`GuildChannel`] was updated in a [`Guild`]. + /// + /// [`Guild`]: struct.Guild.html + /// [`GuildChannel`]: struct.GuildChannel.html + WebhookUpdate(WebhookUpdateEvent), + /// An event type not covered by the above + Unknown(UnknownEvent), +} + +impl Event { + #[allow(cyclomatic_complexity)] + fn decode(kind: String, value: Value) -> Result<Event> { + if kind == "PRESENCES_REPLACE" { + return Ok(Event::PresencesReplace(PresencesReplaceEvent { + presences: try!(decode_array(value, Presence::decode)), + })); + } + + let mut value = try!(into_map(value)); + + if kind == "CALL_CREATE" { + Ok(Event::CallCreate(CallCreateEvent { + call: try!(Call::decode(Value::Object(value))), + })) + } else if kind == "CALL_DELETE" { + missing!(value, Event::CallDelete(CallDeleteEvent { + channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), + })) + } else if kind == "CALL_UPDATE" { + missing!(value, Event::CallUpdate(CallUpdateEvent { + channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), + message_id: try!(remove(&mut value, "message_id").and_then(MessageId::decode)), + region: try!(remove(&mut value, "region").and_then(into_string)), + ringing: try!(decode_array(try!(remove(&mut value, "ringing")), UserId::decode)), + })) + } else if kind == "CHANNEL_CREATE" { + Ok(Event::ChannelCreate(ChannelCreateEvent { + channel: try!(Channel::decode(Value::Object(value))), + })) + } else if kind == "CHANNEL_DELETE" { + Ok(Event::ChannelDelete(ChannelDeleteEvent { + channel: try!(Channel::decode(Value::Object(value))), + })) + } else if kind == "CHANNEL_PINS_ACK" { + missing!(value, Event::ChannelPinsAck(ChannelPinsAckEvent { + channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), + timestamp: try!(remove(&mut value, "timestamp").and_then(into_string)), + })) + } else if kind == "CHANNEL_PINS_UPDATE" { + missing!(value, Event::ChannelPinsUpdate(ChannelPinsUpdateEvent { + channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), + last_pin_timestamp: try!(opt(&mut value, "last_pin_timestamp", into_string)), + })) + } else if kind == "CHANNEL_RECIPIENT_ADD" { + missing!(value, Event::ChannelRecipientAdd(ChannelRecipientAddEvent { + channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), + user: try!(remove(&mut value, "user").and_then(User::decode)), + })) + } else if kind == "CHANNEL_RECIPIENT_REMOVE" { + missing!(value, Event::ChannelRecipientRemove(ChannelRecipientRemoveEvent { + channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), + user: try!(remove(&mut value, "user").and_then(User::decode)), + })) + } else if kind == "CHANNEL_UPDATE" { + Ok(Event::ChannelUpdate(ChannelUpdateEvent { + channel: try!(Channel::decode(Value::Object(value))), + })) + } else if kind == "FRIEND_SUGGESTION_CREATE" { + missing!(value, Event::FriendSuggestionCreate(FriendSuggestionCreateEvent { + reasons: try!(decode_array(try!(remove(&mut value, "reasons")), SuggestionReason::decode)), + suggested_user: try!(remove(&mut value, "suggested_user").and_then(User::decode)), + })) + } else if kind == "FRIEND_SUGGESTION_DELETE" { + missing!(value, Event::FriendSuggestionDelete(FriendSuggestionDeleteEvent { + suggested_user_id: try!(remove(&mut value, "suggested_user_id").and_then(UserId::decode)), + })) + } else if kind == "GUILD_BAN_ADD" { + missing!(value, Event::GuildBanAdd(GuildBanAddEvent { + guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), + user: try!(remove(&mut value, "user").and_then(User::decode)), + })) + } else if kind == "GUILD_BAN_REMOVE" { + missing!(value, Event::GuildBanRemove(GuildBanRemoveEvent { + guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), + user: try!(remove(&mut value, "user").and_then(User::decode)), + })) + } else if kind == "GUILD_CREATE" { + if remove(&mut value, "unavailable").ok().and_then(|v| v.as_bool()).unwrap_or(false) { + Ok(Event::GuildUnavailable(GuildUnavailableEvent { + guild_id: try!(remove(&mut value, "id").and_then(GuildId::decode)), + })) + } else { + Ok(Event::GuildCreate(GuildCreateEvent { + guild: try!(Guild::decode(Value::Object(value))), + })) + } + } else if kind == "GUILD_DELETE" { + if remove(&mut value, "unavailable").ok().and_then(|v| v.as_bool()).unwrap_or(false) { + Ok(Event::GuildUnavailable(GuildUnavailableEvent { + guild_id: try!(remove(&mut value, "id").and_then(GuildId::decode)), + })) + } else { + Ok(Event::GuildDelete(GuildDeleteEvent { + guild: try!(PartialGuild::decode(Value::Object(value))), + })) + } + } else if kind == "GUILD_EMOJIS_UPDATE" { + missing!(value, Event::GuildEmojisUpdate(GuildEmojisUpdateEvent { + emojis: try!(remove(&mut value, "emojis").and_then(decode_emojis)), + guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), + })) + } else if kind == "GUILD_INTEGRATIONS_UPDATE" { + missing!(value, Event::GuildIntegrationsUpdate(GuildIntegrationsUpdateEvent { + guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), + })) + } else if kind == "GUILD_MEMBER_ADD" { + Ok(Event::GuildMemberAdd(GuildMemberAddEvent { + guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), + member: try!(Member::decode(Value::Object(value))), + })) + } else if kind == "GUILD_MEMBER_REMOVE" { + missing!(value, Event::GuildMemberRemove(GuildMemberRemoveEvent { + guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), + user: try!(remove(&mut value, "user").and_then(User::decode)), + })) + } else if kind == "GUILD_MEMBER_UPDATE" { + missing!(value, Event::GuildMemberUpdate(GuildMemberUpdateEvent { + guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), + nick: try!(opt(&mut value, "nick", into_string)), + roles: try!(decode_array(try!(remove(&mut value, "roles")), RoleId::decode)), + user: try!(remove(&mut value, "user").and_then(User::decode)), + })) + } else if kind == "GUILD_MEMBERS_CHUNK" { + missing!(value, Event::GuildMembersChunk(GuildMembersChunkEvent { + guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), + members: try!(remove(&mut value, "members").and_then(decode_members)), + })) + } else if kind == "GUILD_ROLE_CREATE" { + missing!(value, Event::GuildRoleCreate(GuildRoleCreateEvent { + guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), + role: try!(remove(&mut value, "role").and_then(Role::decode)), + })) + } else if kind == "GUILD_ROLE_DELETE" { + missing!(value, Event::GuildRoleDelete(GuildRoleDeleteEvent { + guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), + role_id: try!(remove(&mut value, "role_id").and_then(RoleId::decode)), + })) + } else if kind == "GUILD_ROLE_UPDATE" { + missing!(value, Event::GuildRoleUpdate(GuildRoleUpdateEvent { + guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), + role: try!(remove(&mut value, "role").and_then(Role::decode)), + })) + } else if kind == "GUILD_SYNC" { + missing!(value, Event::GuildSync(GuildSyncEvent { + guild_id: try!(remove(&mut value, "id").and_then(GuildId::decode)), + large: req!(try!(remove(&mut value, "large")).as_bool()), + members: try!(remove(&mut value, "members").and_then(decode_members)), + presences: try!(remove(&mut value, "presences").and_then(decode_presences)), + })) + } else if kind == "GUILD_UPDATE" { + Ok(Event::GuildUpdate(GuildUpdateEvent { + guild: try!(PartialGuild::decode(Value::Object(value))), + })) + } else if kind == "MESSAGE_ACK" { + missing!(value, Event::MessageAck(MessageAckEvent { + channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), + message_id: try!(opt(&mut value, "message_id", MessageId::decode)), + })) + } else if kind == "MESSAGE_CREATE" { + Ok(Event::MessageCreate(MessageCreateEvent { + message: try!(Message::decode(Value::Object(value))), + })) + } else if kind == "MESSAGE_DELETE" { + missing!(value, Event::MessageDelete(MessageDeleteEvent { + channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), + message_id: try!(remove(&mut value, "id").and_then(MessageId::decode)), + })) + } else if kind == "MESSAGE_DELETE_BULK" { + missing!(value, Event::MessageDeleteBulk(MessageDeleteBulkEvent { + channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), + ids: try!(decode_array(try!(remove(&mut value, "ids")), MessageId::decode)), + })) + } else if kind == "MESSAGE_REACTION_ADD" { + Ok(Event::ReactionAdd(ReactionAddEvent { + reaction: try!(Reaction::decode(Value::Object(value))) + })) + } else if kind == "MESSAG_REACTION_REMOVE" { + Ok(Event::ReactionRemove(ReactionRemoveEvent { + reaction: try!(Reaction::decode(Value::Object(value))) + })) + } else if kind == "MESSAGE_REACTION_REMOVE_ALL" { + Ok(Event::ReactionRemoveAll(ReactionRemoveAllEvent { + channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), + message_id: try!(remove(&mut value, "message_id").and_then(MessageId::decode)), + })) + } else if kind == "MESSAGE_UPDATE" { + missing!(value, Event::MessageUpdate(MessageUpdateEvent { + id: try!(remove(&mut value, "id").and_then(MessageId::decode)), + channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), + kind: try!(opt(&mut value, "type", MessageType::decode)), + content: try!(opt(&mut value, "content", into_string)), + nonce: remove(&mut value, "nonce").and_then(into_string).ok(), + tts: remove(&mut value, "tts").ok().and_then(|v| v.as_bool()), + pinned: remove(&mut value, "pinned").ok().and_then(|v| v.as_bool()), + timestamp: try!(opt(&mut value, "timestamp", into_string)), + edited_timestamp: try!(opt(&mut value, "edited_timestamp", into_string)), + author: try!(opt(&mut value, "author", User::decode)), + mention_everyone: remove(&mut value, "mention_everyone").ok().and_then(|v| v.as_bool()), + mentions: try!(opt(&mut value, "mentions", |v| decode_array(v, User::decode))), + mention_roles: try!(opt(&mut value, "mention_roles", |v| decode_array(v, RoleId::decode))), + attachments: try!(opt(&mut value, "attachments", |v| decode_array(v, Attachment::decode))), + embeds: try!(opt(&mut value, "embeds", |v| decode_array(v, Ok))), + })) + } else if kind == "PRESENCE_UPDATE" { + let guild_id = try!(opt(&mut value, "guild_id", GuildId::decode)); + let roles = try!(opt(&mut value, "roles", |v| decode_array(v, RoleId::decode))); + let presence = try!(Presence::decode(Value::Object(value))); + Ok(Event::PresenceUpdate(PresenceUpdateEvent { + guild_id: guild_id, + presence: presence, + roles: roles, + })) + } else if kind == "RELATIONSHIP_ADD" { + Ok(Event::RelationshipAdd(RelationshipAddEvent { + relationship: try!(Relationship::decode(Value::Object(value))), + })) + } else if kind == "RELATIONSHIP_REMOVE" { + missing!(value, Event::RelationshipRemove(RelationshipRemoveEvent { + kind: try!(remove(&mut value, "type").and_then(RelationshipType::decode)), + user_id: try!(remove(&mut value, "id").and_then(UserId::decode)), + })) + } else if kind == "READY" { + Ok(Event::Ready(ReadyEvent { + ready: try!(Ready::decode(Value::Object(value))), + })) + } else if kind == "RESUMED" { + missing!(value, Event::Resumed(ResumedEvent { + heartbeat_interval: req!(try!(remove(&mut value, "heartbeat_interval")).as_u64()), + trace: try!(remove(&mut value, "_trace").and_then(|v| decode_array(v, |v| Ok(into_string(v).ok())))), + })) + } else if kind == "TYPING_START" { + missing!(value, Event::TypingStart(TypingStartEvent { + channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), + timestamp: req!(try!(remove(&mut value, "timestamp")).as_u64()), + user_id: try!(remove(&mut value, "user_id").and_then(UserId::decode)), + })) + } else if kind == "USER_GUILD_SETTINGS_UPDATE" { + Ok(Event::UserGuildSettingsUpdate(UserGuildSettingsUpdateEvent { + settings: try!(UserGuildSettings::decode(Value::Object(value))), + })) + } else if kind == "USER_NOTE_UPDATE" { + missing!(value, Event::UserNoteUpdate(UserNoteUpdateEvent { + note: try!(remove(&mut value, "note").and_then(into_string)), + user_id: try!(remove(&mut value, "id").and_then(UserId::decode)), + })) + } else if kind == "USER_SETTINGS_UPDATE" { + missing!(value, Event::UserSettingsUpdate(UserSettingsUpdateEvent { + enable_tts_command: remove(&mut value, "enable_tts_command").ok().and_then(|v| v.as_bool()), + inline_attachment_media: remove(&mut value, "inline_attachment_media").ok().and_then(|v| v.as_bool()), + inline_embed_media: remove(&mut value, "inline_embed_media").ok().and_then(|v| v.as_bool()), + locale: try!(opt(&mut value, "locale", into_string)), + message_display_compact: remove(&mut value, "message_display_compact").ok().and_then(|v| v.as_bool()), + render_embeds: remove(&mut value, "render_embeds").ok().and_then(|v| v.as_bool()), + show_current_game: remove(&mut value, "show_current_game").ok().and_then(|v| v.as_bool()), + theme: try!(opt(&mut value, "theme", into_string)), + convert_emoticons: remove(&mut value, "convert_emoticons").ok().and_then(|v| v.as_bool()), + friend_source_flags: try!(opt(&mut value, "friend_source_flags", FriendSourceFlags::decode)), + })) + } else if kind == "USER_UPDATE" { + Ok(Event::UserUpdate(UserUpdateEvent { + current_user: try!(CurrentUser::decode(Value::Object(value))), + })) + } else if kind == "VOICE_SERVER_UPDATE" { + missing!(value, Event::VoiceServerUpdate(VoiceServerUpdateEvent { + guild_id: try!(opt(&mut value, "guild_id", GuildId::decode)), + channel_id: try!(opt(&mut value, "channel_id", ChannelId::decode)), + endpoint: try!(opt(&mut value, "endpoint", into_string)), + token: try!(remove(&mut value, "token").and_then(into_string)), + })) + } else if kind == "VOICE_STATE_UPDATE" { + Ok(Event::VoiceStateUpdate(VoiceStateUpdateEvent { + guild_id: try!(opt(&mut value, "guild_id", GuildId::decode)), + voice_state: try!(VoiceState::decode(Value::Object(value))), + })) + } else if kind == "WEBHOOKS_UPDATE" { + Ok(Event::WebhookUpdate(WebhookUpdateEvent { + channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), + guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), + })) + } else { + Ok(Event::Unknown(UnknownEvent { + kind: kind, + value: value, + })) + } + } +} diff --git a/src/model/gateway.rs b/src/model/gateway.rs index c867a00..f188a33 100644 --- a/src/model/gateway.rs +++ b/src/model/gateway.rs @@ -5,761 +5,6 @@ use ::constants::OpCode; use ::internal::prelude::*; use ::utils::decode_array; -#[derive(Clone, Debug)] -pub struct CallCreateEvent { - pub call: Call, -} - -#[derive(Clone, Debug)] -pub struct CallDeleteEvent { - pub channel_id: ChannelId, -} - -#[derive(Clone, Debug)] -pub struct CallUpdateEvent { - pub channel_id: ChannelId, - pub message_id: MessageId, - pub region: String, - pub ringing: Vec<UserId>, -} - -#[derive(Clone, Debug)] -pub struct ChannelCreateEvent { - pub channel: Channel, -} - -#[derive(Clone, Debug)] -pub struct ChannelDeleteEvent { - pub channel: Channel, -} - -#[derive(Clone, Debug)] -pub struct ChannelPinsAckEvent { - pub channel_id: ChannelId, - pub timestamp: String, -} - -#[derive(Clone, Debug)] -pub struct ChannelPinsUpdateEvent { - pub channel_id: ChannelId, - pub last_pin_timestamp: Option<String>, -} - -#[derive(Clone, Debug)] -pub struct ChannelRecipientAddEvent { - pub channel_id: ChannelId, - pub user: User, -} - -#[derive(Clone, Debug)] -pub struct ChannelRecipientRemoveEvent { - pub channel_id: ChannelId, - pub user: User, -} - -#[derive(Clone, Debug)] -pub struct ChannelUpdateEvent { - pub channel: Channel, -} - -#[derive(Clone, Debug)] -pub struct FriendSuggestionCreateEvent { - pub reasons: Vec<SuggestionReason>, - pub suggested_user: User, -} - -#[derive(Clone, Copy, Debug)] -pub struct FriendSuggestionDeleteEvent { - pub suggested_user_id: UserId, -} - -#[derive(Clone, Debug)] -pub struct GuildBanAddEvent { - pub guild_id: GuildId, - pub user: User, -} - -#[derive(Clone, Debug)] -pub struct GuildBanRemoveEvent { - pub guild_id: GuildId, - pub user: User, -} - -#[derive(Clone, Debug)] -pub struct GuildCreateEvent { - pub guild: Guild, -} - -#[derive(Clone, Debug)] -pub struct GuildDeleteEvent { - pub guild: PartialGuild, -} - -#[derive(Clone, Debug)] -pub struct GuildEmojisUpdateEvent { - pub emojis: HashMap<EmojiId, Emoji>, - pub guild_id: GuildId, -} - -#[derive(Clone, Debug)] -pub struct GuildIntegrationsUpdateEvent { - pub guild_id: GuildId, -} - -#[derive(Clone, Debug)] -pub struct GuildMemberAddEvent { - pub guild_id: GuildId, - pub member: Member, -} - -#[derive(Clone, Debug)] -pub struct GuildMemberRemoveEvent { - pub guild_id: GuildId, - pub user: User, -} - -#[derive(Clone, Debug)] -pub struct GuildMemberUpdateEvent { - pub guild_id: GuildId, - pub nick: Option<String>, - pub roles: Vec<RoleId>, - pub user: User, -} - -#[derive(Clone, Debug)] -pub struct GuildMembersChunkEvent { - pub guild_id: GuildId, - pub members: HashMap<UserId, Member>, -} - -#[derive(Clone, Debug)] -pub struct GuildRoleCreateEvent { - pub guild_id: GuildId, - pub role: Role, -} - -#[derive(Clone, Debug)] -pub struct GuildRoleDeleteEvent { - pub guild_id: GuildId, - pub role_id: RoleId, -} - -#[derive(Clone, Debug)] -pub struct GuildRoleUpdateEvent { - pub guild_id: GuildId, - pub role: Role, -} - -#[derive(Clone, Debug)] -pub struct GuildSyncEvent { - pub guild_id: GuildId, - pub large: bool, - pub members: HashMap<UserId, Member>, - pub presences: HashMap<UserId, Presence>, -} - -#[derive(Clone, Debug)] -pub struct GuildUnavailableEvent { - pub guild_id: GuildId, -} - -#[derive(Clone, Debug)] -pub struct GuildUpdateEvent { - pub guild: PartialGuild, -} - -#[derive(Clone, Copy, Debug)] -pub struct MessageAckEvent { - pub channel_id: ChannelId, - /// May be `None` if a private channel with no messages has closed. - pub message_id: Option<MessageId>, -} - -#[derive(Clone, Debug)] -pub struct MessageCreateEvent { - pub message: Message, -} - -#[derive(Clone, Debug)] -pub struct MessageDeleteBulkEvent { - pub channel_id: ChannelId, - pub ids: Vec<MessageId>, -} - -#[derive(Clone, Copy, Debug)] -pub struct MessageDeleteEvent { - pub channel_id: ChannelId, - pub message_id: MessageId, -} - -#[derive(Clone, Debug)] -pub struct MessageUpdateEvent { - pub id: MessageId, - pub channel_id: ChannelId, - pub kind: Option<MessageType>, - pub content: Option<String>, - pub nonce: Option<String>, - pub tts: Option<bool>, - pub pinned: Option<bool>, - pub timestamp: Option<String>, - pub edited_timestamp: Option<String>, - pub author: Option<User>, - pub mention_everyone: Option<bool>, - pub mentions: Option<Vec<User>>, - pub mention_roles: Option<Vec<RoleId>>, - pub attachments: Option<Vec<Attachment>>, - pub embeds: Option<Vec<Value>>, -} - -#[derive(Clone, Debug)] -pub struct PresenceUpdateEvent { - pub guild_id: Option<GuildId>, - pub presence: Presence, - pub roles: Option<Vec<RoleId>>, -} - -#[derive(Clone, Debug)] -pub struct PresencesReplaceEvent { - pub presences: Vec<Presence>, -} - -#[derive(Clone, Debug)] -pub struct ReactionAddEvent { - pub reaction: Reaction, -} - -#[derive(Clone, Debug)] -pub struct ReactionRemoveEvent { - pub reaction: Reaction, -} - -#[derive(Clone, Copy, Debug)] -pub struct ReactionRemoveAllEvent { - pub channel_id: ChannelId, - pub message_id: MessageId, -} - -/// The "Ready" event, containing initial ready cache -#[derive(Clone, Debug)] -pub struct ReadyEvent { - pub ready: Ready, -} - -#[derive(Clone, Debug)] -pub struct RelationshipAddEvent { - pub relationship: Relationship, -} - -#[derive(Clone, Copy, Debug)] -pub struct RelationshipRemoveEvent { - pub kind: RelationshipType, - pub user_id: UserId, -} - -#[derive(Clone, Debug)] -pub struct ResumedEvent { - pub heartbeat_interval: u64, - pub trace: Vec<Option<String>>, -} - -#[derive(Clone, Debug)] -pub struct TypingStartEvent { - pub channel_id: ChannelId, - pub timestamp: u64, - pub user_id: UserId, -} - -#[derive(Clone, Debug)] -pub struct UnknownEvent { - pub kind: String, - pub value: BTreeMap<String, Value> -} - -#[derive(Clone, Debug)] -pub struct UserGuildSettingsUpdateEvent { - pub settings: UserGuildSettings, -} - -#[derive(Clone, Debug)] -pub struct UserNoteUpdateEvent { - pub note: String, - pub user_id: UserId, -} - -#[derive(Clone, Debug)] -pub struct UserUpdateEvent { - pub current_user: CurrentUser, -} - -#[derive(Clone, Debug)] -pub struct UserSettingsUpdateEvent { - pub enable_tts_command: Option<bool>, - pub inline_attachment_media: Option<bool>, - pub inline_embed_media: Option<bool>, - pub locale: Option<String>, - pub message_display_compact: Option<bool>, - pub render_embeds: Option<bool>, - pub show_current_game: Option<bool>, - pub theme: Option<String>, - pub convert_emoticons: Option<bool>, - pub friend_source_flags: Option<FriendSourceFlags>, -} - -#[derive(Clone, Debug)] -pub struct VoiceServerUpdateEvent { - pub channel_id: Option<ChannelId>, - pub endpoint: Option<String>, - pub guild_id: Option<GuildId>, - pub token: String, -} - -#[derive(Clone, Debug)] -pub struct VoiceStateUpdateEvent { - pub guild_id: Option<GuildId>, - pub voice_state: VoiceState, -} - -#[derive(Clone, Debug)] -pub struct WebhookUpdateEvent { - pub channel_id: ChannelId, - pub guild_id: GuildId, -} - -#[derive(Debug, Clone)] -pub enum GatewayEvent { - Dispatch(u64, Event), - Heartbeat(u64), - Reconnect, - InvalidateSession, - Hello(u64), - HeartbeatAck, -} - -impl GatewayEvent { - pub fn decode(value: Value) -> Result<Self> { - let mut value = try!(into_map(value)); - - let op = req!(value.get("op").and_then(|x| x.as_u64())); - - match try!(OpCode::from_num(op).ok_or(Error::Client(ClientError::InvalidOpCode))) { - OpCode::Event => Ok(GatewayEvent::Dispatch( - req!(try!(remove(&mut value, "s")).as_u64()), - try!(Event::decode( - try!(remove(&mut value, "t").and_then(into_string)), - try!(remove(&mut value, "d")) - )) - )), - OpCode::Heartbeat => { - Ok(GatewayEvent::Heartbeat(req!(try!(remove(&mut value, "s")) - .as_u64()))) - }, - OpCode::Reconnect => Ok(GatewayEvent::Reconnect), - OpCode::InvalidSession => Ok(GatewayEvent::InvalidateSession), - OpCode::Hello => { - let mut data = try!(remove(&mut value, "d").and_then(into_map)); - let interval = req!(try!(remove(&mut data, "heartbeat_interval")).as_u64()); - Ok(GatewayEvent::Hello(interval)) - }, - OpCode::HeartbeatAck => Ok(GatewayEvent::HeartbeatAck), - _ => Err(Error::Decode("Unexpected opcode", Value::Object(value))), - } - } -} - -/// Event received over a websocket connection -#[derive(Clone, Debug)] -pub enum Event { - /// A new group call has been created - CallCreate(CallCreateEvent), - /// A group call has been deleted (the call ended) - CallDelete(CallDeleteEvent), - /// A group call has been updated - CallUpdate(CallUpdateEvent), - ChannelCreate(ChannelCreateEvent), - ChannelDelete(ChannelDeleteEvent), - ChannelPinsAck(ChannelPinsAckEvent), - ChannelPinsUpdate(ChannelPinsUpdateEvent), - /// A user has been added to a group - ChannelRecipientAdd(ChannelRecipientAddEvent), - /// A user has been removed from a group - ChannelRecipientRemove(ChannelRecipientRemoveEvent), - ChannelUpdate(ChannelUpdateEvent), - /// When a suggestion for a friend is created, due to a connection like - /// [`Skype`]. - /// - /// [`Connection::Skype`]: enum.Connection.html#variant.Skype - FriendSuggestionCreate(FriendSuggestionCreateEvent), - /// When a suggestion for a friend is removed. - FriendSuggestionDelete(FriendSuggestionDeleteEvent), - GuildBanAdd(GuildBanAddEvent), - GuildBanRemove(GuildBanRemoveEvent), - GuildCreate(GuildCreateEvent), - GuildDelete(GuildDeleteEvent), - GuildEmojisUpdate(GuildEmojisUpdateEvent), - GuildIntegrationsUpdate(GuildIntegrationsUpdateEvent), - GuildMemberAdd(GuildMemberAddEvent), - GuildMemberRemove(GuildMemberRemoveEvent), - /// A member's roles have changed - GuildMemberUpdate(GuildMemberUpdateEvent), - GuildMembersChunk(GuildMembersChunkEvent), - GuildRoleCreate(GuildRoleCreateEvent), - GuildRoleDelete(GuildRoleDeleteEvent), - GuildRoleUpdate(GuildRoleUpdateEvent), - GuildSync(GuildSyncEvent), - /// When a guild is unavailable, such as due to a Discord server outage. - GuildUnavailable(GuildUnavailableEvent), - GuildUpdate(GuildUpdateEvent), - /// Another logged-in device acknowledged this message - MessageAck(MessageAckEvent), - MessageCreate(MessageCreateEvent), - MessageDelete(MessageDeleteEvent), - MessageDeleteBulk(MessageDeleteBulkEvent), - /// A message has been edited, either by the user or the system - MessageUpdate(MessageUpdateEvent), - /// A member's presence state (or username or avatar) has changed - PresenceUpdate(PresenceUpdateEvent), - /// The precense list of the user's friends should be replaced entirely - PresencesReplace(PresencesReplaceEvent), - /// A reaction was added to a message. - /// - /// Fires the [`on_message_reaction_add`] event handler. - /// - /// [`on_message_reaction_add`]: ../client/struct.Client.html#method.on_message_reaction_add - ReactionAdd(ReactionAddEvent), - /// A reaction was removed to a message. - /// - /// Fires the [`on_message_reaction_remove`] event handler. - /// - /// [`on_message_reaction_remove`]: ../client/struct.Client.html#method.on_message_reaction_remove - ReactionRemove(ReactionRemoveEvent), - /// A request was issued to remove all [`Reaction`]s from a [`Message`]. - /// - /// Fires the [`on_reaction_remove_all`] event handler. - /// - /// [`Message`]: struct.Message.html - /// [`Reaction`]: struct.Reaction.html - /// [`on_reaction_remove_all`]: ../client/struct.Clint.html#method.on_reaction_remove_all - ReactionRemoveAll(ReactionRemoveAllEvent), - /// The first event in a connection, containing the initial ready cache. - /// - /// May also be received at a later time in the event of a reconnect. - Ready(ReadyEvent), - RelationshipAdd(RelationshipAddEvent), - RelationshipRemove(RelationshipRemoveEvent), - /// The connection has successfully resumed after a disconnect. - Resumed(ResumedEvent), - /// A user is typing; considered to last 5 seconds - TypingStart(TypingStartEvent), - /// Update to the logged-in user's guild-specific notification settings - UserGuildSettingsUpdate(UserGuildSettingsUpdateEvent), - /// Update to a note that the logged-in user has set for another user. - UserNoteUpdate(UserNoteUpdateEvent), - /// Update to the logged-in user's information - UserUpdate(UserUpdateEvent), - /// Update to the logged-in user's preferences or client settings - UserSettingsUpdate(UserSettingsUpdateEvent), - /// A member's voice state has changed - VoiceStateUpdate(VoiceStateUpdateEvent), - /// Voice server information is available - VoiceServerUpdate(VoiceServerUpdateEvent), - /// A webhook for a [channel][`GuildChannel`] was updated in a [`Guild`]. - /// - /// [`Guild`]: struct.Guild.html - /// [`GuildChannel`]: struct.GuildChannel.html - WebhookUpdate(WebhookUpdateEvent), - /// An event type not covered by the above - Unknown(UnknownEvent), -} - -impl Event { - #[allow(cyclomatic_complexity)] - fn decode(kind: String, value: Value) -> Result<Event> { - if kind == "PRESENCES_REPLACE" { - return Ok(Event::PresencesReplace(PresencesReplaceEvent { - presences: try!(decode_array(value, Presence::decode)), - })); - } - - let mut value = try!(into_map(value)); - - if kind == "CALL_CREATE" { - Ok(Event::CallCreate(CallCreateEvent { - call: try!(Call::decode(Value::Object(value))), - })) - } else if kind == "CALL_DELETE" { - missing!(value, Event::CallDelete(CallDeleteEvent { - channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), - })) - } else if kind == "CALL_UPDATE" { - missing!(value, Event::CallUpdate(CallUpdateEvent { - channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), - message_id: try!(remove(&mut value, "message_id").and_then(MessageId::decode)), - region: try!(remove(&mut value, "region").and_then(into_string)), - ringing: try!(decode_array(try!(remove(&mut value, "ringing")), UserId::decode)), - })) - } else if kind == "CHANNEL_CREATE" { - Ok(Event::ChannelCreate(ChannelCreateEvent { - channel: try!(Channel::decode(Value::Object(value))), - })) - } else if kind == "CHANNEL_DELETE" { - Ok(Event::ChannelDelete(ChannelDeleteEvent { - channel: try!(Channel::decode(Value::Object(value))), - })) - } else if kind == "CHANNEL_PINS_ACK" { - missing!(value, Event::ChannelPinsAck(ChannelPinsAckEvent { - channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), - timestamp: try!(remove(&mut value, "timestamp").and_then(into_string)), - })) - } else if kind == "CHANNEL_PINS_UPDATE" { - missing!(value, Event::ChannelPinsUpdate(ChannelPinsUpdateEvent { - channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), - last_pin_timestamp: try!(opt(&mut value, "last_pin_timestamp", into_string)), - })) - } else if kind == "CHANNEL_RECIPIENT_ADD" { - missing!(value, Event::ChannelRecipientAdd(ChannelRecipientAddEvent { - channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), - user: try!(remove(&mut value, "user").and_then(User::decode)), - })) - } else if kind == "CHANNEL_RECIPIENT_REMOVE" { - missing!(value, Event::ChannelRecipientRemove(ChannelRecipientRemoveEvent { - channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), - user: try!(remove(&mut value, "user").and_then(User::decode)), - })) - } else if kind == "CHANNEL_UPDATE" { - Ok(Event::ChannelUpdate(ChannelUpdateEvent { - channel: try!(Channel::decode(Value::Object(value))), - })) - } else if kind == "FRIEND_SUGGESTION_CREATE" { - missing!(value, Event::FriendSuggestionCreate(FriendSuggestionCreateEvent { - reasons: try!(decode_array(try!(remove(&mut value, "reasons")), SuggestionReason::decode)), - suggested_user: try!(remove(&mut value, "suggested_user").and_then(User::decode)), - })) - } else if kind == "FRIEND_SUGGESTION_DELETE" { - missing!(value, Event::FriendSuggestionDelete(FriendSuggestionDeleteEvent { - suggested_user_id: try!(remove(&mut value, "suggested_user_id").and_then(UserId::decode)), - })) - } else if kind == "GUILD_BAN_ADD" { - missing!(value, Event::GuildBanAdd(GuildBanAddEvent { - guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), - user: try!(remove(&mut value, "user").and_then(User::decode)), - })) - } else if kind == "GUILD_BAN_REMOVE" { - missing!(value, Event::GuildBanRemove(GuildBanRemoveEvent { - guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), - user: try!(remove(&mut value, "user").and_then(User::decode)), - })) - } else if kind == "GUILD_CREATE" { - if remove(&mut value, "unavailable").ok().and_then(|v| v.as_bool()).unwrap_or(false) { - Ok(Event::GuildUnavailable(GuildUnavailableEvent { - guild_id: try!(remove(&mut value, "id").and_then(GuildId::decode)), - })) - } else { - Ok(Event::GuildCreate(GuildCreateEvent { - guild: try!(Guild::decode(Value::Object(value))), - })) - } - } else if kind == "GUILD_DELETE" { - if remove(&mut value, "unavailable").ok().and_then(|v| v.as_bool()).unwrap_or(false) { - Ok(Event::GuildUnavailable(GuildUnavailableEvent { - guild_id: try!(remove(&mut value, "id").and_then(GuildId::decode)), - })) - } else { - Ok(Event::GuildDelete(GuildDeleteEvent { - guild: try!(PartialGuild::decode(Value::Object(value))), - })) - } - } else if kind == "GUILD_EMOJIS_UPDATE" { - missing!(value, Event::GuildEmojisUpdate(GuildEmojisUpdateEvent { - emojis: try!(remove(&mut value, "emojis").and_then(decode_emojis)), - guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), - })) - } else if kind == "GUILD_INTEGRATIONS_UPDATE" { - missing!(value, Event::GuildIntegrationsUpdate(GuildIntegrationsUpdateEvent { - guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), - })) - } else if kind == "GUILD_MEMBER_ADD" { - Ok(Event::GuildMemberAdd(GuildMemberAddEvent { - guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), - member: try!(Member::decode(Value::Object(value))), - })) - } else if kind == "GUILD_MEMBER_REMOVE" { - missing!(value, Event::GuildMemberRemove(GuildMemberRemoveEvent { - guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), - user: try!(remove(&mut value, "user").and_then(User::decode)), - })) - } else if kind == "GUILD_MEMBER_UPDATE" { - missing!(value, Event::GuildMemberUpdate(GuildMemberUpdateEvent { - guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), - nick: try!(opt(&mut value, "nick", into_string)), - roles: try!(decode_array(try!(remove(&mut value, "roles")), RoleId::decode)), - user: try!(remove(&mut value, "user").and_then(User::decode)), - })) - } else if kind == "GUILD_MEMBERS_CHUNK" { - missing!(value, Event::GuildMembersChunk(GuildMembersChunkEvent { - guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), - members: try!(remove(&mut value, "members").and_then(decode_members)), - })) - } else if kind == "GUILD_ROLE_CREATE" { - missing!(value, Event::GuildRoleCreate(GuildRoleCreateEvent { - guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), - role: try!(remove(&mut value, "role").and_then(Role::decode)), - })) - } else if kind == "GUILD_ROLE_DELETE" { - missing!(value, Event::GuildRoleDelete(GuildRoleDeleteEvent { - guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), - role_id: try!(remove(&mut value, "role_id").and_then(RoleId::decode)), - })) - } else if kind == "GUILD_ROLE_UPDATE" { - missing!(value, Event::GuildRoleUpdate(GuildRoleUpdateEvent { - guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), - role: try!(remove(&mut value, "role").and_then(Role::decode)), - })) - } else if kind == "GUILD_SYNC" { - missing!(value, Event::GuildSync(GuildSyncEvent { - guild_id: try!(remove(&mut value, "id").and_then(GuildId::decode)), - large: req!(try!(remove(&mut value, "large")).as_bool()), - members: try!(remove(&mut value, "members").and_then(decode_members)), - presences: try!(remove(&mut value, "presences").and_then(decode_presences)), - })) - } else if kind == "GUILD_UPDATE" { - Ok(Event::GuildUpdate(GuildUpdateEvent { - guild: try!(PartialGuild::decode(Value::Object(value))), - })) - } else if kind == "MESSAGE_ACK" { - missing!(value, Event::MessageAck(MessageAckEvent { - channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), - message_id: try!(opt(&mut value, "message_id", MessageId::decode)), - })) - } else if kind == "MESSAGE_CREATE" { - Ok(Event::MessageCreate(MessageCreateEvent { - message: try!(Message::decode(Value::Object(value))), - })) - } else if kind == "MESSAGE_DELETE" { - missing!(value, Event::MessageDelete(MessageDeleteEvent { - channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), - message_id: try!(remove(&mut value, "id").and_then(MessageId::decode)), - })) - } else if kind == "MESSAGE_DELETE_BULK" { - missing!(value, Event::MessageDeleteBulk(MessageDeleteBulkEvent { - channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), - ids: try!(decode_array(try!(remove(&mut value, "ids")), MessageId::decode)), - })) - } else if kind == "MESSAGE_REACTION_ADD" { - Ok(Event::ReactionAdd(ReactionAddEvent { - reaction: try!(Reaction::decode(Value::Object(value))) - })) - } else if kind == "MESSAG_REACTION_REMOVE" { - Ok(Event::ReactionRemove(ReactionRemoveEvent { - reaction: try!(Reaction::decode(Value::Object(value))) - })) - } else if kind == "MESSAGE_REACTION_REMOVE_ALL" { - Ok(Event::ReactionRemoveAll(ReactionRemoveAllEvent { - channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), - message_id: try!(remove(&mut value, "message_id").and_then(MessageId::decode)), - })) - } else if kind == "MESSAGE_UPDATE" { - missing!(value, Event::MessageUpdate(MessageUpdateEvent { - id: try!(remove(&mut value, "id").and_then(MessageId::decode)), - channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), - kind: try!(opt(&mut value, "type", MessageType::decode)), - content: try!(opt(&mut value, "content", into_string)), - nonce: remove(&mut value, "nonce").and_then(into_string).ok(), - tts: remove(&mut value, "tts").ok().and_then(|v| v.as_bool()), - pinned: remove(&mut value, "pinned").ok().and_then(|v| v.as_bool()), - timestamp: try!(opt(&mut value, "timestamp", into_string)), - edited_timestamp: try!(opt(&mut value, "edited_timestamp", into_string)), - author: try!(opt(&mut value, "author", User::decode)), - mention_everyone: remove(&mut value, "mention_everyone").ok().and_then(|v| v.as_bool()), - mentions: try!(opt(&mut value, "mentions", |v| decode_array(v, User::decode))), - mention_roles: try!(opt(&mut value, "mention_roles", |v| decode_array(v, RoleId::decode))), - attachments: try!(opt(&mut value, "attachments", |v| decode_array(v, Attachment::decode))), - embeds: try!(opt(&mut value, "embeds", |v| decode_array(v, Ok))), - })) - } else if kind == "PRESENCE_UPDATE" { - let guild_id = try!(opt(&mut value, "guild_id", GuildId::decode)); - let roles = try!(opt(&mut value, "roles", |v| decode_array(v, RoleId::decode))); - let presence = try!(Presence::decode(Value::Object(value))); - Ok(Event::PresenceUpdate(PresenceUpdateEvent { - guild_id: guild_id, - presence: presence, - roles: roles, - })) - } else if kind == "RELATIONSHIP_ADD" { - Ok(Event::RelationshipAdd(RelationshipAddEvent { - relationship: try!(Relationship::decode(Value::Object(value))), - })) - } else if kind == "RELATIONSHIP_REMOVE" { - missing!(value, Event::RelationshipRemove(RelationshipRemoveEvent { - kind: try!(remove(&mut value, "type").and_then(RelationshipType::decode)), - user_id: try!(remove(&mut value, "id").and_then(UserId::decode)), - })) - } else if kind == "READY" { - Ok(Event::Ready(ReadyEvent { - ready: try!(Ready::decode(Value::Object(value))), - })) - } else if kind == "RESUMED" { - missing!(value, Event::Resumed(ResumedEvent { - heartbeat_interval: req!(try!(remove(&mut value, "heartbeat_interval")).as_u64()), - trace: try!(remove(&mut value, "_trace").and_then(|v| decode_array(v, |v| Ok(into_string(v).ok())))), - })) - } else if kind == "TYPING_START" { - missing!(value, Event::TypingStart(TypingStartEvent { - channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), - timestamp: req!(try!(remove(&mut value, "timestamp")).as_u64()), - user_id: try!(remove(&mut value, "user_id").and_then(UserId::decode)), - })) - } else if kind == "USER_GUILD_SETTINGS_UPDATE" { - Ok(Event::UserGuildSettingsUpdate(UserGuildSettingsUpdateEvent { - settings: try!(UserGuildSettings::decode(Value::Object(value))), - })) - } else if kind == "USER_NOTE_UPDATE" { - missing!(value, Event::UserNoteUpdate(UserNoteUpdateEvent { - note: try!(remove(&mut value, "note").and_then(into_string)), - user_id: try!(remove(&mut value, "id").and_then(UserId::decode)), - })) - } else if kind == "USER_SETTINGS_UPDATE" { - missing!(value, Event::UserSettingsUpdate(UserSettingsUpdateEvent { - enable_tts_command: remove(&mut value, "enable_tts_command").ok().and_then(|v| v.as_bool()), - inline_attachment_media: remove(&mut value, "inline_attachment_media").ok().and_then(|v| v.as_bool()), - inline_embed_media: remove(&mut value, "inline_embed_media").ok().and_then(|v| v.as_bool()), - locale: try!(opt(&mut value, "locale", into_string)), - message_display_compact: remove(&mut value, "message_display_compact").ok().and_then(|v| v.as_bool()), - render_embeds: remove(&mut value, "render_embeds").ok().and_then(|v| v.as_bool()), - show_current_game: remove(&mut value, "show_current_game").ok().and_then(|v| v.as_bool()), - theme: try!(opt(&mut value, "theme", into_string)), - convert_emoticons: remove(&mut value, "convert_emoticons").ok().and_then(|v| v.as_bool()), - friend_source_flags: try!(opt(&mut value, "friend_source_flags", FriendSourceFlags::decode)), - })) - } else if kind == "USER_UPDATE" { - Ok(Event::UserUpdate(UserUpdateEvent { - current_user: try!(CurrentUser::decode(Value::Object(value))), - })) - } else if kind == "VOICE_SERVER_UPDATE" { - missing!(value, Event::VoiceServerUpdate(VoiceServerUpdateEvent { - guild_id: try!(opt(&mut value, "guild_id", GuildId::decode)), - channel_id: try!(opt(&mut value, "channel_id", ChannelId::decode)), - endpoint: try!(opt(&mut value, "endpoint", into_string)), - token: try!(remove(&mut value, "token").and_then(into_string)), - })) - } else if kind == "VOICE_STATE_UPDATE" { - Ok(Event::VoiceStateUpdate(VoiceStateUpdateEvent { - guild_id: try!(opt(&mut value, "guild_id", GuildId::decode)), - voice_state: try!(VoiceState::decode(Value::Object(value))), - })) - } else if kind == "WEBHOOKS_UPDATE" { - Ok(Event::WebhookUpdate(WebhookUpdateEvent { - channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), - guild_id: try!(remove(&mut value, "guild_id").and_then(GuildId::decode)), - })) - } else { - Ok(Event::Unknown(UnknownEvent { - kind: kind, - value: value, - })) - } - } -} - impl Game { #[cfg(feature="methods")] pub fn playing(name: &str) -> Game { diff --git a/src/model/mod.rs b/src/model/mod.rs index 5d1540b..832cda4 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -1,8 +1,10 @@ -pub mod permissions; - #[macro_use] mod utils; +pub mod event; +pub mod permissions; + + mod channel; mod gateway; mod guild; |