djs_rs/event/payload.rs
1//! The payloads the client builds itself (D-225): events with the state before them (D-221,
2//! D-227), events derived from a list (D-223) and shard lifecycle events (D-226).
3//!
4//! Events with a previous state have two fields (D-228):
5//!
6//! - `old`: what the cache held before the event — `None` without a cache, with the resource
7//! disabled in [`CacheConfig`](djs_cache::CacheConfig), or for something it never received
8//! (the counterpart of a discord.js partial);
9//! - `new`: Discord's data for the event, as its [`djs-model`](djs_model) type. For a removal,
10//! this is Discord's notice (IDs) and `old` the removed object.
11//!
12//! Each of them dereferences to `new`, so its fields read as on an event without a wrapper:
13//!
14//! ```no_run
15//! # use djs_rs::prelude::*;
16//! # let client = Client::builder("token").build();
17//! client.on(Events::GuildMemberUpdate, |_ctx, update| async move {
18//! let old_nick = update.old.as_ref().and_then(|old| old.nick.clone());
19//! println!("{} was {old_nick:?}, is now {:?}", update.user.id, update.nick);
20//! Ok(())
21//! });
22//! ```
23
24use core::ops::Deref;
25use std::sync::Arc;
26
27use djs_cache::{CachedGuild, CachedMember, CachedMessage, CachedPresence};
28use djs_model::{
29 channel::Channel, emoji::Emoji, gateway::payload as gateway, guild::Role,
30 guild_scheduled_event::GuildScheduledEvent, id::GuildId, soundboard::SoundboardSound,
31 stage_instance::StageInstance, sticker::Sticker, user::CurrentUser, voice::VoiceState,
32};
33
34/// Defines a payload with the state before the event: `Name(Old => New)`, where `old` is an
35/// `Option<Arc<Old>>` and `new` a `New`.
36macro_rules! with_old {
37 ($(
38 $(#[$doc:meta])*
39 $name:ident($old:ty => $new:ident);
40 )*) => {$(
41 $(#[$doc])*
42 ///
43 #[doc = concat!(
44 "Dereferences to `new`, a [`", stringify!($new), "`](djs_model::gateway::payload::",
45 stringify!($new), ")."
46 )]
47 #[derive(Clone, Debug, PartialEq)]
48 #[non_exhaustive]
49 pub struct $name {
50 /// What the cache held before the event, if it held it.
51 pub old: Option<Arc<$old>>,
52 /// Discord's data for the event.
53 pub new: gateway::$new,
54 }
55
56 impl Deref for $name {
57 type Target = gateway::$new;
58
59 fn deref(&self) -> &gateway::$new {
60 &self.new
61 }
62 }
63 )*};
64}
65
66with_old! {
67 /// The payload of [`Events::ChannelUpdate`](crate::Events::ChannelUpdate).
68 ChannelUpdate(Channel => ChannelUpdate);
69 /// The payload of [`Events::ChannelDelete`](crate::Events::ChannelDelete).
70 ChannelDelete(Channel => ChannelDelete);
71 /// The payload of [`Events::ThreadUpdate`](crate::Events::ThreadUpdate).
72 ThreadUpdate(Channel => ThreadUpdate);
73 /// The payload of [`Events::ThreadDelete`](crate::Events::ThreadDelete): `new` only holds
74 /// the thread's IDs, `old` the cached thread.
75 ThreadDelete(Channel => ThreadDelete);
76 /// The payload of [`Events::GuildUpdate`](crate::Events::GuildUpdate).
77 GuildUpdate(CachedGuild => GuildUpdate);
78 /// The payload of [`Events::GuildDelete`](crate::Events::GuildDelete): `new` only holds the
79 /// guild's ID, `old` the cached guild.
80 GuildDelete(CachedGuild => GuildDelete);
81 /// The payload of [`Events::GuildUnavailable`](crate::Events::GuildUnavailable): `new`
82 /// only holds the guild's ID, `old` the cached guild (which the cache keeps while the guild
83 /// is unavailable).
84 GuildUnavailable(CachedGuild => GuildDelete);
85 /// The payload of [`Events::GuildMemberUpdate`](crate::Events::GuildMemberUpdate).
86 GuildMemberUpdate(CachedMember => GuildMemberUpdate);
87 /// The payload of [`Events::GuildMemberRemove`](crate::Events::GuildMemberRemove): `new`
88 /// holds the guild ID and the user, `old` the cached member.
89 GuildMemberRemove(CachedMember => GuildMemberRemove);
90 /// The payload of [`Events::GuildRoleUpdate`](crate::Events::GuildRoleUpdate).
91 GuildRoleUpdate(Role => GuildRoleUpdate);
92 /// The payload of [`Events::GuildRoleDelete`](crate::Events::GuildRoleDelete): `new` only
93 /// holds the IDs, `old` the cached role.
94 GuildRoleDelete(Role => GuildRoleDelete);
95 /// The payload of
96 /// [`Events::GuildScheduledEventUpdate`](crate::Events::GuildScheduledEventUpdate).
97 GuildScheduledEventUpdate(GuildScheduledEvent => GuildScheduledEventUpdate);
98 /// The payload of
99 /// [`Events::GuildScheduledEventDelete`](crate::Events::GuildScheduledEventDelete).
100 GuildScheduledEventDelete(GuildScheduledEvent => GuildScheduledEventDelete);
101 /// The payload of
102 /// [`Events::GuildSoundboardSoundUpdate`](crate::Events::GuildSoundboardSoundUpdate).
103 GuildSoundboardSoundUpdate(SoundboardSound => GuildSoundboardSoundUpdate);
104 /// The payload of
105 /// [`Events::GuildSoundboardSoundDelete`](crate::Events::GuildSoundboardSoundDelete): `new`
106 /// only holds the IDs, `old` the cached sound.
107 GuildSoundboardSoundDelete(SoundboardSound => GuildSoundboardSoundDelete);
108 /// The payload of [`Events::StageInstanceUpdate`](crate::Events::StageInstanceUpdate).
109 StageInstanceUpdate(StageInstance => StageInstanceUpdate);
110 /// The payload of [`Events::StageInstanceDelete`](crate::Events::StageInstanceDelete).
111 StageInstanceDelete(StageInstance => StageInstanceDelete);
112 /// The payload of [`Events::MessageUpdate`](crate::Events::MessageUpdate).
113 MessageUpdate(CachedMessage => MessageUpdate);
114 /// The payload of [`Events::MessageDelete`](crate::Events::MessageDelete): `new` only holds
115 /// the IDs, `old` the cached message.
116 MessageDelete(CachedMessage => MessageDelete);
117 /// The payload of [`Events::PresenceUpdate`](crate::Events::PresenceUpdate).
118 PresenceUpdate(CachedPresence => PresenceUpdate);
119 /// The payload of [`Events::UserUpdate`](crate::Events::UserUpdate).
120 UserUpdate(CurrentUser => UserUpdate);
121 /// The payload of [`Events::VoiceStateUpdate`](crate::Events::VoiceStateUpdate).
122 VoiceStateUpdate(VoiceState => VoiceStateUpdate);
123}
124
125/// The payload of [`Events::MessageBulkDelete`](crate::Events::MessageBulkDelete): `new` holds
126/// the IDs, `old` the messages the cache held (empty without a message cache).
127///
128/// Dereferences to `new`, a
129/// [`MessageDeleteBulk`](djs_model::gateway::payload::MessageDeleteBulk).
130///
131/// discord.js counterpart: the arguments of `Events.MessageBulkDelete` (`messages`, `channel`).
132#[derive(Clone, Debug, PartialEq)]
133#[non_exhaustive]
134pub struct MessageBulkDelete {
135 /// The deleted messages the cache held, in no particular order.
136 pub old: Vec<Arc<CachedMessage>>,
137 /// Discord's data for the event.
138 pub new: gateway::MessageDeleteBulk,
139}
140
141impl Deref for MessageBulkDelete {
142 type Target = gateway::MessageDeleteBulk;
143
144 fn deref(&self) -> &gateway::MessageDeleteBulk {
145 &self.new
146 }
147}
148
149/// Defines the three per-item payloads derived from a guild's list of `$item`s (D-223).
150macro_rules! derived {
151 ($item:ident: $field:ident, $create:ident, $update:ident, $delete:ident, $list:literal) => {
152 #[doc = concat!(
153 "The payload of [`Events::", stringify!($create), "`](crate::Events::",
154 stringify!($create), "): a new ", $list, ".\n\nDereferences to the [`",
155 stringify!($item), "`]."
156 )]
157 #[derive(Clone, Debug, PartialEq)]
158 #[non_exhaustive]
159 pub struct $create {
160 /// The guild.
161 pub guild_id: GuildId,
162 #[doc = concat!("The new ", $list, ".")]
163 pub $field: $item,
164 }
165
166 impl Deref for $create {
167 type Target = $item;
168
169 fn deref(&self) -> &$item {
170 &self.$field
171 }
172 }
173
174 #[doc = concat!(
175 "The payload of [`Events::", stringify!($update), "`](crate::Events::",
176 stringify!($update), "): a changed ", $list, ", before and after.\n\n",
177 "Dereferences to `new`."
178 )]
179 #[derive(Clone, Debug, PartialEq)]
180 #[non_exhaustive]
181 pub struct $update {
182 /// The guild.
183 pub guild_id: GuildId,
184 #[doc = concat!("The ", $list, " as the cache held it.")]
185 pub old: Arc<$item>,
186 #[doc = concat!("The ", $list, " now.")]
187 pub new: $item,
188 }
189
190 impl Deref for $update {
191 type Target = $item;
192
193 fn deref(&self) -> &$item {
194 &self.new
195 }
196 }
197
198 #[doc = concat!(
199 "The payload of [`Events::", stringify!($delete), "`](crate::Events::",
200 stringify!($delete), "): a deleted ", $list, ", as the cache held it.\n\n",
201 "Dereferences to the [`", stringify!($item), "`]."
202 )]
203 #[derive(Clone, Debug, PartialEq)]
204 #[non_exhaustive]
205 pub struct $delete {
206 /// The guild.
207 pub guild_id: GuildId,
208 #[doc = concat!("The deleted ", $list, ".")]
209 pub $field: Arc<$item>,
210 }
211
212 impl Deref for $delete {
213 type Target = $item;
214
215 fn deref(&self) -> &$item {
216 &self.$field
217 }
218 }
219 };
220}
221
222derived!(Emoji: emoji, GuildEmojiCreate, GuildEmojiUpdate, GuildEmojiDelete, "emoji");
223derived!(Sticker: sticker, GuildStickerCreate, GuildStickerUpdate, GuildStickerDelete, "sticker");
224
225/// The payload of [`Events::ShardResume`](crate::Events::ShardResume).
226///
227/// discord.js counterpart: the arguments of `Events.ShardResume`.
228#[derive(Clone, Copy, Debug, PartialEq, Eq)]
229#[non_exhaustive]
230pub struct ShardResume {
231 /// How many events Discord replayed between the reconnect and `RESUMED`. They were
232 /// emitted as usual before this event.
233 pub replayed: u64,
234}
235
236/// The payload of [`Events::ShardReconnecting`](crate::Events::ShardReconnecting). The shard
237/// is [`Context::shard_id`](crate::Context::shard_id).
238///
239/// discord.js counterpart: the arguments of `Events.ShardReconnecting`.
240#[derive(Clone, Copy, Debug, PartialEq, Eq)]
241#[non_exhaustive]
242pub struct ShardReconnecting;
243
244/// The payload of [`Events::Invalidated`](crate::Events::Invalidated). The shard is
245/// [`Context::shard_id`](crate::Context::shard_id).
246///
247/// discord.js counterpart: `Events.Invalidated`.
248#[derive(Clone, Copy, Debug, PartialEq, Eq)]
249#[non_exhaustive]
250pub struct Invalidated;