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 /// Subscription for a Premium App has been created.
174 const SUBSCRIPTION_CREATE = 1 << 80;
175 /// Subscription for a Premium App has been deleted.
176 const SUBSCRIPTION_DELETE = 1 << 81;
177 /// Subscription for a Premium App has been updated.
178 const SUBSCRIPTION_UPDATE = 1 << 82;
179 /// A thread has been created, relevant to the current user,
180 /// or the current user has been added to a thread.
181 const THREAD_CREATE = 1 << 50;
182 /// A thread, relevant to the current user, has been deleted.
183 const THREAD_DELETE = 1 << 52;
184 /// The current user has gained access to a thread.
185 const THREAD_LIST_SYNC = 1 << 53;
186 /// A user has been added to or removed from a thread.
187 const THREAD_MEMBERS_UPDATE = 1 << 55;
188 /// The thread member object for the current user has been updated.
189 const THREAD_MEMBER_UPDATE = 1 << 54;
190 /// A thread has been updated.
191 const THREAD_UPDATE = 1 << 51;
192 /// User has begun typing in a channel.
193 const TYPING_START = 1 << 39;
194 /// Guild is unavailable, potentially due to an outage.
195 const UNAVAILABLE_GUILD = 1 << 40;
196 /// Current user's profile has been updated.
197 const USER_UPDATE = 1 << 41;
198 /// Voice server has provided an update with voice session details.
199 const VOICE_SERVER_UPDATE = 1 << 42;
200 /// User's state in a voice channel has been updated.
201 const VOICE_STATE_UPDATE = 1 << 43;
202 /// Webhook in a guild has been updated.
203 const WEBHOOKS_UPDATE = 1 << 44;
204 /// The shard encountered a gateway rate limit for an event, e.g.
205 /// request guild members.
206 const RATE_LIMITED = 1 << 79;
207
208 /// All [`EventTypeFlags`] in [`Intents::AUTO_MODERATION_CONFIGURATION`].
209 ///
210 /// [`Intents::AUTO_MODERATION_CONFIGURATION`]: crate::Intents::AUTO_MODERATION_CONFIGURATION
211 const AUTO_MODERATION_CONFIGURATION = Self::AUTO_MODERATION_RULE_CREATE.bits()
212 | Self::AUTO_MODERATION_RULE_DELETE.bits()
213 | Self::AUTO_MODERATION_RULE_UPDATE.bits();
214 /// All [`EventTypeFlags`] in [`Intents::AUTO_MODERATION_EXECUTION`].
215 ///
216 /// [`Intents::AUTO_MODERATION_EXECUTION`]: crate::Intents::AUTO_MODERATION_EXECUTION
217 const AUTO_MODERATION_EXECUTION = Self::AUTO_MODERATION_ACTION_EXECUTION.bits();
218 /// All [`EventTypeFlags`] in [`Intents::DIRECT_MESSAGES`].
219 ///
220 /// [`Intents::DIRECT_MESSAGES`]: crate::Intents::DIRECT_MESSAGES
221 const DIRECT_MESSAGES = Self::MESSAGE_CREATE.bits()
222 | Self::MESSAGE_DELETE.bits()
223 | Self::MESSAGE_DELETE_BULK.bits()
224 | Self::MESSAGE_UPDATE.bits();
225 /// All [`EventTypeFlags`] in [`Intents::DIRECT_MESSAGE_REACTIONS`].
226 ///
227 /// [`Intents::DIRECT_MESSAGE_REACTIONS`]: crate::Intents::DIRECT_MESSAGE_REACTIONS
228 const DIRECT_MESSAGE_REACTIONS = Self::REACTION_ADD.bits()
229 | Self::REACTION_REMOVE.bits()
230 | Self::REACTION_REMOVE_ALL.bits()
231 | Self::REACTION_REMOVE_EMOJI.bits();
232 /// All [`EventTypeFlags`] in [`Intents::DIRECT_MESSAGE_TYPING`].
233 ///
234 /// [`Intents::DIRECT_MESSAGE_TYPING`]: crate::Intents::DIRECT_MESSAGE_TYPING
235 const DIRECT_MESSAGE_TYPING = Self::TYPING_START.bits();
236 /// All [`EventTypeFlags`] in [`Intents::GUILDS`].
237 ///
238 /// [`Intents::GUILDS`]: crate::Intents::GUILDS
239 const GUILDS = Self::CHANNEL_CREATE.bits()
240 | Self::CHANNEL_DELETE.bits()
241 | Self::CHANNEL_PINS_UPDATE.bits()
242 | Self::CHANNEL_UPDATE.bits()
243 | Self::GUILD_CREATE.bits()
244 | Self::GUILD_DELETE.bits()
245 | Self::GUILD_UPDATE.bits()
246 | Self::ROLE_CREATE.bits()
247 | Self::ROLE_DELETE.bits()
248 | Self::ROLE_UPDATE.bits()
249 | Self::STAGE_INSTANCE_CREATE.bits()
250 | Self::STAGE_INSTANCE_UPDATE.bits()
251 | Self::STAGE_INSTANCE_DELETE.bits()
252 | Self::THREAD_CREATE.bits()
253 | Self::THREAD_UPDATE.bits()
254 | Self::THREAD_DELETE.bits()
255 | Self::THREAD_LIST_SYNC.bits()
256 | Self::THREAD_MEMBER_UPDATE.bits()
257 | Self::THREAD_MEMBERS_UPDATE.bits();
258 /// All [`EventTypeFlags`] in [`Intents::GUILD_MODERATION`].
259 ///
260 /// [`Intents::GUILD_MODERATION`]: crate::Intents::GUILD_MODERATION
261 const GUILD_MODERATION = Self::BAN_ADD.bits() | Self::BAN_REMOVE.bits() | Self::GUILD_AUDIT_LOG_ENTRY_CREATE.bits();
262 /// All [`EventTypeFlags`] in [`Intents::GUILD_EMOJIS_AND_STICKERS`].
263 ///
264 /// [`Intents::GUILD_EMOJIS_AND_STICKERS`]: crate::Intents::GUILD_EMOJIS_AND_STICKERS
265 const GUILD_EMOJIS_AND_STICKERS = Self::GUILD_EMOJIS_UPDATE.bits()
266 | Self::GUILD_STICKERS_UPDATE.bits();
267
268 /// All [`EventTypeFlags`] in [`Intents::GUILD_INTEGRATIONS`].
269 ///
270 /// [`Intents::GUILD_INTEGRATIONS`]: crate::Intents::GUILD_INTEGRATIONS
271 const GUILD_INTEGRATIONS = Self::GUILD_INTEGRATIONS_UPDATE.bits()
272 | Self::INTEGRATION_CREATE.bits()
273 | Self::INTEGRATION_UPDATE.bits()
274 | Self::INTEGRATION_DELETE.bits();
275
276 /// All [`EventTypeFlags`] in [`Intents::GUILD_INVITES`].
277 ///
278 /// [`Intents::GUILD_INVITES`]: crate::Intents::GUILD_INVITES
279 const GUILD_INVITES = Self::INVITE_CREATE.bits() | Self::INVITE_DELETE.bits();
280
281 /// All [`EventTypeFlags`] in [`Intents::GUILD_MEMBERS`].
282 ///
283 /// [`Intents::GUILD_MEMBERS`]: crate::Intents::GUILD_MEMBERS
284 const GUILD_MEMBERS = Self::MEMBER_ADD.bits()
285 | Self::MEMBER_REMOVE.bits()
286 | Self::MEMBER_UPDATE.bits()
287 | Self::THREAD_MEMBERS_UPDATE.bits();
288
289
290 /// All [`EventTypeFlags`] in [`Intents::GUILD_MESSAGES`].
291 ///
292 /// [`Intents::GUILD_MESSAGES`]: crate::Intents::GUILD_MESSAGES
293 const GUILD_MESSAGES = Self::MESSAGE_CREATE.bits()
294 | Self::MESSAGE_DELETE.bits()
295 | Self::MESSAGE_DELETE_BULK.bits()
296 | Self::MESSAGE_UPDATE.bits();
297
298 /// All [`EventTypeFlags`] in [`Intents::DIRECT_MESSAGE_POLLS`] and [`Intents::GUILD_MESSAGE_POLLS`].
299 ///
300 /// [`Intents::DIRECT_MESSAGE_POLLS`]: crate::Intents::DIRECT_MESSAGE_POLLS
301 /// [`Intents::GUILD_MESSAGE_POLLS`]: crate::Intents::GUILD_MESSAGE_POLLS
302 const MESSAGE_POLLS = Self::MESSAGE_POLL_VOTE_ADD.bits() | Self::MESSAGE_POLL_VOTE_REMOVE.bits();
303
304 /// All [`EventTypeFlags`] in [`Intents::GUILD_MESSAGE_REACTIONS`].
305 ///
306 /// [`Intents::GUILD_MESSAGE_REACTIONS`]: crate::Intents::GUILD_MESSAGE_REACTIONS
307 const GUILD_MESSAGE_REACTIONS = Self::REACTION_ADD.bits()
308 | Self::REACTION_REMOVE.bits()
309 | Self::REACTION_REMOVE_ALL.bits()
310 | Self::REACTION_REMOVE_EMOJI.bits();
311
312 /// All [`EventTypeFlags`] in [`Intents::GUILD_MESSAGE_TYPING`].
313 ///
314 /// [`Intents::GUILD_MESSAGE_TYPING`]: crate::Intents::GUILD_MESSAGE_TYPING
315 const GUILD_MESSAGE_TYPING = Self::TYPING_START.bits();
316
317 /// All [`EventTypeFlags`] in [`Intents::GUILD_PRESENCES`].
318 ///
319 /// [`Intents::GUILD_PRESENCES`]: crate::Intents::GUILD_PRESENCES
320 const GUILD_PRESENCES = Self::PRESENCE_UPDATE.bits();
321
322 /// All [`EventTypeFlags`] in [`Intents::GUILD_SCHEDULED_EVENTS`].
323 ///
324 /// [`Intents::GUILD_SCHEDULED_EVENTS`]: crate::Intents::GUILD_SCHEDULED_EVENTS
325 const GUILD_SCHEDULED_EVENTS = Self::GUILD_SCHEDULED_EVENT_CREATE.bits()
326 | Self::GUILD_SCHEDULED_EVENT_DELETE.bits()
327 | Self::GUILD_SCHEDULED_EVENT_UPDATE.bits()
328 | Self::GUILD_SCHEDULED_EVENT_USER_ADD.bits()
329 | Self::GUILD_SCHEDULED_EVENT_USER_REMOVE.bits();
330
331 /// All [`EventTypeFlags`] in [`Intents::GUILD_VOICE_STATES`].
332 ///
333 /// [`Intents::GUILD_VOICE_STATES`]: crate::Intents::GUILD_VOICE_STATES
334 const GUILD_VOICE_STATES = Self::VOICE_STATE_UPDATE.bits();
335
336 /// All [`EventTypeFlags`] in [`Intents::GUILD_WEBHOOKS`].
337 ///
338 /// [`Intents::GUILD_WEBHOOKS`]: crate::Intents::GUILD_WEBHOOKS
339 const GUILD_WEBHOOKS = Self::WEBHOOKS_UPDATE.bits();
340
341 }
342}
343
344impl From<EventType> for EventTypeFlags {
345 fn from(event_type: EventType) -> Self {
346 match event_type {
347 EventType::AutoModerationActionExecution => Self::AUTO_MODERATION_ACTION_EXECUTION,
348 EventType::AutoModerationRuleCreate => Self::AUTO_MODERATION_RULE_CREATE,
349 EventType::AutoModerationRuleDelete => Self::AUTO_MODERATION_RULE_DELETE,
350 EventType::AutoModerationRuleUpdate => Self::AUTO_MODERATION_RULE_UPDATE,
351 EventType::BanAdd => Self::BAN_ADD,
352 EventType::BanRemove => Self::BAN_REMOVE,
353 EventType::ChannelCreate => Self::CHANNEL_CREATE,
354 EventType::ChannelDelete => Self::CHANNEL_DELETE,
355 EventType::ChannelPinsUpdate => Self::CHANNEL_PINS_UPDATE,
356 EventType::ChannelUpdate => Self::CHANNEL_UPDATE,
357 EventType::CommandPermissionsUpdate => Self::COMMAND_PERMISSIONS_UPDATE,
358 EventType::EntitlementCreate => Self::ENTITLEMENT_CREATE,
359 EventType::EntitlementDelete => Self::ENTITLEMENT_DELETE,
360 EventType::EntitlementUpdate => Self::ENTITLEMENT_UPDATE,
361 EventType::GatewayClose => Self::empty(),
362 EventType::GatewayHeartbeat => Self::GATEWAY_HEARTBEAT,
363 EventType::GatewayHeartbeatAck => Self::GATEWAY_HEARTBEAT_ACK,
364 EventType::GatewayHello => Self::GATEWAY_HELLO,
365 EventType::GatewayInvalidateSession => Self::GATEWAY_INVALIDATE_SESSION,
366 EventType::GatewayReconnect => Self::GATEWAY_RECONNECT,
367 EventType::GuildAuditLogEntryCreate => Self::GUILD_AUDIT_LOG_ENTRY_CREATE,
368 EventType::GuildCreate => Self::GUILD_CREATE,
369 EventType::GuildDelete => Self::GUILD_DELETE,
370 EventType::GuildEmojisUpdate => Self::GUILD_EMOJIS_UPDATE,
371 EventType::GuildIntegrationsUpdate => Self::GUILD_INTEGRATIONS_UPDATE,
372 EventType::GuildScheduledEventCreate => Self::GUILD_SCHEDULED_EVENT_CREATE,
373 EventType::GuildScheduledEventDelete => Self::GUILD_SCHEDULED_EVENT_DELETE,
374 EventType::GuildScheduledEventUpdate => Self::GUILD_SCHEDULED_EVENT_UPDATE,
375 EventType::GuildScheduledEventUserAdd => Self::GUILD_SCHEDULED_EVENT_USER_ADD,
376 EventType::GuildScheduledEventUserRemove => Self::GUILD_SCHEDULED_EVENT_USER_REMOVE,
377 EventType::GuildStickersUpdate => Self::GUILD_STICKERS_UPDATE,
378 EventType::GuildUpdate => Self::GUILD_UPDATE,
379 EventType::IntegrationCreate => Self::INTEGRATION_CREATE,
380 EventType::IntegrationDelete => Self::INTEGRATION_DELETE,
381 EventType::IntegrationUpdate => Self::INTEGRATION_UPDATE,
382 EventType::InteractionCreate => Self::INTERACTION_CREATE,
383 EventType::InviteCreate => Self::INVITE_CREATE,
384 EventType::InviteDelete => Self::INVITE_DELETE,
385 EventType::MemberAdd => Self::MEMBER_ADD,
386 EventType::MemberRemove => Self::MEMBER_REMOVE,
387 EventType::MemberUpdate => Self::MEMBER_UPDATE,
388 EventType::MemberChunk => Self::MEMBER_CHUNK,
389 EventType::MessageCreate => Self::MESSAGE_CREATE,
390 EventType::MessageDelete => Self::MESSAGE_DELETE,
391 EventType::MessageDeleteBulk => Self::MESSAGE_DELETE_BULK,
392 EventType::MessagePollVoteAdd => Self::MESSAGE_POLL_VOTE_ADD,
393 EventType::MessagePollVoteRemove => Self::MESSAGE_POLL_VOTE_REMOVE,
394 EventType::MessageUpdate => Self::MESSAGE_UPDATE,
395 EventType::PresenceUpdate => Self::PRESENCE_UPDATE,
396 EventType::RateLimited => Self::RATE_LIMITED,
397 EventType::ReactionAdd => Self::REACTION_ADD,
398 EventType::ReactionRemove => Self::REACTION_REMOVE,
399 EventType::ReactionRemoveAll => Self::REACTION_REMOVE_ALL,
400 EventType::ReactionRemoveEmoji => Self::REACTION_REMOVE_EMOJI,
401 EventType::Ready => Self::READY,
402 EventType::Resumed => Self::RESUMED,
403 EventType::RoleCreate => Self::ROLE_CREATE,
404 EventType::RoleDelete => Self::ROLE_DELETE,
405 EventType::RoleUpdate => Self::ROLE_UPDATE,
406 EventType::StageInstanceCreate => Self::STAGE_INSTANCE_CREATE,
407 EventType::StageInstanceDelete => Self::STAGE_INSTANCE_DELETE,
408 EventType::StageInstanceUpdate => Self::STAGE_INSTANCE_UPDATE,
409 EventType::SubscriptionCreate => Self::SUBSCRIPTION_CREATE,
410 EventType::SubscriptionDelete => Self::SUBSCRIPTION_DELETE,
411 EventType::SubscriptionUpdate => Self::SUBSCRIPTION_UPDATE,
412 EventType::ThreadCreate => Self::THREAD_CREATE,
413 EventType::ThreadDelete => Self::THREAD_DELETE,
414 EventType::ThreadListSync => Self::THREAD_LIST_SYNC,
415 EventType::ThreadMembersUpdate => Self::THREAD_MEMBERS_UPDATE,
416 EventType::ThreadMemberUpdate => Self::THREAD_MEMBER_UPDATE,
417 EventType::ThreadUpdate => Self::THREAD_UPDATE,
418 EventType::TypingStart => Self::TYPING_START,
419 EventType::UnavailableGuild => Self::UNAVAILABLE_GUILD,
420 EventType::UserUpdate => Self::USER_UPDATE,
421 EventType::VoiceServerUpdate => Self::VOICE_SERVER_UPDATE,
422 EventType::VoiceStateUpdate => Self::VOICE_STATE_UPDATE,
423 EventType::WebhooksUpdate => Self::WEBHOOKS_UPDATE,
424 }
425 }
426}
427
428impl TryFrom<(OpCode, Option<&str>)> for EventTypeFlags {
429 type Error = ();
430
431 fn try_from((op, event_type): (OpCode, Option<&str>)) -> Result<Self, Self::Error> {
432 match (op, event_type) {
433 (OpCode::Heartbeat, _) => Ok(Self::GATEWAY_HEARTBEAT),
434 (OpCode::Reconnect, _) => Ok(Self::GATEWAY_RECONNECT),
435 (OpCode::InvalidSession, _) => Ok(Self::GATEWAY_INVALIDATE_SESSION),
436 (OpCode::Hello, _) => Ok(Self::GATEWAY_HELLO),
437 (OpCode::HeartbeatAck, _) => Ok(Self::GATEWAY_HEARTBEAT_ACK),
438 (_, Some(event_type)) => EventType::try_from(event_type)
439 .map(Self::from)
440 .map_err(|_| ()),
441 (_, None) => Err(()),
442 }
443 }
444}
445
446#[cfg(test)]
447mod tests {
448 use super::EventTypeFlags;
449 use static_assertions::assert_impl_all;
450 use std::{fmt::Debug, hash::Hash};
451 use twilight_model::gateway::event::EventType;
452
453 assert_impl_all!(
454 EventTypeFlags: Copy,
455 Clone,
456 Debug,
457 Eq,
458 From<EventType>,
459 Hash,
460 PartialEq,
461 Send,
462 Sync,
463 );
464}