djs-model 0.1.0-alpha.2

Discord API v10 types — users, guilds, channels, messages, interactions and gateway events.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! Channel, thread, invite, stage, webhook and voice events.

use compact_str::CompactString;
use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Error as DeError};

use super::{guild_id_event, newtype_event};
use crate::{
    application::PartialApplication,
    channel::{Channel, ChannelType, ThreadMember},
    emoji::PartialEmoji,
    gateway::Presence,
    id::{ChannelId, GuildId, RoleId, SoundboardSoundId, UserId},
    invite::InviteTargetType,
    macros::int_enum,
    serde_util::extras::{self, Extras, Visit},
    stage_instance::StageInstance,
    user::User,
    util::Timestamp,
    voice::VoiceState,
};

newtype_event! {
    /// The data of the [Channel Create](https://docs.discord.com/developers/events/gateway-events#channel-create)
    /// dispatch event: a guild channel relevant to the bot was created.
    ///
    /// discord.js counterpart: the `ChannelCreate` event.
    ChannelCreate(Channel)
}

newtype_event! {
    /// The data of the [Channel Update](https://docs.discord.com/developers/events/gateway-events#channel-update)
    /// dispatch event (not sent for `last_message_id` changes).
    ///
    /// discord.js counterpart: the `ChannelUpdate` event.
    ChannelUpdate(Channel)
}

newtype_event! {
    /// The data of the [Channel Delete](https://docs.discord.com/developers/events/gateway-events#channel-delete)
    /// dispatch event.
    ///
    /// discord.js counterpart: the `ChannelDelete` event.
    ChannelDelete(Channel)
}

/// The data of the [Channel Info](https://docs.discord.com/developers/events/gateway-events#channel-info)
/// dispatch event: the ephemeral data of a guild's channels, sent in response to a
/// [`RequestChannelInfo`](crate::gateway::payload::RequestChannelInfo) command.
///
/// discord.js counterpart: `GatewayChannelInfoDispatchData` (discord-api-types).
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ChannelInfo {
    /// The guild.
    pub guild_id: GuildId,
    /// The channels' ephemeral data.
    #[serde(default)]
    pub channels: Vec<ChannelInfoChannel>,
}

/// The [ephemeral data of a channel](https://docs.discord.com/developers/events/gateway-events#channel-info-channel-info-channel-structure)
/// in a [`ChannelInfo`] event.
///
/// discord.js counterpart: `GatewayChannelInfoChannel` (discord-api-types).
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ChannelInfoChannel {
    /// The channel.
    pub id: ChannelId,
    /// The voice channel status.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status: Option<CompactString>,
    /// When the voice session started (sent as Unix seconds, D-075).
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "crate::serde_util::option_unix_secs"
    )]
    pub voice_start_time: Option<Timestamp>,
}

/// The data of the [Voice Channel Status Update](https://docs.discord.com/developers/events/gateway-events#voice-channel-status-update)
/// dispatch event.
///
/// discord.js counterpart: the `VoiceChannelStatusUpdate` event.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct VoiceChannelStatusUpdate {
    /// The channel.
    pub id: ChannelId,
    /// The guild.
    pub guild_id: GuildId,
    /// The new status (`None` when cleared).
    pub status: Option<CompactString>,
}

/// The data of the [Voice Channel Start Time Update](https://docs.discord.com/developers/events/gateway-events#voice-channel-start-time-update)
/// dispatch event.
///
/// discord.js counterpart: `GatewayVoiceChannelStartTimeUpdateDispatchData` (discord-api-types).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct VoiceChannelStartTimeUpdate {
    /// The channel.
    pub id: ChannelId,
    /// The guild.
    pub guild_id: GuildId,
    /// When the voice session started (sent as Unix seconds, D-075).
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "crate::serde_util::option_unix_secs"
    )]
    pub voice_start_time: Option<Timestamp>,
}

/// The data of the [Thread Create](https://docs.discord.com/developers/events/gateway-events#thread-create)
/// dispatch event: a thread was created, or the bot was added to a private thread (then the
/// thread includes the bot's [`ThreadMember`]).
///
/// Dereferences to the contained [`Channel`].
///
/// discord.js counterpart: the `ThreadCreate` event (`newlyCreated`).
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct ThreadCreate {
    /// The thread.
    pub thread: Channel,
    /// Whether the thread was just created (`false` when the bot was added to an existing
    /// thread).
    pub newly_created: bool,
}

impl core::ops::Deref for ThreadCreate {
    type Target = Channel;

    fn deref(&self) -> &Channel {
        &self.thread
    }
}

#[derive(Default)]
struct ThreadCreateExtra {
    newly_created: bool,
}

impl<'de> Extras<'de> for ThreadCreateExtra {
    fn visit<A: serde::de::MapAccess<'de>>(
        &mut self,
        key: &str,
        map: &mut A,
    ) -> Result<Visit, A::Error> {
        if key == "newly_created" {
            self.newly_created = map.next_value()?;
            Ok(Visit::Consumed)
        } else {
            Ok(Visit::Pass)
        }
    }
}

impl<'de> Deserialize<'de> for ThreadCreate {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let (thread, extra) = extras::deserialize::<Channel, ThreadCreateExtra, D>(deserializer)?;
        Ok(Self {
            thread: thread.ok_or_else(|| D::Error::custom("missing event data"))?,
            newly_created: extra.newly_created,
        })
    }
}

impl Serialize for ThreadCreate {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        #[derive(Serialize)]
        struct Flat<'a> {
            #[serde(flatten)]
            thread: &'a Channel,
            #[serde(skip_serializing_if = "core::ops::Not::not")]
            newly_created: bool,
        }

        Flat {
            thread: &self.thread,
            newly_created: self.newly_created,
        }
        .serialize(serializer)
    }
}

newtype_event! {
    /// The data of the [Thread Update](https://docs.discord.com/developers/events/gateway-events#thread-update)
    /// dispatch event (not sent for `last_message_id` changes).
    ///
    /// discord.js counterpart: the `ThreadUpdate` event.
    ThreadUpdate(Channel)
}

/// The data of the [Thread Delete](https://docs.discord.com/developers/events/gateway-events#thread-delete)
/// dispatch event: only the thread's IDs and type.
///
/// discord.js counterpart: the `ThreadDelete` event.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ThreadDelete {
    /// The thread.
    pub id: ChannelId,
    /// The guild.
    pub guild_id: GuildId,
    /// The parent channel.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent_id: Option<ChannelId>,
    /// The thread's type.
    #[serde(rename = "type")]
    pub kind: ChannelType,
}

/// The data of the [Thread List Sync](https://docs.discord.com/developers/events/gateway-events#thread-list-sync)
/// dispatch event: the active threads of channels the bot gained access to.
///
/// discord.js counterpart: the `ThreadListSync` event.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ThreadListSync {
    /// The guild.
    pub guild_id: GuildId,
    /// The parent channels whose threads are synced (may include channels without active
    /// threads); `None` means the whole guild.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub channel_ids: Option<Vec<ChannelId>>,
    /// The active threads the bot can access in those channels.
    #[serde(default)]
    pub threads: Vec<Channel>,
    /// The bot's thread members of the synced threads (which threads it was added to).
    #[serde(default)]
    pub members: Vec<ThreadMember>,
}

guild_id_event! {
    /// The data of the [Thread Member Update](https://docs.discord.com/developers/events/gateway-events#thread-member-update)
    /// dispatch event: the bot's thread member changed.
    ///
    /// discord.js counterpart: the `ThreadMemberUpdate` event.
    ThreadMemberUpdate {
        /// The bot's thread member.
        member: ThreadMember
    }
}

/// The data of the [Thread Members Update](https://docs.discord.com/developers/events/gateway-events#thread-members-update)
/// dispatch event: users were added to or removed from a thread (without the `GUILD_MEMBERS`
/// intent: only the bot).
///
/// discord.js counterpart: the `ThreadMembersUpdate` event.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ThreadMembersUpdate {
    /// The thread.
    pub id: ChannelId,
    /// The guild.
    pub guild_id: GuildId,
    /// The approximate number of members, capped at 50.
    pub member_count: u32,
    /// The added members.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub added_members: Vec<AddedThreadMember>,
    /// The removed users.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub removed_member_ids: Vec<UserId>,
}

/// A thread member added in a [`ThreadMembersUpdate`]: with its guild member (in
/// [`ThreadMember::member`]) and presence.
///
/// Dereferences to the contained [`ThreadMember`].
///
/// discord.js counterpart: an element of `GatewayThreadMembersUpdate`'s `added_members` (discord-api-types).
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AddedThreadMember {
    /// The thread member.
    pub member: ThreadMember,
    /// The member's presence.
    pub presence: Option<Presence>,
}

impl core::ops::Deref for AddedThreadMember {
    type Target = ThreadMember;

    fn deref(&self) -> &ThreadMember {
        &self.member
    }
}

#[derive(Default)]
struct PresenceExtra {
    presence: Option<Presence>,
}

impl<'de> Extras<'de> for PresenceExtra {
    fn visit<A: serde::de::MapAccess<'de>>(
        &mut self,
        key: &str,
        map: &mut A,
    ) -> Result<Visit, A::Error> {
        if key == "presence" {
            self.presence = map.next_value()?;
            Ok(Visit::Consumed)
        } else {
            Ok(Visit::Pass)
        }
    }
}

impl<'de> Deserialize<'de> for AddedThreadMember {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let (member, extra) = extras::deserialize::<ThreadMember, PresenceExtra, D>(deserializer)?;
        Ok(Self {
            member: member.ok_or_else(|| D::Error::custom("missing event data"))?,
            presence: extra.presence,
        })
    }
}

impl Serialize for AddedThreadMember {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        #[derive(Serialize)]
        struct Flat<'a> {
            #[serde(flatten)]
            member: &'a ThreadMember,
            presence: Option<&'a Presence>,
        }

        Flat {
            member: &self.member,
            presence: self.presence.as_ref(),
        }
        .serialize(serializer)
    }
}

/// The data of the [Channel Pins Update](https://docs.discord.com/developers/events/gateway-events#channel-pins-update)
/// dispatch event (not sent when a pinned message is deleted).
///
/// discord.js counterpart: the `ChannelPinsUpdate` event.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ChannelPinsUpdate {
    /// The guild.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub guild_id: Option<GuildId>,
    /// The channel.
    pub channel_id: ChannelId,
    /// When the most recent pin was made.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_pin_timestamp: Option<Timestamp>,
}

/// The data of the [Invite Create](https://docs.discord.com/developers/events/gateway-events#invite-create)
/// dispatch event (requires `MANAGE_CHANNELS` on the channel).
///
/// discord.js counterpart: the `InviteCreate` event.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct InviteCreate {
    /// The channel the invite is for.
    pub channel_id: ChannelId,
    /// The invite code.
    pub code: CompactString,
    /// When the invite was created.
    pub created_at: Timestamp,
    /// The invite's guild.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub guild_id: Option<GuildId>,
    /// Who created the invite.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub inviter: Option<Box<User>>,
    /// How long the invite is valid, in seconds (`0` = forever).
    pub max_age: u32,
    /// How often it can be used (`0` = unlimited).
    pub max_uses: u32,
    /// What a voice channel invite targets.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub target_type: Option<InviteTargetType>,
    /// The user whose stream a stream invite shows.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub target_user: Option<Box<User>>,
    /// The embedded application an application invite opens.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub target_application: Option<Box<PartialApplication>>,
    /// Whether users who join through it are kicked on disconnect unless given a role.
    #[serde(default)]
    pub temporary: bool,
    /// How often it was used (always `0` here).
    #[serde(default)]
    pub uses: u32,
    /// When the invite expires.
    #[serde(default)]
    pub expires_at: Option<Timestamp>,
    /// The roles given to users who accept the invite.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub role_ids: Vec<RoleId>,
}

/// The data of the [Invite Delete](https://docs.discord.com/developers/events/gateway-events#invite-delete)
/// dispatch event.
///
/// discord.js counterpart: the `InviteDelete` event.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct InviteDelete {
    /// The invite's channel.
    pub channel_id: ChannelId,
    /// The invite's guild.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub guild_id: Option<GuildId>,
    /// The invite code.
    pub code: CompactString,
}

newtype_event! {
    /// The data of the [Stage Instance Create](https://docs.discord.com/developers/events/gateway-events#stage-instance-create)
    /// dispatch event: a stage went live.
    ///
    /// discord.js counterpart: the `StageInstanceCreate` event.
    StageInstanceCreate(StageInstance)
}

newtype_event! {
    /// The data of the [Stage Instance Update](https://docs.discord.com/developers/events/gateway-events#stage-instance-update)
    /// dispatch event.
    ///
    /// discord.js counterpart: the `StageInstanceUpdate` event.
    StageInstanceUpdate(StageInstance)
}

newtype_event! {
    /// The data of the [Stage Instance Delete](https://docs.discord.com/developers/events/gateway-events#stage-instance-delete)
    /// dispatch event: a stage ended.
    ///
    /// discord.js counterpart: the `StageInstanceDelete` event.
    StageInstanceDelete(StageInstance)
}

/// The data of the [Webhooks Update](https://docs.discord.com/developers/events/gateway-events#webhooks-update)
/// dispatch event: a channel's webhooks changed.
///
/// discord.js counterpart: the `WebhooksUpdate` event.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WebhooksUpdate {
    /// The guild.
    pub guild_id: GuildId,
    /// The channel.
    pub channel_id: ChannelId,
}

newtype_event! {
    /// The data of the [Voice State Update](https://docs.discord.com/developers/events/gateway-events#voice-state-update)
    /// dispatch event: someone joined, left or moved between voice channels.
    ///
    /// discord.js counterpart: the `VoiceStateUpdate` event.
    VoiceStateUpdate(VoiceState)
}

/// The data of the [Voice Server Update](https://docs.discord.com/developers/events/gateway-events#voice-server-update)
/// dispatch event: where to connect for voice. `Debug` does not print the token.
///
/// discord.js counterpart: the voice server data used by `@discordjs/voice`.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct VoiceServerUpdate {
    /// The voice connection token (secret).
    pub token: CompactString,
    /// The guild.
    pub guild_id: GuildId,
    /// The voice server host; `None` while the server is reallocated (disconnect and wait for
    /// the next update).
    pub endpoint: Option<CompactString>,
}

impl core::fmt::Debug for VoiceServerUpdate {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("VoiceServerUpdate")
            .field("token", &"<redacted>")
            .field("guild_id", &self.guild_id)
            .field("endpoint", &self.endpoint)
            .finish()
    }
}

/// The data of the [Voice Channel Effect Send](https://docs.discord.com/developers/events/gateway-events#voice-channel-effect-send)
/// dispatch event: someone sent an emoji reaction or soundboard sound in the bot's voice
/// channel.
///
/// discord.js counterpart: the `VoiceChannelEffectSend` event.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct VoiceChannelEffectSend {
    /// The channel.
    pub channel_id: ChannelId,
    /// The guild.
    pub guild_id: GuildId,
    /// The user who sent the effect.
    pub user_id: UserId,
    /// The emoji (emoji reactions and soundboard sounds).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub emoji: Option<PartialEmoji>,
    /// The kind of animation.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub animation_type: Option<VoiceChannelEffectAnimationType>,
    /// The animation's ID.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub animation_id: Option<u64>,
    /// The soundboard sound (soundboard effects).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sound_id: Option<SoundboardSoundId>,
    /// The sound's volume, from 0 to 1.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sound_volume: Option<f64>,
}

int_enum! {
    /// The [animation of a voice channel effect](https://docs.discord.com/developers/events/gateway-events#voice-channel-effect-send-animation-types).
    ///
    /// discord.js counterpart: `VoiceChannelEffectSendAnimationType`.
    pub enum VoiceChannelEffectAnimationType: u8 {
        /// A fun animation, sent by a Nitro subscriber.
        Premium = 0,
        /// The standard animation.
        Basic = 1,
    }
}