twilight_gateway/
event.rs

1//! Optimization for skipping deserialization of unwanted events.
2
3use bitflags::bitflags;
4use twilight_model::gateway::{OpCode, event::EventType};
5
6bitflags! {
7    /// Important optimization for narrowing requested event types.
8    ///
9    /// Specifying event types is an important optimization technique on top of
10    /// [`Intents`], which can dramatically decrease processor usage in many
11    /// circumstances. While specifying intents are required by Discord and
12    /// allow filtering groups of [`Event`]s, event type flags are a
13    /// Twilight-specific technique to filter out individual events from being
14    /// deserialized at all, effectively discarding them.
15    ///
16    /// For example, [`Intents::GUILDS`] includes a wide range of events from
17    /// [`GuildCreate`] to [`RoleUpdate`] to [`ChannelPinsUpdate`]. If the only
18    /// events used in this group of events is, say, [`ChannelCreate`] and
19    /// [`RoleCreate`], then the [`CHANNEL_CREATE`][Self::CHANNEL_CREATE] and
20    /// [`ROLE_CREATE`][Self::ROLE_CREATE] event type flags can be specified in
21    /// combination with that intent to only deserialize those events.
22    ///
23    /// Selected event types only affect the events returned by [`Shard`]s.
24    /// Events necessary for maintaining the connection to Discord, such as
25    /// [`GATEWAY_HEARTBEAT`][`Self::GATEWAY_HEARTBEAT`] and
26    /// [`GATEWAY_HELLO`][`Self::GATEWAY_HELLO`], can safely be excluded and
27    /// won't cause the operation of shards to fail, because shards will always
28    /// parse portions of necessary events.
29    ///
30    /// [`ChannelCreate`]: twilight_model::gateway::event::Event::ChannelCreate
31    /// [`ChannelPinsUpdate`]: twilight_model::gateway::event::Event::ChannelPinsUpdate
32    /// [`Event`]: twilight_model::gateway::event::Event
33    /// [`GuildCreate`]: twilight_model::gateway::event::Event::GuildCreate
34    /// [`Intents`]: twilight_model::gateway::Intents
35    /// [`Intents::GUILDS`]: twilight_model::gateway::Intents::GUILDS
36    /// [`RoleCreate`]: twilight_model::gateway::event::Event::RoleCreate
37    /// [`RoleUpdate`]: twilight_model::gateway::event::Event::RoleUpdate
38    /// [`Shard`]: crate::Shard
39    #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
40    pub struct EventTypeFlags: u128 {
41        /// Message has been blocked by AutoMod according to a rule.
42        const AUTO_MODERATION_ACTION_EXECUTION = 1 << 71;
43        /// [`AutoModerationRule`] has been created.
44        ///
45        /// [`AutoModerationRule`]: crate::guild::auto_moderation::AutoModerationRule
46        const AUTO_MODERATION_RULE_CREATE = 1 << 72;
47        /// [`AutoModerationRule`] has been deleted.
48        ///
49        /// [`AutoModerationRule`]: crate::guild::auto_moderation::AutoModerationRule
50        const AUTO_MODERATION_RULE_DELETE = 1 << 73;
51        /// [`AutoModerationRule`] has been updated.
52        ///
53        /// [`AutoModerationRule`]: crate::guild::auto_moderation::AutoModerationRule
54        const AUTO_MODERATION_RULE_UPDATE = 1 << 74;
55        /// An entitlement has been created.
56        const ENTITLEMENT_CREATE = 1 << 76;
57        /// An entitlement has been deleted.
58        const ENTITLEMENT_DELETE = 1 << 77;
59        /// An entitlement has been updated.
60        const ENTITLEMENT_UPDATE = 1 << 78;
61        /// User has been banned from a guild.
62        const BAN_ADD = 1;
63        /// User has been unbanned from a guild.
64        const BAN_REMOVE = 1 << 1;
65        /// Channel has been created.
66        const CHANNEL_CREATE = 1 << 2;
67        /// Channel has been deleted.
68        const CHANNEL_DELETE = 1 << 3;
69        /// Channel's pins have been updated.
70        const CHANNEL_PINS_UPDATE = 1 << 4;
71        /// Channel has been updated.
72        const CHANNEL_UPDATE = 1 << 5;
73        /// A command's permissions has been updated.
74        const COMMAND_PERMISSIONS_UPDATE = 1 << 70;
75        /// Heartbeat has been created.
76        const GATEWAY_HEARTBEAT = 1 << 6;
77        /// Heartbeat has been acknowledged.
78        const GATEWAY_HEARTBEAT_ACK = 1 << 7;
79        /// A "hello" packet has been received from the gateway.
80        const GATEWAY_HELLO = 1 << 8;
81        /// Shard's session has been invalidated.
82        ///
83        /// A payload containing a boolean is included. If `true` the session is
84        /// resumable. If not, then the shard must initialize a new session.
85        const GATEWAY_INVALIDATE_SESSION = 1 << 69;
86        /// Gateway is indicating that a shard should perform a reconnect.
87        const GATEWAY_RECONNECT = 1 << 9;
88        /// An audit log entry has been created.
89        const GUILD_AUDIT_LOG_ENTRY_CREATE = 1 << 75;
90        /// A guild has been created.
91        const GUILD_CREATE = 1 << 10;
92        /// A guild has been deleted or the current user has been removed from a guild.
93        const GUILD_DELETE = 1 << 11;
94        /// A guild's emojis have been updated.
95        const GUILD_EMOJIS_UPDATE = 1 << 12;
96        /// A guild's integrations have been updated.
97        const GUILD_INTEGRATIONS_UPDATE = 1 << 13;
98        /// A guild's integrations have been updated.
99        const GUILD_SCHEDULED_EVENT_CREATE = 1 << 64;
100        /// A guild's integrations have been updated.
101        const GUILD_SCHEDULED_EVENT_DELETE = 1 << 65;
102        /// A guild's integrations have been updated.
103        const GUILD_SCHEDULED_EVENT_UPDATE = 1 << 66;
104        /// A guild's integrations have been updated.
105        const GUILD_SCHEDULED_EVENT_USER_ADD = 1 << 67;
106        /// A guild's integrations have been updated.
107        const GUILD_SCHEDULED_EVENT_USER_REMOVE = 1 << 68;
108        /// A guild's stickers have been updated.
109        const GUILD_STICKERS_UPDATE = 1 << 63;
110        /// A guild has been updated.
111        const GUILD_UPDATE = 1 << 14;
112        /// A guild integration was created.
113        const INTEGRATION_CREATE = 1 << 60;
114        /// A guild integration was deleted.
115        const INTEGRATION_DELETE = 1 << 61;
116        /// A guild integration was updated.
117        const INTEGRATION_UPDATE = 1 << 62;
118        /// An interaction was invoked by a user.
119        const INTERACTION_CREATE = 1 << 56;
120        /// Invite for a channel has been created.
121        const INVITE_CREATE = 1 << 46;
122        /// Invite for a channel has been deleted.
123        const INVITE_DELETE = 1 << 47;
124        /// Member has been added to a guild.
125        const MEMBER_ADD = 1 << 15;
126        /// Member has been removed from a guild.
127        const MEMBER_REMOVE = 1 << 16;
128        /// Member in a guild has been updated.
129        const MEMBER_UPDATE = 1 << 17;
130        /// Group of members from a guild.
131        ///
132        /// This may be all of the remaining members or not; the chunk index in
133        /// the event payload needs to be confirmed.
134        const MEMBER_CHUNK = 1 << 18;
135        /// Message created in a channel.
136        const MESSAGE_CREATE = 1 << 19;
137        /// Message deleted in a channel.
138        const MESSAGE_DELETE = 1 << 20;
139        /// Multiple messages have been deleted in a channel.
140        const MESSAGE_DELETE_BULK = 1 << 21;
141        /// Message poll vote has been added.
142        const MESSAGE_POLL_VOTE_ADD = 1 << 28;
143        /// Message poll vote has been removed.
144        const MESSAGE_POLL_VOTE_REMOVE = 1 << 29;
145        /// Message in a channel has been updated.
146        const MESSAGE_UPDATE = 1 << 22;
147        /// User's presence details are updated.
148        const PRESENCE_UPDATE = 1 << 23;
149        /// Reaction has been added to a message.
150        const REACTION_ADD = 1 << 25;
151        /// Reaction has been removed from a message.
152        const REACTION_REMOVE = 1 << 26;
153        /// All of the reactions for a message have been removed.
154        const REACTION_REMOVE_ALL = 1 << 27;
155        /// All of a given emoji's reactions for a message have been removed.
156        const REACTION_REMOVE_EMOJI = 1 << 48;
157        /// Session is initialized.
158        const READY = 1 << 28;
159        /// Session is resumed.
160        const RESUMED = 1 << 29;
161        /// Role has been created in a guild.
162        const ROLE_CREATE = 1 << 30;
163        /// Role has been deleted in a guild.
164        const ROLE_DELETE = 1 << 31;
165        /// Role has been updated in a guild.
166        const ROLE_UPDATE = 1 << 32;
167        /// Stage instance was created in a stage channel.
168        const STAGE_INSTANCE_CREATE = 1 << 57;
169        /// Stage instance was deleted in a stage channel.
170        const STAGE_INSTANCE_DELETE = 1 << 58;
171        /// Stage instance was updated in a stage channel.
172        const STAGE_INSTANCE_UPDATE = 1 << 59;
173        /// A thread has been created, relevant to the current user,
174        /// or the current user has been added to a thread.
175        const THREAD_CREATE = 1 << 50;
176        /// A thread, relevant to the current user, has been deleted.
177        const THREAD_DELETE = 1 << 52;
178        /// The current user has gained access to a thread.
179        const THREAD_LIST_SYNC = 1 << 53;
180        /// A user has been added to or removed from a thread.
181        const THREAD_MEMBERS_UPDATE = 1 << 55;
182        /// The thread member object for the current user has been updated.
183        const THREAD_MEMBER_UPDATE = 1 << 54;
184        /// A thread has been updated.
185        const THREAD_UPDATE = 1 << 51;
186        /// User has begun typing in a channel.
187        const TYPING_START = 1 << 39;
188        /// Guild is unavailable, potentially due to an outage.
189        const UNAVAILABLE_GUILD = 1 << 40;
190        /// Current user's profile has been updated.
191        const USER_UPDATE = 1 << 41;
192        /// Voice server has provided an update with voice session details.
193        const VOICE_SERVER_UPDATE = 1 << 42;
194        /// User's state in a voice channel has been updated.
195        const VOICE_STATE_UPDATE = 1 << 43;
196        /// Webhook in a guild has been updated.
197        const WEBHOOKS_UPDATE = 1 << 44;
198        /// The shard encountered a gateway rate limit for an event, e.g.
199        /// request guild members.
200        const RATE_LIMITED = 1 << 79;
201
202        /// All [`EventTypeFlags`] in [`Intents::AUTO_MODERATION_CONFIGURATION`].
203        ///
204        /// [`Intents::AUTO_MODERATION_CONFIGURATION`]: crate::Intents::AUTO_MODERATION_CONFIGURATION
205        const AUTO_MODERATION_CONFIGURATION = Self::AUTO_MODERATION_RULE_CREATE.bits()
206                | Self::AUTO_MODERATION_RULE_DELETE.bits()
207                | Self::AUTO_MODERATION_RULE_UPDATE.bits();
208        /// All [`EventTypeFlags`] in [`Intents::AUTO_MODERATION_EXECUTION`].
209        ///
210        /// [`Intents::AUTO_MODERATION_EXECUTION`]: crate::Intents::AUTO_MODERATION_EXECUTION
211        const AUTO_MODERATION_EXECUTION = Self::AUTO_MODERATION_ACTION_EXECUTION.bits();
212        /// All [`EventTypeFlags`] in [`Intents::DIRECT_MESSAGES`].
213        ///
214        /// [`Intents::DIRECT_MESSAGES`]: crate::Intents::DIRECT_MESSAGES
215        const DIRECT_MESSAGES = Self::MESSAGE_CREATE.bits()
216            | Self::MESSAGE_DELETE.bits()
217            | Self::MESSAGE_DELETE_BULK.bits()
218            | Self::MESSAGE_UPDATE.bits();
219        /// All [`EventTypeFlags`] in [`Intents::DIRECT_MESSAGE_REACTIONS`].
220        ///
221        /// [`Intents::DIRECT_MESSAGE_REACTIONS`]: crate::Intents::DIRECT_MESSAGE_REACTIONS
222        const DIRECT_MESSAGE_REACTIONS = Self::REACTION_ADD.bits()
223            | Self::REACTION_REMOVE.bits()
224            | Self::REACTION_REMOVE_ALL.bits()
225            | Self::REACTION_REMOVE_EMOJI.bits();
226        /// All [`EventTypeFlags`] in [`Intents::DIRECT_MESSAGE_TYPING`].
227        ///
228        /// [`Intents::DIRECT_MESSAGE_TYPING`]: crate::Intents::DIRECT_MESSAGE_TYPING
229        const DIRECT_MESSAGE_TYPING = Self::TYPING_START.bits();
230        /// All [`EventTypeFlags`] in [`Intents::GUILDS`].
231        ///
232        /// [`Intents::GUILDS`]: crate::Intents::GUILDS
233        const GUILDS = Self::CHANNEL_CREATE.bits()
234            | Self::CHANNEL_DELETE.bits()
235            | Self::CHANNEL_PINS_UPDATE.bits()
236            | Self::CHANNEL_UPDATE.bits()
237            | Self::GUILD_CREATE.bits()
238            | Self::GUILD_DELETE.bits()
239            | Self::GUILD_UPDATE.bits()
240            | Self::ROLE_CREATE.bits()
241            | Self::ROLE_DELETE.bits()
242            | Self::ROLE_UPDATE.bits()
243            | Self::STAGE_INSTANCE_CREATE.bits()
244            | Self::STAGE_INSTANCE_UPDATE.bits()
245            | Self::STAGE_INSTANCE_DELETE.bits()
246            | Self::THREAD_CREATE.bits()
247            | Self::THREAD_UPDATE.bits()
248            | Self::THREAD_DELETE.bits()
249            | Self::THREAD_LIST_SYNC.bits()
250            | Self::THREAD_MEMBER_UPDATE.bits()
251            | Self::THREAD_MEMBERS_UPDATE.bits();
252        /// All [`EventTypeFlags`] in [`Intents::GUILD_MODERATION`].
253        ///
254        /// [`Intents::GUILD_MODERATION`]: crate::Intents::GUILD_MODERATION
255        const GUILD_MODERATION = Self::BAN_ADD.bits() | Self::BAN_REMOVE.bits() | Self::GUILD_AUDIT_LOG_ENTRY_CREATE.bits();
256        /// All [`EventTypeFlags`] in [`Intents::GUILD_EMOJIS_AND_STICKERS`].
257        ///
258        /// [`Intents::GUILD_EMOJIS_AND_STICKERS`]: crate::Intents::GUILD_EMOJIS_AND_STICKERS
259        const GUILD_EMOJIS_AND_STICKERS = Self::GUILD_EMOJIS_UPDATE.bits()
260            | Self::GUILD_STICKERS_UPDATE.bits();
261
262        /// All [`EventTypeFlags`] in [`Intents::GUILD_INTEGRATIONS`].
263        ///
264        /// [`Intents::GUILD_INTEGRATIONS`]: crate::Intents::GUILD_INTEGRATIONS
265        const GUILD_INTEGRATIONS = Self::GUILD_INTEGRATIONS_UPDATE.bits()
266            | Self::INTEGRATION_CREATE.bits()
267            | Self::INTEGRATION_UPDATE.bits()
268            | Self::INTEGRATION_DELETE.bits();
269
270        /// All [`EventTypeFlags`] in [`Intents::GUILD_INVITES`].
271        ///
272        /// [`Intents::GUILD_INVITES`]: crate::Intents::GUILD_INVITES
273        const GUILD_INVITES = Self::INVITE_CREATE.bits() | Self::INVITE_DELETE.bits();
274
275        /// All [`EventTypeFlags`] in [`Intents::GUILD_MEMBERS`].
276        ///
277        /// [`Intents::GUILD_MEMBERS`]: crate::Intents::GUILD_MEMBERS
278        const GUILD_MEMBERS = Self::MEMBER_ADD.bits()
279            | Self::MEMBER_REMOVE.bits()
280            | Self::MEMBER_UPDATE.bits()
281            | Self::THREAD_MEMBERS_UPDATE.bits();
282
283
284        /// All [`EventTypeFlags`] in [`Intents::GUILD_MESSAGES`].
285        ///
286        /// [`Intents::GUILD_MESSAGES`]: crate::Intents::GUILD_MESSAGES
287        const GUILD_MESSAGES = Self::MESSAGE_CREATE.bits()
288            | Self::MESSAGE_DELETE.bits()
289            | Self::MESSAGE_DELETE_BULK.bits()
290            | Self::MESSAGE_UPDATE.bits();
291
292         /// All [`EventTypeFlags`] in [`Intents::DIRECT_MESSAGE_POLLS`] and [`Intents::GUILD_MESSAGE_POLLS`].
293        ///
294        /// [`Intents::DIRECT_MESSAGE_POLLS`]: crate::Intents::DIRECT_MESSAGE_POLLS
295        /// [`Intents::GUILD_MESSAGE_POLLS`]: crate::Intents::GUILD_MESSAGE_POLLS
296        const MESSAGE_POLLS = Self::MESSAGE_POLL_VOTE_ADD.bits() | Self::MESSAGE_POLL_VOTE_REMOVE.bits();
297
298        /// All [`EventTypeFlags`] in [`Intents::GUILD_MESSAGE_REACTIONS`].
299        ///
300        /// [`Intents::GUILD_MESSAGE_REACTIONS`]: crate::Intents::GUILD_MESSAGE_REACTIONS
301        const GUILD_MESSAGE_REACTIONS = Self::REACTION_ADD.bits()
302            | Self::REACTION_REMOVE.bits()
303            | Self::REACTION_REMOVE_ALL.bits()
304            | Self::REACTION_REMOVE_EMOJI.bits();
305
306        /// All [`EventTypeFlags`] in [`Intents::GUILD_MESSAGE_TYPING`].
307        ///
308        /// [`Intents::GUILD_MESSAGE_TYPING`]: crate::Intents::GUILD_MESSAGE_TYPING
309        const GUILD_MESSAGE_TYPING = Self::TYPING_START.bits();
310
311        /// All [`EventTypeFlags`] in [`Intents::GUILD_PRESENCES`].
312        ///
313        /// [`Intents::GUILD_PRESENCES`]: crate::Intents::GUILD_PRESENCES
314        const GUILD_PRESENCES = Self::PRESENCE_UPDATE.bits();
315
316        /// All [`EventTypeFlags`] in [`Intents::GUILD_SCHEDULED_EVENTS`].
317        ///
318        /// [`Intents::GUILD_SCHEDULED_EVENTS`]: crate::Intents::GUILD_SCHEDULED_EVENTS
319        const GUILD_SCHEDULED_EVENTS = Self::GUILD_SCHEDULED_EVENT_CREATE.bits()
320            | Self::GUILD_SCHEDULED_EVENT_DELETE.bits()
321            | Self::GUILD_SCHEDULED_EVENT_UPDATE.bits()
322            | Self::GUILD_SCHEDULED_EVENT_USER_ADD.bits()
323            | Self::GUILD_SCHEDULED_EVENT_USER_REMOVE.bits();
324
325        /// All [`EventTypeFlags`] in [`Intents::GUILD_VOICE_STATES`].
326        ///
327        /// [`Intents::GUILD_VOICE_STATES`]: crate::Intents::GUILD_VOICE_STATES
328        const GUILD_VOICE_STATES = Self::VOICE_STATE_UPDATE.bits();
329
330        /// All [`EventTypeFlags`] in [`Intents::GUILD_WEBHOOKS`].
331        ///
332        /// [`Intents::GUILD_WEBHOOKS`]: crate::Intents::GUILD_WEBHOOKS
333        const GUILD_WEBHOOKS = Self::WEBHOOKS_UPDATE.bits();
334
335    }
336}
337
338impl From<EventType> for EventTypeFlags {
339    fn from(event_type: EventType) -> Self {
340        match event_type {
341            EventType::AutoModerationActionExecution => Self::AUTO_MODERATION_ACTION_EXECUTION,
342            EventType::AutoModerationRuleCreate => Self::AUTO_MODERATION_RULE_CREATE,
343            EventType::AutoModerationRuleDelete => Self::AUTO_MODERATION_RULE_DELETE,
344            EventType::AutoModerationRuleUpdate => Self::AUTO_MODERATION_RULE_UPDATE,
345            EventType::BanAdd => Self::BAN_ADD,
346            EventType::BanRemove => Self::BAN_REMOVE,
347            EventType::ChannelCreate => Self::CHANNEL_CREATE,
348            EventType::ChannelDelete => Self::CHANNEL_DELETE,
349            EventType::ChannelPinsUpdate => Self::CHANNEL_PINS_UPDATE,
350            EventType::ChannelUpdate => Self::CHANNEL_UPDATE,
351            EventType::CommandPermissionsUpdate => Self::COMMAND_PERMISSIONS_UPDATE,
352            EventType::EntitlementCreate => Self::ENTITLEMENT_CREATE,
353            EventType::EntitlementDelete => Self::ENTITLEMENT_DELETE,
354            EventType::EntitlementUpdate => Self::ENTITLEMENT_UPDATE,
355            EventType::GatewayClose => Self::empty(),
356            EventType::GatewayHeartbeat => Self::GATEWAY_HEARTBEAT,
357            EventType::GatewayHeartbeatAck => Self::GATEWAY_HEARTBEAT_ACK,
358            EventType::GatewayHello => Self::GATEWAY_HELLO,
359            EventType::GatewayInvalidateSession => Self::GATEWAY_INVALIDATE_SESSION,
360            EventType::GatewayReconnect => Self::GATEWAY_RECONNECT,
361            EventType::GuildAuditLogEntryCreate => Self::GUILD_AUDIT_LOG_ENTRY_CREATE,
362            EventType::GuildCreate => Self::GUILD_CREATE,
363            EventType::GuildDelete => Self::GUILD_DELETE,
364            EventType::GuildEmojisUpdate => Self::GUILD_EMOJIS_UPDATE,
365            EventType::GuildIntegrationsUpdate => Self::GUILD_INTEGRATIONS_UPDATE,
366            EventType::GuildScheduledEventCreate => Self::GUILD_SCHEDULED_EVENT_CREATE,
367            EventType::GuildScheduledEventDelete => Self::GUILD_SCHEDULED_EVENT_DELETE,
368            EventType::GuildScheduledEventUpdate => Self::GUILD_SCHEDULED_EVENT_UPDATE,
369            EventType::GuildScheduledEventUserAdd => Self::GUILD_SCHEDULED_EVENT_USER_ADD,
370            EventType::GuildScheduledEventUserRemove => Self::GUILD_SCHEDULED_EVENT_USER_REMOVE,
371            EventType::GuildStickersUpdate => Self::GUILD_STICKERS_UPDATE,
372            EventType::GuildUpdate => Self::GUILD_UPDATE,
373            EventType::IntegrationCreate => Self::INTEGRATION_CREATE,
374            EventType::IntegrationDelete => Self::INTEGRATION_DELETE,
375            EventType::IntegrationUpdate => Self::INTEGRATION_UPDATE,
376            EventType::InteractionCreate => Self::INTERACTION_CREATE,
377            EventType::InviteCreate => Self::INVITE_CREATE,
378            EventType::InviteDelete => Self::INVITE_DELETE,
379            EventType::MemberAdd => Self::MEMBER_ADD,
380            EventType::MemberRemove => Self::MEMBER_REMOVE,
381            EventType::MemberUpdate => Self::MEMBER_UPDATE,
382            EventType::MemberChunk => Self::MEMBER_CHUNK,
383            EventType::MessageCreate => Self::MESSAGE_CREATE,
384            EventType::MessageDelete => Self::MESSAGE_DELETE,
385            EventType::MessageDeleteBulk => Self::MESSAGE_DELETE_BULK,
386            EventType::MessagePollVoteAdd => Self::MESSAGE_POLL_VOTE_ADD,
387            EventType::MessagePollVoteRemove => Self::MESSAGE_POLL_VOTE_REMOVE,
388            EventType::MessageUpdate => Self::MESSAGE_UPDATE,
389            EventType::PresenceUpdate => Self::PRESENCE_UPDATE,
390            EventType::RateLimited => Self::RATE_LIMITED,
391            EventType::ReactionAdd => Self::REACTION_ADD,
392            EventType::ReactionRemove => Self::REACTION_REMOVE,
393            EventType::ReactionRemoveAll => Self::REACTION_REMOVE_ALL,
394            EventType::ReactionRemoveEmoji => Self::REACTION_REMOVE_EMOJI,
395            EventType::Ready => Self::READY,
396            EventType::Resumed => Self::RESUMED,
397            EventType::RoleCreate => Self::ROLE_CREATE,
398            EventType::RoleDelete => Self::ROLE_DELETE,
399            EventType::RoleUpdate => Self::ROLE_UPDATE,
400            EventType::StageInstanceCreate => Self::STAGE_INSTANCE_CREATE,
401            EventType::StageInstanceDelete => Self::STAGE_INSTANCE_DELETE,
402            EventType::StageInstanceUpdate => Self::STAGE_INSTANCE_UPDATE,
403            EventType::ThreadCreate => Self::THREAD_CREATE,
404            EventType::ThreadDelete => Self::THREAD_DELETE,
405            EventType::ThreadListSync => Self::THREAD_LIST_SYNC,
406            EventType::ThreadMembersUpdate => Self::THREAD_MEMBERS_UPDATE,
407            EventType::ThreadMemberUpdate => Self::THREAD_MEMBER_UPDATE,
408            EventType::ThreadUpdate => Self::THREAD_UPDATE,
409            EventType::TypingStart => Self::TYPING_START,
410            EventType::UnavailableGuild => Self::UNAVAILABLE_GUILD,
411            EventType::UserUpdate => Self::USER_UPDATE,
412            EventType::VoiceServerUpdate => Self::VOICE_SERVER_UPDATE,
413            EventType::VoiceStateUpdate => Self::VOICE_STATE_UPDATE,
414            EventType::WebhooksUpdate => Self::WEBHOOKS_UPDATE,
415        }
416    }
417}
418
419impl TryFrom<(OpCode, Option<&str>)> for EventTypeFlags {
420    type Error = ();
421
422    fn try_from((op, event_type): (OpCode, Option<&str>)) -> Result<Self, Self::Error> {
423        match (op, event_type) {
424            (OpCode::Heartbeat, _) => Ok(Self::GATEWAY_HEARTBEAT),
425            (OpCode::Reconnect, _) => Ok(Self::GATEWAY_RECONNECT),
426            (OpCode::InvalidSession, _) => Ok(Self::GATEWAY_INVALIDATE_SESSION),
427            (OpCode::Hello, _) => Ok(Self::GATEWAY_HELLO),
428            (OpCode::HeartbeatAck, _) => Ok(Self::GATEWAY_HEARTBEAT_ACK),
429            (_, Some(event_type)) => EventType::try_from(event_type)
430                .map(Self::from)
431                .map_err(|_| ()),
432            (_, None) => Err(()),
433        }
434    }
435}
436
437#[cfg(test)]
438mod tests {
439    use super::EventTypeFlags;
440    use static_assertions::assert_impl_all;
441    use std::{fmt::Debug, hash::Hash};
442    use twilight_model::gateway::event::EventType;
443
444    assert_impl_all!(
445        EventTypeFlags: Copy,
446        Clone,
447        Debug,
448        Eq,
449        From<EventType>,
450        Hash,
451        PartialEq,
452        Send,
453        Sync,
454    );
455}