djs_model/gateway/payload/incoming/channel.rs
1//! Channel, thread, invite, stage, webhook and voice events.
2
3use compact_str::CompactString;
4use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Error as DeError};
5
6use super::{guild_id_event, newtype_event};
7use crate::{
8 application::PartialApplication,
9 channel::{Channel, ChannelType, ThreadMember},
10 emoji::PartialEmoji,
11 gateway::Presence,
12 id::{ChannelId, GuildId, RoleId, SoundboardSoundId, UserId},
13 invite::InviteTargetType,
14 macros::int_enum,
15 serde_util::extras::{self, Extras, Visit},
16 stage_instance::StageInstance,
17 user::User,
18 util::Timestamp,
19 voice::VoiceState,
20};
21
22newtype_event! {
23 /// The data of the [Channel Create](https://docs.discord.com/developers/events/gateway-events#channel-create)
24 /// dispatch event: a guild channel relevant to the bot was created.
25 ///
26 /// discord.js counterpart: the `ChannelCreate` event.
27 ChannelCreate(Channel)
28}
29
30newtype_event! {
31 /// The data of the [Channel Update](https://docs.discord.com/developers/events/gateway-events#channel-update)
32 /// dispatch event (not sent for `last_message_id` changes).
33 ///
34 /// discord.js counterpart: the `ChannelUpdate` event.
35 ChannelUpdate(Channel)
36}
37
38newtype_event! {
39 /// The data of the [Channel Delete](https://docs.discord.com/developers/events/gateway-events#channel-delete)
40 /// dispatch event.
41 ///
42 /// discord.js counterpart: the `ChannelDelete` event.
43 ChannelDelete(Channel)
44}
45
46/// The data of the [Channel Info](https://docs.discord.com/developers/events/gateway-events#channel-info)
47/// dispatch event: the ephemeral data of a guild's channels, sent in response to a
48/// [`RequestChannelInfo`](crate::gateway::payload::RequestChannelInfo) command.
49///
50/// discord.js counterpart: `GatewayChannelInfoDispatchData` (discord-api-types).
51#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
52#[non_exhaustive]
53pub struct ChannelInfo {
54 /// The guild.
55 pub guild_id: GuildId,
56 /// The channels' ephemeral data.
57 #[serde(default)]
58 pub channels: Vec<ChannelInfoChannel>,
59}
60
61/// The [ephemeral data of a channel](https://docs.discord.com/developers/events/gateway-events#channel-info-channel-info-channel-structure)
62/// in a [`ChannelInfo`] event.
63///
64/// discord.js counterpart: `GatewayChannelInfoChannel` (discord-api-types).
65#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
66#[non_exhaustive]
67pub struct ChannelInfoChannel {
68 /// The channel.
69 pub id: ChannelId,
70 /// The voice channel status.
71 #[serde(default, skip_serializing_if = "Option::is_none")]
72 pub status: Option<CompactString>,
73 /// When the voice session started (sent as Unix seconds, D-075).
74 #[serde(
75 default,
76 skip_serializing_if = "Option::is_none",
77 with = "crate::serde_util::option_unix_secs"
78 )]
79 pub voice_start_time: Option<Timestamp>,
80}
81
82/// The data of the [Voice Channel Status Update](https://docs.discord.com/developers/events/gateway-events#voice-channel-status-update)
83/// dispatch event.
84///
85/// discord.js counterpart: the `VoiceChannelStatusUpdate` event.
86#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
87#[non_exhaustive]
88pub struct VoiceChannelStatusUpdate {
89 /// The channel.
90 pub id: ChannelId,
91 /// The guild.
92 pub guild_id: GuildId,
93 /// The new status (`None` when cleared).
94 pub status: Option<CompactString>,
95}
96
97/// The data of the [Voice Channel Start Time Update](https://docs.discord.com/developers/events/gateway-events#voice-channel-start-time-update)
98/// dispatch event.
99///
100/// discord.js counterpart: `GatewayVoiceChannelStartTimeUpdateDispatchData` (discord-api-types).
101#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
102#[non_exhaustive]
103pub struct VoiceChannelStartTimeUpdate {
104 /// The channel.
105 pub id: ChannelId,
106 /// The guild.
107 pub guild_id: GuildId,
108 /// When the voice session started (sent as Unix seconds, D-075).
109 #[serde(
110 default,
111 skip_serializing_if = "Option::is_none",
112 with = "crate::serde_util::option_unix_secs"
113 )]
114 pub voice_start_time: Option<Timestamp>,
115}
116
117/// The data of the [Thread Create](https://docs.discord.com/developers/events/gateway-events#thread-create)
118/// dispatch event: a thread was created, or the bot was added to a private thread (then the
119/// thread includes the bot's [`ThreadMember`]).
120///
121/// Dereferences to the contained [`Channel`].
122///
123/// discord.js counterpart: the `ThreadCreate` event (`newlyCreated`).
124#[derive(Clone, Debug, PartialEq)]
125#[non_exhaustive]
126pub struct ThreadCreate {
127 /// The thread.
128 pub thread: Channel,
129 /// Whether the thread was just created (`false` when the bot was added to an existing
130 /// thread).
131 pub newly_created: bool,
132}
133
134impl core::ops::Deref for ThreadCreate {
135 type Target = Channel;
136
137 fn deref(&self) -> &Channel {
138 &self.thread
139 }
140}
141
142#[derive(Default)]
143struct ThreadCreateExtra {
144 newly_created: bool,
145}
146
147impl<'de> Extras<'de> for ThreadCreateExtra {
148 fn visit<A: serde::de::MapAccess<'de>>(
149 &mut self,
150 key: &str,
151 map: &mut A,
152 ) -> Result<Visit, A::Error> {
153 if key == "newly_created" {
154 self.newly_created = map.next_value()?;
155 Ok(Visit::Consumed)
156 } else {
157 Ok(Visit::Pass)
158 }
159 }
160}
161
162impl<'de> Deserialize<'de> for ThreadCreate {
163 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
164 let (thread, extra) = extras::deserialize::<Channel, ThreadCreateExtra, D>(deserializer)?;
165 Ok(Self {
166 thread: thread.ok_or_else(|| D::Error::custom("missing event data"))?,
167 newly_created: extra.newly_created,
168 })
169 }
170}
171
172impl Serialize for ThreadCreate {
173 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
174 #[derive(Serialize)]
175 struct Flat<'a> {
176 #[serde(flatten)]
177 thread: &'a Channel,
178 #[serde(skip_serializing_if = "core::ops::Not::not")]
179 newly_created: bool,
180 }
181
182 Flat {
183 thread: &self.thread,
184 newly_created: self.newly_created,
185 }
186 .serialize(serializer)
187 }
188}
189
190newtype_event! {
191 /// The data of the [Thread Update](https://docs.discord.com/developers/events/gateway-events#thread-update)
192 /// dispatch event (not sent for `last_message_id` changes).
193 ///
194 /// discord.js counterpart: the `ThreadUpdate` event.
195 ThreadUpdate(Channel)
196}
197
198/// The data of the [Thread Delete](https://docs.discord.com/developers/events/gateway-events#thread-delete)
199/// dispatch event: only the thread's IDs and type.
200///
201/// discord.js counterpart: the `ThreadDelete` event.
202#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
203#[non_exhaustive]
204pub struct ThreadDelete {
205 /// The thread.
206 pub id: ChannelId,
207 /// The guild.
208 pub guild_id: GuildId,
209 /// The parent channel.
210 #[serde(default, skip_serializing_if = "Option::is_none")]
211 pub parent_id: Option<ChannelId>,
212 /// The thread's type.
213 #[serde(rename = "type")]
214 pub kind: ChannelType,
215}
216
217/// The data of the [Thread List Sync](https://docs.discord.com/developers/events/gateway-events#thread-list-sync)
218/// dispatch event: the active threads of channels the bot gained access to.
219///
220/// discord.js counterpart: the `ThreadListSync` event.
221#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
222#[non_exhaustive]
223pub struct ThreadListSync {
224 /// The guild.
225 pub guild_id: GuildId,
226 /// The parent channels whose threads are synced (may include channels without active
227 /// threads); `None` means the whole guild.
228 #[serde(default, skip_serializing_if = "Option::is_none")]
229 pub channel_ids: Option<Vec<ChannelId>>,
230 /// The active threads the bot can access in those channels.
231 #[serde(default)]
232 pub threads: Vec<Channel>,
233 /// The bot's thread members of the synced threads (which threads it was added to).
234 #[serde(default)]
235 pub members: Vec<ThreadMember>,
236}
237
238guild_id_event! {
239 /// The data of the [Thread Member Update](https://docs.discord.com/developers/events/gateway-events#thread-member-update)
240 /// dispatch event: the bot's thread member changed.
241 ///
242 /// discord.js counterpart: the `ThreadMemberUpdate` event.
243 ThreadMemberUpdate {
244 /// The bot's thread member.
245 member: ThreadMember
246 }
247}
248
249/// The data of the [Thread Members Update](https://docs.discord.com/developers/events/gateway-events#thread-members-update)
250/// dispatch event: users were added to or removed from a thread (without the `GUILD_MEMBERS`
251/// intent: only the bot).
252///
253/// discord.js counterpart: the `ThreadMembersUpdate` event.
254#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
255#[non_exhaustive]
256pub struct ThreadMembersUpdate {
257 /// The thread.
258 pub id: ChannelId,
259 /// The guild.
260 pub guild_id: GuildId,
261 /// The approximate number of members, capped at 50.
262 pub member_count: u32,
263 /// The added members.
264 #[serde(default, skip_serializing_if = "Vec::is_empty")]
265 pub added_members: Vec<AddedThreadMember>,
266 /// The removed users.
267 #[serde(default, skip_serializing_if = "Vec::is_empty")]
268 pub removed_member_ids: Vec<UserId>,
269}
270
271/// A thread member added in a [`ThreadMembersUpdate`]: with its guild member (in
272/// [`ThreadMember::member`]) and presence.
273///
274/// Dereferences to the contained [`ThreadMember`].
275///
276/// discord.js counterpart: an element of `GatewayThreadMembersUpdate`'s `added_members` (discord-api-types).
277#[derive(Clone, Debug, PartialEq, Eq)]
278#[non_exhaustive]
279pub struct AddedThreadMember {
280 /// The thread member.
281 pub member: ThreadMember,
282 /// The member's presence.
283 pub presence: Option<Presence>,
284}
285
286impl core::ops::Deref for AddedThreadMember {
287 type Target = ThreadMember;
288
289 fn deref(&self) -> &ThreadMember {
290 &self.member
291 }
292}
293
294#[derive(Default)]
295struct PresenceExtra {
296 presence: Option<Presence>,
297}
298
299impl<'de> Extras<'de> for PresenceExtra {
300 fn visit<A: serde::de::MapAccess<'de>>(
301 &mut self,
302 key: &str,
303 map: &mut A,
304 ) -> Result<Visit, A::Error> {
305 if key == "presence" {
306 self.presence = map.next_value()?;
307 Ok(Visit::Consumed)
308 } else {
309 Ok(Visit::Pass)
310 }
311 }
312}
313
314impl<'de> Deserialize<'de> for AddedThreadMember {
315 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
316 let (member, extra) = extras::deserialize::<ThreadMember, PresenceExtra, D>(deserializer)?;
317 Ok(Self {
318 member: member.ok_or_else(|| D::Error::custom("missing event data"))?,
319 presence: extra.presence,
320 })
321 }
322}
323
324impl Serialize for AddedThreadMember {
325 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
326 #[derive(Serialize)]
327 struct Flat<'a> {
328 #[serde(flatten)]
329 member: &'a ThreadMember,
330 presence: Option<&'a Presence>,
331 }
332
333 Flat {
334 member: &self.member,
335 presence: self.presence.as_ref(),
336 }
337 .serialize(serializer)
338 }
339}
340
341/// The data of the [Channel Pins Update](https://docs.discord.com/developers/events/gateway-events#channel-pins-update)
342/// dispatch event (not sent when a pinned message is deleted).
343///
344/// discord.js counterpart: the `ChannelPinsUpdate` event.
345#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
346#[non_exhaustive]
347pub struct ChannelPinsUpdate {
348 /// The guild.
349 #[serde(default, skip_serializing_if = "Option::is_none")]
350 pub guild_id: Option<GuildId>,
351 /// The channel.
352 pub channel_id: ChannelId,
353 /// When the most recent pin was made.
354 #[serde(default, skip_serializing_if = "Option::is_none")]
355 pub last_pin_timestamp: Option<Timestamp>,
356}
357
358/// The data of the [Invite Create](https://docs.discord.com/developers/events/gateway-events#invite-create)
359/// dispatch event (requires `MANAGE_CHANNELS` on the channel).
360///
361/// discord.js counterpart: the `InviteCreate` event.
362#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
363#[non_exhaustive]
364pub struct InviteCreate {
365 /// The channel the invite is for.
366 pub channel_id: ChannelId,
367 /// The invite code.
368 pub code: CompactString,
369 /// When the invite was created.
370 pub created_at: Timestamp,
371 /// The invite's guild.
372 #[serde(default, skip_serializing_if = "Option::is_none")]
373 pub guild_id: Option<GuildId>,
374 /// Who created the invite.
375 #[serde(default, skip_serializing_if = "Option::is_none")]
376 pub inviter: Option<Box<User>>,
377 /// How long the invite is valid, in seconds (`0` = forever).
378 pub max_age: u32,
379 /// How often it can be used (`0` = unlimited).
380 pub max_uses: u32,
381 /// What a voice channel invite targets.
382 #[serde(default, skip_serializing_if = "Option::is_none")]
383 pub target_type: Option<InviteTargetType>,
384 /// The user whose stream a stream invite shows.
385 #[serde(default, skip_serializing_if = "Option::is_none")]
386 pub target_user: Option<Box<User>>,
387 /// The embedded application an application invite opens.
388 #[serde(default, skip_serializing_if = "Option::is_none")]
389 pub target_application: Option<Box<PartialApplication>>,
390 /// Whether users who join through it are kicked on disconnect unless given a role.
391 #[serde(default)]
392 pub temporary: bool,
393 /// How often it was used (always `0` here).
394 #[serde(default)]
395 pub uses: u32,
396 /// When the invite expires.
397 #[serde(default)]
398 pub expires_at: Option<Timestamp>,
399 /// The roles given to users who accept the invite.
400 #[serde(default, skip_serializing_if = "Vec::is_empty")]
401 pub role_ids: Vec<RoleId>,
402}
403
404/// The data of the [Invite Delete](https://docs.discord.com/developers/events/gateway-events#invite-delete)
405/// dispatch event.
406///
407/// discord.js counterpart: the `InviteDelete` event.
408#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
409#[non_exhaustive]
410pub struct InviteDelete {
411 /// The invite's channel.
412 pub channel_id: ChannelId,
413 /// The invite's guild.
414 #[serde(default, skip_serializing_if = "Option::is_none")]
415 pub guild_id: Option<GuildId>,
416 /// The invite code.
417 pub code: CompactString,
418}
419
420newtype_event! {
421 /// The data of the [Stage Instance Create](https://docs.discord.com/developers/events/gateway-events#stage-instance-create)
422 /// dispatch event: a stage went live.
423 ///
424 /// discord.js counterpart: the `StageInstanceCreate` event.
425 StageInstanceCreate(StageInstance)
426}
427
428newtype_event! {
429 /// The data of the [Stage Instance Update](https://docs.discord.com/developers/events/gateway-events#stage-instance-update)
430 /// dispatch event.
431 ///
432 /// discord.js counterpart: the `StageInstanceUpdate` event.
433 StageInstanceUpdate(StageInstance)
434}
435
436newtype_event! {
437 /// The data of the [Stage Instance Delete](https://docs.discord.com/developers/events/gateway-events#stage-instance-delete)
438 /// dispatch event: a stage ended.
439 ///
440 /// discord.js counterpart: the `StageInstanceDelete` event.
441 StageInstanceDelete(StageInstance)
442}
443
444/// The data of the [Webhooks Update](https://docs.discord.com/developers/events/gateway-events#webhooks-update)
445/// dispatch event: a channel's webhooks changed.
446///
447/// discord.js counterpart: the `WebhooksUpdate` event.
448#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
449#[non_exhaustive]
450pub struct WebhooksUpdate {
451 /// The guild.
452 pub guild_id: GuildId,
453 /// The channel.
454 pub channel_id: ChannelId,
455}
456
457newtype_event! {
458 /// The data of the [Voice State Update](https://docs.discord.com/developers/events/gateway-events#voice-state-update)
459 /// dispatch event: someone joined, left or moved between voice channels.
460 ///
461 /// discord.js counterpart: the `VoiceStateUpdate` event.
462 VoiceStateUpdate(VoiceState)
463}
464
465/// The data of the [Voice Server Update](https://docs.discord.com/developers/events/gateway-events#voice-server-update)
466/// dispatch event: where to connect for voice. `Debug` does not print the token.
467///
468/// discord.js counterpart: the voice server data used by `@discordjs/voice`.
469#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
470#[non_exhaustive]
471pub struct VoiceServerUpdate {
472 /// The voice connection token (secret).
473 pub token: CompactString,
474 /// The guild.
475 pub guild_id: GuildId,
476 /// The voice server host; `None` while the server is reallocated (disconnect and wait for
477 /// the next update).
478 pub endpoint: Option<CompactString>,
479}
480
481impl core::fmt::Debug for VoiceServerUpdate {
482 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
483 f.debug_struct("VoiceServerUpdate")
484 .field("token", &"<redacted>")
485 .field("guild_id", &self.guild_id)
486 .field("endpoint", &self.endpoint)
487 .finish()
488 }
489}
490
491/// The data of the [Voice Channel Effect Send](https://docs.discord.com/developers/events/gateway-events#voice-channel-effect-send)
492/// dispatch event: someone sent an emoji reaction or soundboard sound in the bot's voice
493/// channel.
494///
495/// discord.js counterpart: the `VoiceChannelEffectSend` event.
496#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
497#[non_exhaustive]
498pub struct VoiceChannelEffectSend {
499 /// The channel.
500 pub channel_id: ChannelId,
501 /// The guild.
502 pub guild_id: GuildId,
503 /// The user who sent the effect.
504 pub user_id: UserId,
505 /// The emoji (emoji reactions and soundboard sounds).
506 #[serde(default, skip_serializing_if = "Option::is_none")]
507 pub emoji: Option<PartialEmoji>,
508 /// The kind of animation.
509 #[serde(default, skip_serializing_if = "Option::is_none")]
510 pub animation_type: Option<VoiceChannelEffectAnimationType>,
511 /// The animation's ID.
512 #[serde(default, skip_serializing_if = "Option::is_none")]
513 pub animation_id: Option<u64>,
514 /// The soundboard sound (soundboard effects).
515 #[serde(default, skip_serializing_if = "Option::is_none")]
516 pub sound_id: Option<SoundboardSoundId>,
517 /// The sound's volume, from 0 to 1.
518 #[serde(default, skip_serializing_if = "Option::is_none")]
519 pub sound_volume: Option<f64>,
520}
521
522int_enum! {
523 /// The [animation of a voice channel effect](https://docs.discord.com/developers/events/gateway-events#voice-channel-effect-send-animation-types).
524 ///
525 /// discord.js counterpart: `VoiceChannelEffectSendAnimationType`.
526 pub enum VoiceChannelEffectAnimationType: u8 {
527 /// A fun animation, sent by a Nitro subscriber.
528 Premium = 0,
529 /// The standard animation.
530 Basic = 1,
531 }
532}