Skip to main content

djs_model/gateway/payload/incoming/
message.rs

1//! Message, reaction, poll, interaction, presence, user and monetization events.
2
3use compact_str::CompactString;
4use serde::{Deserialize, Serialize};
5
6use super::newtype_event;
7use crate::{
8    emoji::PartialEmoji,
9    entitlement::Entitlement,
10    gateway::Presence,
11    guild::Member,
12    id::{ChannelId, GuildId, MessageId, UserId},
13    interaction::Interaction,
14    message::{Message, ReactionType},
15    subscription::Subscription,
16    user::CurrentUser,
17    util::Timestamp,
18};
19
20newtype_event! {
21    /// The data of the [Message Create](https://docs.discord.com/developers/events/gateway-events#message-create)
22    /// dispatch event: a new message was sent.
23    ///
24    /// discord.js counterpart: the `MessageCreate` event.
25    MessageCreate(Message)
26}
27
28newtype_event! {
29    /// The data of the [Message Update](https://docs.discord.com/developers/events/gateway-events#message-update)
30    /// dispatch event: a message was edited (the full new message; `tts` is always `false`).
31    ///
32    /// discord.js counterpart: the `MessageUpdate` event.
33    MessageUpdate(Message)
34}
35
36/// The data of the [Message Delete](https://docs.discord.com/developers/events/gateway-events#message-delete)
37/// dispatch event.
38///
39/// discord.js counterpart: the `MessageDelete` event.
40#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
41#[non_exhaustive]
42pub struct MessageDelete {
43    /// The deleted message.
44    pub id: MessageId,
45    /// The message's channel.
46    pub channel_id: ChannelId,
47    /// The message's guild.
48    #[serde(default, skip_serializing_if = "Option::is_none")]
49    pub guild_id: Option<GuildId>,
50}
51
52/// The data of the [Message Delete Bulk](https://docs.discord.com/developers/events/gateway-events#message-delete-bulk)
53/// dispatch event.
54///
55/// discord.js counterpart: the `MessageBulkDelete` event.
56#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
57#[non_exhaustive]
58pub struct MessageDeleteBulk {
59    /// The deleted messages.
60    pub ids: Vec<MessageId>,
61    /// The messages' channel.
62    pub channel_id: ChannelId,
63    /// The messages' guild.
64    #[serde(default, skip_serializing_if = "Option::is_none")]
65    pub guild_id: Option<GuildId>,
66}
67
68/// The data of the [Message Reaction Add](https://docs.discord.com/developers/events/gateway-events#message-reaction-add)
69/// dispatch event.
70///
71/// discord.js counterpart: the `MessageReactionAdd` event.
72#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
73#[non_exhaustive]
74pub struct MessageReactionAdd {
75    /// The user who reacted.
76    pub user_id: UserId,
77    /// The message's channel.
78    pub channel_id: ChannelId,
79    /// The message.
80    pub message_id: MessageId,
81    /// The message's guild.
82    #[serde(default, skip_serializing_if = "Option::is_none")]
83    pub guild_id: Option<GuildId>,
84    /// The member who reacted (guild reactions).
85    #[serde(default, skip_serializing_if = "Option::is_none")]
86    pub member: Option<Box<Member>>,
87    /// The emoji.
88    pub emoji: PartialEmoji,
89    /// The author of the message.
90    #[serde(default, skip_serializing_if = "Option::is_none")]
91    pub message_author_id: Option<UserId>,
92    /// Whether this is a super reaction.
93    #[serde(default)]
94    pub burst: bool,
95    /// The super reaction's animation colors (`#rrggbb`).
96    #[serde(default, skip_serializing_if = "Vec::is_empty")]
97    pub burst_colors: Vec<CompactString>,
98    /// The kind of reaction.
99    #[serde(default, rename = "type")]
100    pub kind: ReactionType,
101}
102
103/// The data of the [Message Reaction Remove](https://docs.discord.com/developers/events/gateway-events#message-reaction-remove)
104/// dispatch event.
105///
106/// discord.js counterpart: the `MessageReactionRemove` event.
107#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
108#[non_exhaustive]
109pub struct MessageReactionRemove {
110    /// The user whose reaction was removed.
111    pub user_id: UserId,
112    /// The message's channel.
113    pub channel_id: ChannelId,
114    /// The message.
115    pub message_id: MessageId,
116    /// The message's guild.
117    #[serde(default, skip_serializing_if = "Option::is_none")]
118    pub guild_id: Option<GuildId>,
119    /// The emoji.
120    pub emoji: PartialEmoji,
121    /// Whether this was a super reaction.
122    #[serde(default)]
123    pub burst: bool,
124    /// The kind of reaction.
125    #[serde(default, rename = "type")]
126    pub kind: ReactionType,
127}
128
129/// The data of the [Message Reaction Remove All](https://docs.discord.com/developers/events/gateway-events#message-reaction-remove-all)
130/// dispatch event.
131///
132/// discord.js counterpart: the `MessageReactionRemoveAll` event.
133#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
134#[non_exhaustive]
135pub struct MessageReactionRemoveAll {
136    /// The message's channel.
137    pub channel_id: ChannelId,
138    /// The message.
139    pub message_id: MessageId,
140    /// The message's guild.
141    #[serde(default, skip_serializing_if = "Option::is_none")]
142    pub guild_id: Option<GuildId>,
143}
144
145/// The data of the [Message Reaction Remove Emoji](https://docs.discord.com/developers/events/gateway-events#message-reaction-remove-emoji)
146/// dispatch event: all reactions with one emoji were removed.
147///
148/// discord.js counterpart: the `MessageReactionRemoveEmoji` event.
149#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
150#[non_exhaustive]
151pub struct MessageReactionRemoveEmoji {
152    /// The message's channel.
153    pub channel_id: ChannelId,
154    /// The message's guild.
155    #[serde(default, skip_serializing_if = "Option::is_none")]
156    pub guild_id: Option<GuildId>,
157    /// The message.
158    pub message_id: MessageId,
159    /// The removed emoji.
160    pub emoji: PartialEmoji,
161}
162
163/// The data of the [Message Poll Vote Add](https://docs.discord.com/developers/events/gateway-events#message-poll-vote-add)
164/// dispatch event (one event per answer for multiselect polls).
165///
166/// discord.js counterpart: the `MessagePollVoteAdd` event.
167#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
168#[non_exhaustive]
169pub struct MessagePollVoteAdd {
170    /// The voter.
171    pub user_id: UserId,
172    /// The poll message's channel.
173    pub channel_id: ChannelId,
174    /// The poll message.
175    pub message_id: MessageId,
176    /// The poll message's guild.
177    #[serde(default, skip_serializing_if = "Option::is_none")]
178    pub guild_id: Option<GuildId>,
179    /// The answer voted for.
180    pub answer_id: u32,
181}
182
183/// The data of the [Message Poll Vote Remove](https://docs.discord.com/developers/events/gateway-events#message-poll-vote-remove)
184/// dispatch event (one event per answer for multiselect polls).
185///
186/// discord.js counterpart: the `MessagePollVoteRemove` event.
187#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
188#[non_exhaustive]
189pub struct MessagePollVoteRemove {
190    /// The user who removed the vote.
191    pub user_id: UserId,
192    /// The poll message's channel.
193    pub channel_id: ChannelId,
194    /// The poll message.
195    pub message_id: MessageId,
196    /// The poll message's guild.
197    #[serde(default, skip_serializing_if = "Option::is_none")]
198    pub guild_id: Option<GuildId>,
199    /// The answer the vote was removed from.
200    pub answer_id: u32,
201}
202
203/// The data of the [Typing Start](https://docs.discord.com/developers/events/gateway-events#typing-start)
204/// dispatch event.
205///
206/// discord.js counterpart: the `TypingStart` event.
207#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
208#[non_exhaustive]
209pub struct TypingStart {
210    /// The channel.
211    pub channel_id: ChannelId,
212    /// The guild.
213    #[serde(default, skip_serializing_if = "Option::is_none")]
214    pub guild_id: Option<GuildId>,
215    /// The typing user.
216    pub user_id: UserId,
217    /// When the user started typing (sent as Unix seconds, D-075).
218    #[serde(with = "crate::serde_util::unix_secs")]
219    pub timestamp: Timestamp,
220    /// The typing member (guild channels).
221    #[serde(default, skip_serializing_if = "Option::is_none")]
222    pub member: Option<Box<Member>>,
223}
224
225newtype_event! {
226    /// The data of the [Interaction Create](https://docs.discord.com/developers/events/gateway-events#interaction-create)
227    /// dispatch event: a user used a command, component or modal.
228    ///
229    /// discord.js counterpart: the `InteractionCreate` event.
230    InteractionCreate(Interaction)
231}
232
233newtype_event! {
234    /// The data of the [Presence Update](https://docs.discord.com/developers/events/gateway-events#presence-update)
235    /// dispatch event (requires the `GUILD_PRESENCES` intent).
236    ///
237    /// discord.js counterpart: the `PresenceUpdate` event.
238    PresenceUpdate(Presence)
239}
240
241newtype_event! {
242    /// The data of the [User Update](https://docs.discord.com/developers/events/gateway-events#user-update)
243    /// dispatch event: the bot's own user changed.
244    ///
245    /// discord.js counterpart: the `UserUpdate` event (for the client user).
246    UserUpdate(CurrentUser)
247}
248
249newtype_event! {
250    /// The data of the [Entitlement Create](https://docs.discord.com/developers/events/gateway-events#entitlement-create)
251    /// dispatch event.
252    ///
253    /// discord.js counterpart: the `EntitlementCreate` event.
254    EntitlementCreate(Entitlement)
255}
256
257newtype_event! {
258    /// The data of the [Entitlement Update](https://docs.discord.com/developers/events/gateway-events#entitlement-update)
259    /// dispatch event (for subscriptions: when they end, with `ends_at`).
260    ///
261    /// discord.js counterpart: the `EntitlementUpdate` event.
262    EntitlementUpdate(Entitlement)
263}
264
265newtype_event! {
266    /// The data of the [Entitlement Delete](https://docs.discord.com/developers/events/gateway-events#entitlement-delete)
267    /// dispatch event (refunds and removals; entitlements are not deleted when they expire).
268    ///
269    /// discord.js counterpart: the `EntitlementDelete` event.
270    EntitlementDelete(Entitlement)
271}
272
273newtype_event! {
274    /// The data of the [Subscription Create](https://docs.discord.com/developers/events/gateway-events#subscription-create)
275    /// dispatch event (use entitlements, not the status, to grant perks).
276    ///
277    /// discord.js counterpart: the `SubscriptionCreate` event.
278    SubscriptionCreate(Subscription)
279}
280
281newtype_event! {
282    /// The data of the [Subscription Update](https://docs.discord.com/developers/events/gateway-events#subscription-update)
283    /// dispatch event.
284    ///
285    /// discord.js counterpart: the `SubscriptionUpdate` event.
286    SubscriptionUpdate(Subscription)
287}
288
289newtype_event! {
290    /// The data of the [Subscription Delete](https://docs.discord.com/developers/events/gateway-events#subscription-delete)
291    /// dispatch event.
292    ///
293    /// discord.js counterpart: the `SubscriptionDelete` event.
294    SubscriptionDelete(Subscription)
295}