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
use twilight_model::{
channel::{
embed::{Embed, EmbedField},
message::{
sticker::{MessageSticker, StickerFormatType},
MessageActivityType, MessageFlags, MessageType,
},
Attachment, Message,
},
gateway::payload::incoming::MessageUpdate,
id::{
marker::{
ApplicationMarker, AttachmentMarker, ChannelMarker, GenericMarker, GuildMarker,
MessageMarker, StickerMarker, UserMarker, WebhookMarker,
},
Id,
},
util::{ImageHash, Timestamp},
};
use crate::unique_id;
#[derive(Clone, Debug)]
pub struct CachedEmbedField {
pub embed_id: Id<GenericMarker>,
pub inline: bool,
pub name: String,
pub value: String,
}
impl CachedEmbedField {
#[allow(clippy::missing_const_for_fn)]
#[must_use]
pub fn from_embed_field(embed_field: EmbedField, embed_id: Id<GenericMarker>) -> Self {
Self {
embed_id,
inline: embed_field.inline,
name: embed_field.name,
value: embed_field.value,
}
}
}
#[derive(Clone, Debug)]
pub struct CachedEmbed {
pub id: Id<GenericMarker>,
pub message_id: Id<MessageMarker>,
pub author_icon_url: Option<String>,
pub author_name: Option<String>,
pub author_proxy_icon_url: Option<String>,
pub author_url: Option<String>,
pub color: Option<u32>,
pub description: Option<String>,
pub footer_icon_url: Option<String>,
pub footer_proxy_icon_url: Option<String>,
pub footer_text: Option<String>,
pub image_height: Option<u64>,
pub image_proxy_url: Option<String>,
pub image_url: Option<String>,
pub image_width: Option<u64>,
pub kind: String,
pub provider_name: Option<String>,
pub provider_url: Option<String>,
pub thumbnail_height: Option<u64>,
pub thumbnail_proxy_url: Option<String>,
pub thumbnail_url: Option<String>,
pub thumbnail_width: Option<u64>,
pub timestamp: Option<Timestamp>,
pub title: Option<String>,
pub url: Option<String>,
pub video_height: Option<u64>,
pub video_proxy_url: Option<String>,
pub video_url: Option<String>,
pub video_width: Option<u64>,
}
impl CachedEmbed {
#[allow(clippy::cast_sign_loss, clippy::as_conversions)]
#[must_use]
pub fn from_embed(embed: Embed, message_id: Id<MessageMarker>) -> Self {
Self {
id: Id::new(unique_id() as u64),
message_id,
author_icon_url: embed
.author
.as_ref()
.and_then(|author| author.icon_url.clone()),
author_name: embed.author.as_ref().map(|author| author.name.clone()),
author_proxy_icon_url: embed
.author
.as_ref()
.and_then(|author| author.icon_url.clone()),
author_url: embed.author.as_ref().and_then(|author| author.url.clone()),
color: embed.color,
description: embed.description,
footer_icon_url: embed
.footer
.as_ref()
.and_then(|footer| footer.icon_url.clone()),
footer_proxy_icon_url: embed
.footer
.as_ref()
.and_then(|footer| footer.proxy_icon_url.clone()),
footer_text: embed.footer.as_ref().map(|footer| footer.text.clone()),
image_height: embed.image.as_ref().and_then(|image| image.height),
image_proxy_url: embed
.image
.as_ref()
.and_then(|image| image.proxy_url.clone()),
image_url: embed.image.as_ref().map(|image| image.url.clone()),
image_width: embed.image.as_ref().and_then(|image| image.width),
kind: embed.kind,
provider_name: embed
.provider
.as_ref()
.and_then(|provider| provider.name.clone()),
provider_url: embed
.provider
.as_ref()
.and_then(|provider| provider.url.clone()),
thumbnail_height: embed
.thumbnail
.as_ref()
.and_then(|thumbnail| thumbnail.height),
thumbnail_proxy_url: embed
.thumbnail
.as_ref()
.and_then(|thumbnail| thumbnail.proxy_url.clone()),
thumbnail_url: embed
.thumbnail
.as_ref()
.map(|thumbnail| thumbnail.url.clone()),
thumbnail_width: embed
.thumbnail
.as_ref()
.and_then(|thumbnail| thumbnail.width),
timestamp: embed.timestamp,
title: embed.title,
url: embed.url,
video_height: embed.video.as_ref().and_then(|video| video.height),
video_proxy_url: embed
.video
.as_ref()
.and_then(|video| video.proxy_url.clone()),
video_url: embed.video.as_ref().and_then(|video| video.url.clone()),
video_width: embed.video.as_ref().and_then(|video| video.width),
}
}
}
#[derive(Clone, Debug)]
pub struct CachedAttachment {
pub message_id: Id<MessageMarker>,
pub content_type: Option<String>,
pub ephemeral: bool,
pub filename: String,
pub description: Option<String>,
pub height: Option<u64>,
pub id: Id<AttachmentMarker>,
pub proxy_url: String,
pub size: u64,
pub url: String,
pub width: Option<u64>,
}
impl CachedAttachment {
#[allow(clippy::missing_const_for_fn)]
#[must_use]
pub fn from_attachment(attachment: Attachment, message_id: Id<MessageMarker>) -> Self {
Self {
message_id,
content_type: attachment.content_type,
ephemeral: attachment.ephemeral,
filename: attachment.filename,
description: attachment.description,
height: attachment.height,
id: attachment.id,
proxy_url: attachment.proxy_url,
size: attachment.size,
url: attachment.url,
width: attachment.width,
}
}
}
#[derive(Clone, Debug)]
pub struct CachedMessageSticker {
pub message_id: Id<MessageMarker>,
pub format_type: StickerFormatType,
pub id: Id<StickerMarker>,
pub name: String,
}
impl CachedMessageSticker {
#[allow(clippy::missing_const_for_fn)]
#[must_use]
pub fn from_sticker(sticker: MessageSticker, message_id: Id<MessageMarker>) -> Self {
Self {
message_id,
format_type: sticker.format_type,
id: sticker.id,
name: sticker.name,
}
}
}
#[derive(Clone, Debug)]
pub struct CachedMessage {
pub activity_type: Option<MessageActivityType>,
pub activity_party_id: Option<String>,
pub application_cover_image: Option<ImageHash>,
pub application_description: Option<String>,
pub application_icon: Option<ImageHash>,
pub application_id: Option<Id<ApplicationMarker>>,
pub application_name: Option<String>,
pub interaction_application_id: Option<Id<ApplicationMarker>>,
pub author: Id<UserMarker>,
pub channel_id: Id<ChannelMarker>,
pub content: String,
pub edited_timestamp: Option<Timestamp>,
pub flags: Option<MessageFlags>,
pub guild_id: Option<Id<GuildMarker>>,
pub id: Id<MessageMarker>,
pub kind: MessageType,
pub mention_everyone: bool,
pub pinned: bool,
pub reference_channel_id: Option<Id<ChannelMarker>>,
pub reference_guild_id: Option<Id<GuildMarker>>,
pub reference_message_id: Option<Id<MessageMarker>>,
pub reference_fail_if_not_exists: Option<bool>,
pub referenced_message: Option<Id<MessageMarker>>,
pub timestamp: Timestamp,
pub thread: Option<Id<ChannelMarker>>,
pub tts: bool,
pub webhook_id: Option<Id<WebhookMarker>>,
}
impl CachedMessage {
pub fn update(&mut self, message: &MessageUpdate) {
if let Some(content) = &message.content {
self.content.clone_from(content);
}
if message.edited_timestamp.is_some() {
self.edited_timestamp = message.edited_timestamp;
}
if let Some(mentions) = message.mention_everyone {
self.mention_everyone = mentions;
}
if let Some(pinned) = message.pinned {
self.pinned = pinned;
}
}
}
impl From<&Message> for CachedMessage {
fn from(message: &Message) -> Self {
Self {
activity_type: message.activity.as_ref().map(|activity| activity.kind),
activity_party_id: message
.activity
.as_ref()
.and_then(|activity| activity.party_id.clone()),
application_cover_image: message
.application
.as_ref()
.and_then(|application| application.cover_image),
application_description: message
.application
.as_ref()
.map(|application| application.description.clone()),
application_icon: message
.application
.as_ref()
.and_then(|application| application.icon),
application_id: message
.application
.as_ref()
.map(|application| application.id),
application_name: message
.application
.as_ref()
.map(|application| application.name.clone()),
interaction_application_id: message.application_id,
author: message.author.id,
channel_id: message.channel_id,
content: message.content.clone(),
edited_timestamp: message.edited_timestamp,
guild_id: message.guild_id,
id: message.id,
kind: message.kind,
mention_everyone: message.mention_everyone,
pinned: message.pinned,
reference_channel_id: message
.reference
.as_ref()
.and_then(|reference| reference.channel_id),
reference_guild_id: message
.reference
.as_ref()
.and_then(|reference| reference.guild_id),
reference_message_id: message
.reference
.as_ref()
.and_then(|reference| reference.message_id),
reference_fail_if_not_exists: message
.reference
.as_ref()
.and_then(|reference| reference.fail_if_not_exists),
referenced_message: message
.referenced_message
.as_ref()
.map(|reference| reference.id),
timestamp: message.timestamp,
thread: message.thread.as_ref().map(|thread| thread.id),
tts: message.tts,
flags: message.flags,
webhook_id: message.webhook_id,
}
}
}