twilight_gateway/event.rs
1//! Optimization for skipping deserialization of unwanted events.
2
3use bitflags::bitflags;
4use twilight_model::gateway::{event::EventType, OpCode};
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
199 /// All [`EventTypeFlags`] in [`Intents::AUTO_MODERATION_CONFIGURATION`].
200 ///
201 /// [`Intents::AUTO_MODERATION_CONFIGURATION`]: crate::Intents::AUTO_MODERATION_CONFIGURATION
202 const AUTO_MODERATION_CONFIGURATION = Self::AUTO_MODERATION_RULE_CREATE.bits()
203 | Self::AUTO_MODERATION_RULE_DELETE.bits()
204 | Self::AUTO_MODERATION_RULE_UPDATE.bits();
205 /// All [`EventTypeFlags`] in [`Intents::AUTO_MODERATION_EXECUTION`].
206 ///
207 /// [`Intents::AUTO_MODERATION_EXECUTION`]: crate::Intents::AUTO_MODERATION_EXECUTION
208 const AUTO_MODERATION_EXECUTION = Self::AUTO_MODERATION_ACTION_EXECUTION.bits();
209 /// All [`EventTypeFlags`] in [`Intents::DIRECT_MESSAGES`].
210 ///
211 /// [`Intents::DIRECT_MESSAGES`]: crate::Intents::DIRECT_MESSAGES
212 const DIRECT_MESSAGES = Self::MESSAGE_CREATE.bits()
213 | Self::MESSAGE_DELETE.bits()
214 | Self::MESSAGE_DELETE_BULK.bits()
215 | Self::MESSAGE_UPDATE.bits();
216 /// All [`EventTypeFlags`] in [`Intents::DIRECT_MESSAGE_REACTIONS`].
217 ///
218 /// [`Intents::DIRECT_MESSAGE_REACTIONS`]: crate::Intents::DIRECT_MESSAGE_REACTIONS
219 const DIRECT_MESSAGE_REACTIONS = Self::REACTION_ADD.bits()
220 | Self::REACTION_REMOVE.bits()
221 | Self::REACTION_REMOVE_ALL.bits()
222 | Self::REACTION_REMOVE_EMOJI.bits();
223 /// All [`EventTypeFlags`] in [`Intents::DIRECT_MESSAGE_TYPING`].
224 ///
225 /// [`Intents::DIRECT_MESSAGE_TYPING`]: crate::Intents::DIRECT_MESSAGE_TYPING
226 const DIRECT_MESSAGE_TYPING = Self::TYPING_START.bits();
227 /// All [`EventTypeFlags`] in [`Intents::GUILDS`].
228 ///
229 /// [`Intents::GUILDS`]: crate::Intents::GUILDS
230 const GUILDS = Self::CHANNEL_CREATE.bits()
231 | Self::CHANNEL_DELETE.bits()
232 | Self::CHANNEL_PINS_UPDATE.bits()
233 | Self::CHANNEL_UPDATE.bits()
234 | Self::GUILD_CREATE.bits()
235 | Self::GUILD_DELETE.bits()
236 | Self::GUILD_UPDATE.bits()
237 | Self::ROLE_CREATE.bits()
238 | Self::ROLE_DELETE.bits()
239 | Self::ROLE_UPDATE.bits()
240 | Self::STAGE_INSTANCE_CREATE.bits()
241 | Self::STAGE_INSTANCE_UPDATE.bits()
242 | Self::STAGE_INSTANCE_DELETE.bits()
243 | Self::THREAD_CREATE.bits()
244 | Self::THREAD_UPDATE.bits()
245 | Self::THREAD_DELETE.bits()
246 | Self::THREAD_LIST_SYNC.bits()
247 | Self::THREAD_MEMBER_UPDATE.bits()
248 | Self::THREAD_MEMBERS_UPDATE.bits();
249 /// All [`EventTypeFlags`] in [`Intents::GUILD_MODERATION`].
250 ///
251 /// [`Intents::GUILD_MODERATION`]: crate::Intents::GUILD_MODERATION
252 const GUILD_MODERATION = Self::BAN_ADD.bits() | Self::BAN_REMOVE.bits() | Self::GUILD_AUDIT_LOG_ENTRY_CREATE.bits();
253 /// All [`EventTypeFlags`] in [`Intents::GUILD_EMOJIS_AND_STICKERS`].
254 ///
255 /// [`Intents::GUILD_EMOJIS_AND_STICKERS`]: crate::Intents::GUILD_EMOJIS_AND_STICKERS
256 const GUILD_EMOJIS_AND_STICKERS = Self::GUILD_EMOJIS_UPDATE.bits()
257 | Self::GUILD_STICKERS_UPDATE.bits();
258
259 /// All [`EventTypeFlags`] in [`Intents::GUILD_INTEGRATIONS`].
260 ///
261 /// [`Intents::GUILD_INTEGRATIONS`]: crate::Intents::GUILD_INTEGRATIONS
262 const GUILD_INTEGRATIONS = Self::GUILD_INTEGRATIONS_UPDATE.bits()
263 | Self::INTEGRATION_CREATE.bits()
264 | Self::INTEGRATION_UPDATE.bits()
265 | Self::INTEGRATION_DELETE.bits();
266
267 /// All [`EventTypeFlags`] in [`Intents::GUILD_INVITES`].
268 ///
269 /// [`Intents::GUILD_INVITES`]: crate::Intents::GUILD_INVITES
270 const GUILD_INVITES = Self::INVITE_CREATE.bits() | Self::INVITE_DELETE.bits();
271
272 /// All [`EventTypeFlags`] in [`Intents::GUILD_MEMBERS`].
273 ///
274 /// [`Intents::GUILD_MEMBERS`]: crate::Intents::GUILD_MEMBERS
275 const GUILD_MEMBERS = Self::MEMBER_ADD.bits()
276 | Self::MEMBER_REMOVE.bits()
277 | Self::MEMBER_UPDATE.bits()
278 | Self::THREAD_MEMBERS_UPDATE.bits();
279
280
281 /// All [`EventTypeFlags`] in [`Intents::GUILD_MESSAGES`].
282 ///
283 /// [`Intents::GUILD_MESSAGES`]: crate::Intents::GUILD_MESSAGES
284 const GUILD_MESSAGES = Self::MESSAGE_CREATE.bits()
285 | Self::MESSAGE_DELETE.bits()
286 | Self::MESSAGE_DELETE_BULK.bits()
287 | Self::MESSAGE_UPDATE.bits();
288
289 /// All [`EventTypeFlags`] in [`Intents::DIRECT_MESSAGE_POLLS`] and [`Intents::GUILD_MESSAGE_POLLS`].
290 ///
291 /// [`Intents::DIRECT_MESSAGE_POLLS`]: crate::Intents::DIRECT_MESSAGE_POLLS
292 /// [`Intents::GUILD_MESSAGE_POLLS`]: crate::Intents::GUILD_MESSAGE_POLLS
293 const MESSAGE_POLLS = Self::MESSAGE_POLL_VOTE_ADD.bits() | Self::MESSAGE_POLL_VOTE_REMOVE.bits();
294
295 /// All [`EventTypeFlags`] in [`Intents::GUILD_MESSAGE_REACTIONS`].
296 ///
297 /// [`Intents::GUILD_MESSAGE_REACTIONS`]: crate::Intents::GUILD_MESSAGE_REACTIONS
298 const GUILD_MESSAGE_REACTIONS = Self::REACTION_ADD.bits()
299 | Self::REACTION_REMOVE.bits()
300 | Self::REACTION_REMOVE_ALL.bits()
301 | Self::REACTION_REMOVE_EMOJI.bits();
302
303 /// All [`EventTypeFlags`] in [`Intents::GUILD_MESSAGE_TYPING`].
304 ///
305 /// [`Intents::GUILD_MESSAGE_TYPING`]: crate::Intents::GUILD_MESSAGE_TYPING
306 const GUILD_MESSAGE_TYPING = Self::TYPING_START.bits();
307
308 /// All [`EventTypeFlags`] in [`Intents::GUILD_PRESENCES`].
309 ///
310 /// [`Intents::GUILD_PRESENCES`]: crate::Intents::GUILD_PRESENCES
311 const GUILD_PRESENCES = Self::PRESENCE_UPDATE.bits();
312
313 /// All [`EventTypeFlags`] in [`Intents::GUILD_SCHEDULED_EVENTS`].
314 ///
315 /// [`Intents::GUILD_SCHEDULED_EVENTS`]: crate::Intents::GUILD_SCHEDULED_EVENTS
316 const GUILD_SCHEDULED_EVENTS = Self::GUILD_SCHEDULED_EVENT_CREATE.bits()
317 | Self::GUILD_SCHEDULED_EVENT_DELETE.bits()
318 | Self::GUILD_SCHEDULED_EVENT_UPDATE.bits()
319 | Self::GUILD_SCHEDULED_EVENT_USER_ADD.bits()
320 | Self::GUILD_SCHEDULED_EVENT_USER_REMOVE.bits();
321
322 /// All [`EventTypeFlags`] in [`Intents::GUILD_VOICE_STATES`].
323 ///
324 /// [`Intents::GUILD_VOICE_STATES`]: crate::Intents::GUILD_VOICE_STATES
325 const GUILD_VOICE_STATES = Self::VOICE_STATE_UPDATE.bits();
326
327 /// All [`EventTypeFlags`] in [`Intents::GUILD_WEBHOOKS`].
328 ///
329 /// [`Intents::GUILD_WEBHOOKS`]: crate::Intents::GUILD_WEBHOOKS
330 const GUILD_WEBHOOKS = Self::WEBHOOKS_UPDATE.bits();
331
332 }
333}
334
335impl From<EventType> for EventTypeFlags {
336 fn from(event_type: EventType) -> Self {
337 match event_type {
338 EventType::AutoModerationActionExecution => Self::AUTO_MODERATION_ACTION_EXECUTION,
339 EventType::AutoModerationRuleCreate => Self::AUTO_MODERATION_RULE_CREATE,
340 EventType::AutoModerationRuleDelete => Self::AUTO_MODERATION_RULE_DELETE,
341 EventType::AutoModerationRuleUpdate => Self::AUTO_MODERATION_RULE_UPDATE,
342 EventType::BanAdd => Self::BAN_ADD,
343 EventType::BanRemove => Self::BAN_REMOVE,
344 EventType::ChannelCreate => Self::CHANNEL_CREATE,
345 EventType::ChannelDelete => Self::CHANNEL_DELETE,
346 EventType::ChannelPinsUpdate => Self::CHANNEL_PINS_UPDATE,
347 EventType::ChannelUpdate => Self::CHANNEL_UPDATE,
348 EventType::CommandPermissionsUpdate => Self::COMMAND_PERMISSIONS_UPDATE,
349 EventType::EntitlementCreate => Self::ENTITLEMENT_CREATE,
350 EventType::EntitlementDelete => Self::ENTITLEMENT_DELETE,
351 EventType::EntitlementUpdate => Self::ENTITLEMENT_UPDATE,
352 EventType::GatewayClose => Self::empty(),
353 EventType::GatewayHeartbeat => Self::GATEWAY_HEARTBEAT,
354 EventType::GatewayHeartbeatAck => Self::GATEWAY_HEARTBEAT_ACK,
355 EventType::GatewayHello => Self::GATEWAY_HELLO,
356 EventType::GatewayInvalidateSession => Self::GATEWAY_INVALIDATE_SESSION,
357 EventType::GatewayReconnect => Self::GATEWAY_RECONNECT,
358 EventType::GuildAuditLogEntryCreate => Self::GUILD_AUDIT_LOG_ENTRY_CREATE,
359 EventType::GuildCreate => Self::GUILD_CREATE,
360 EventType::GuildDelete => Self::GUILD_DELETE,
361 EventType::GuildEmojisUpdate => Self::GUILD_EMOJIS_UPDATE,
362 EventType::GuildIntegrationsUpdate => Self::GUILD_INTEGRATIONS_UPDATE,
363 EventType::GuildScheduledEventCreate => Self::GUILD_SCHEDULED_EVENT_CREATE,
364 EventType::GuildScheduledEventDelete => Self::GUILD_SCHEDULED_EVENT_DELETE,
365 EventType::GuildScheduledEventUpdate => Self::GUILD_SCHEDULED_EVENT_UPDATE,
366 EventType::GuildScheduledEventUserAdd => Self::GUILD_SCHEDULED_EVENT_USER_ADD,
367 EventType::GuildScheduledEventUserRemove => Self::GUILD_SCHEDULED_EVENT_USER_REMOVE,
368 EventType::GuildStickersUpdate => Self::GUILD_STICKERS_UPDATE,
369 EventType::GuildUpdate => Self::GUILD_UPDATE,
370 EventType::IntegrationCreate => Self::INTEGRATION_CREATE,
371 EventType::IntegrationDelete => Self::INTEGRATION_DELETE,
372 EventType::IntegrationUpdate => Self::INTEGRATION_UPDATE,
373 EventType::InteractionCreate => Self::INTERACTION_CREATE,
374 EventType::InviteCreate => Self::INVITE_CREATE,
375 EventType::InviteDelete => Self::INVITE_DELETE,
376 EventType::MemberAdd => Self::MEMBER_ADD,
377 EventType::MemberRemove => Self::MEMBER_REMOVE,
378 EventType::MemberUpdate => Self::MEMBER_UPDATE,
379 EventType::MemberChunk => Self::MEMBER_CHUNK,
380 EventType::MessageCreate => Self::MESSAGE_CREATE,
381 EventType::MessageDelete => Self::MESSAGE_DELETE,
382 EventType::MessageDeleteBulk => Self::MESSAGE_DELETE_BULK,
383 EventType::MessagePollVoteAdd => Self::MESSAGE_POLL_VOTE_ADD,
384 EventType::MessagePollVoteRemove => Self::MESSAGE_POLL_VOTE_REMOVE,
385 EventType::MessageUpdate => Self::MESSAGE_UPDATE,
386 EventType::PresenceUpdate => Self::PRESENCE_UPDATE,
387 EventType::ReactionAdd => Self::REACTION_ADD,
388 EventType::ReactionRemove => Self::REACTION_REMOVE,
389 EventType::ReactionRemoveAll => Self::REACTION_REMOVE_ALL,
390 EventType::ReactionRemoveEmoji => Self::REACTION_REMOVE_EMOJI,
391 EventType::Ready => Self::READY,
392 EventType::Resumed => Self::RESUMED,
393 EventType::RoleCreate => Self::ROLE_CREATE,
394 EventType::RoleDelete => Self::ROLE_DELETE,
395 EventType::RoleUpdate => Self::ROLE_UPDATE,
396 EventType::StageInstanceCreate => Self::STAGE_INSTANCE_CREATE,
397 EventType::StageInstanceDelete => Self::STAGE_INSTANCE_DELETE,
398 EventType::StageInstanceUpdate => Self::STAGE_INSTANCE_UPDATE,
399 EventType::ThreadCreate => Self::THREAD_CREATE,
400 EventType::ThreadDelete => Self::THREAD_DELETE,
401 EventType::ThreadListSync => Self::THREAD_LIST_SYNC,
402 EventType::ThreadMembersUpdate => Self::THREAD_MEMBERS_UPDATE,
403 EventType::ThreadMemberUpdate => Self::THREAD_MEMBER_UPDATE,
404 EventType::ThreadUpdate => Self::THREAD_UPDATE,
405 EventType::TypingStart => Self::TYPING_START,
406 EventType::UnavailableGuild => Self::UNAVAILABLE_GUILD,
407 EventType::UserUpdate => Self::USER_UPDATE,
408 EventType::VoiceServerUpdate => Self::VOICE_SERVER_UPDATE,
409 EventType::VoiceStateUpdate => Self::VOICE_STATE_UPDATE,
410 EventType::WebhooksUpdate => Self::WEBHOOKS_UPDATE,
411 }
412 }
413}
414
415impl TryFrom<(OpCode, Option<&str>)> for EventTypeFlags {
416 type Error = ();
417
418 fn try_from((op, event_type): (OpCode, Option<&str>)) -> Result<Self, Self::Error> {
419 match (op, event_type) {
420 (OpCode::Heartbeat, _) => Ok(Self::GATEWAY_HEARTBEAT),
421 (OpCode::Reconnect, _) => Ok(Self::GATEWAY_RECONNECT),
422 (OpCode::InvalidSession, _) => Ok(Self::GATEWAY_INVALIDATE_SESSION),
423 (OpCode::Hello, _) => Ok(Self::GATEWAY_HELLO),
424 (OpCode::HeartbeatAck, _) => Ok(Self::GATEWAY_HEARTBEAT_ACK),
425 (_, Some(event_type)) => EventType::try_from(event_type)
426 .map(Self::from)
427 .map_err(|_| ()),
428 (_, None) => Err(()),
429 }
430 }
431}
432
433#[cfg(test)]
434mod tests {
435 use super::EventTypeFlags;
436 use static_assertions::assert_impl_all;
437 use std::{fmt::Debug, hash::Hash};
438 use twilight_model::gateway::event::EventType;
439
440 assert_impl_all!(
441 EventTypeFlags: Copy,
442 Clone,
443 Debug,
444 Eq,
445 From<EventType>,
446 Hash,
447 PartialEq,
448 Send,
449 Sync,
450 );
451}