|
3 | 3 | //! Every event includes the gateway intent required to receive it, as well as a link to the |
4 | 4 | //! Discord documentation for the event. |
5 | 5 |
|
6 | | -// Just for MessageUpdateEvent (for some reason the #[allow] doesn't work when placed directly) |
7 | | -#![allow(clippy::option_option)] |
8 | | - |
9 | | -use nonmax::NonMaxU64; |
10 | 6 | use serde::de::Error as DeError; |
11 | 7 | use serde::Serialize; |
12 | 8 | use strum::{EnumCount, IntoStaticStr, VariantNames}; |
@@ -467,153 +463,15 @@ pub struct MessageDeleteEvent { |
467 | 463 | pub message_id: MessageId, |
468 | 464 | } |
469 | 465 |
|
470 | | -// Any value that is present is considered Some value, including null. |
471 | | -// Taken from https://github.com/serde-rs/serde/issues/984#issuecomment-314143738 |
472 | | -fn deserialize_some<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error> |
473 | | -where |
474 | | - T: Deserialize<'de>, |
475 | | - D: Deserializer<'de>, |
476 | | -{ |
477 | | - Deserialize::deserialize(deserializer).map(Some) |
478 | | -} |
479 | | - |
480 | 466 | /// Requires [`GatewayIntents::GUILD_MESSAGES`]. |
481 | 467 | /// |
482 | | -/// Contains identical fields to [`Message`], except everything but `id` and `channel_id` are |
483 | | -/// optional. Even fields that cannot change in a message update event are included, because Discord |
484 | | -/// may include them anyways, independent from whether they have actually changed (like |
485 | | -/// [`Self::guild_id`]) |
486 | | -/// |
487 | 468 | /// [Discord docs](https://discord.com/developers/docs/topics/gateway-events#message-update). |
488 | 469 | #[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))] |
489 | 470 | #[derive(Clone, Debug, Deserialize, Serialize)] |
490 | 471 | #[non_exhaustive] |
491 | 472 | pub struct MessageUpdateEvent { |
492 | | - pub id: MessageId, |
493 | | - pub channel_id: ChannelId, |
494 | | - pub author: Option<User>, |
495 | | - pub content: Option<FixedString<u16>>, |
496 | | - pub timestamp: Option<Timestamp>, |
497 | | - pub edited_timestamp: Option<Timestamp>, |
498 | | - pub tts: Option<bool>, |
499 | | - pub mention_everyone: Option<bool>, |
500 | | - pub mentions: Option<FixedArray<User>>, |
501 | | - pub mention_roles: Option<FixedArray<RoleId>>, |
502 | | - pub mention_channels: Option<FixedArray<ChannelMention>>, |
503 | | - pub attachments: Option<FixedArray<Attachment>>, |
504 | | - pub embeds: Option<FixedArray<Embed>>, |
505 | | - pub reactions: Option<FixedArray<MessageReaction>>, |
506 | | - pub pinned: Option<bool>, |
507 | | - #[serde(default, deserialize_with = "deserialize_some")] |
508 | | - pub webhook_id: Option<Option<WebhookId>>, |
509 | | - #[serde(rename = "type")] |
510 | | - pub kind: Option<MessageType>, |
511 | | - #[serde(default, deserialize_with = "deserialize_some")] |
512 | | - pub activity: Option<Option<MessageActivity>>, |
513 | | - #[serde(default, deserialize_with = "deserialize_some")] |
514 | | - pub application: Option<Option<MessageApplication>>, |
515 | | - #[serde(default, deserialize_with = "deserialize_some")] |
516 | | - pub application_id: Option<Option<ApplicationId>>, |
517 | | - pub message_reference: Option<Option<MessageReference>>, |
518 | | - #[serde(default, deserialize_with = "deserialize_some")] |
519 | | - pub flags: Option<Option<MessageFlags>>, |
520 | | - #[serde(default, deserialize_with = "deserialize_some")] |
521 | | - pub referenced_message: Option<Option<Box<Message>>>, |
522 | | - #[serde(default, deserialize_with = "deserialize_some")] |
523 | | - #[cfg(not(feature = "unstable"))] |
524 | | - pub interaction: Option<Option<Box<MessageInteraction>>>, |
525 | | - pub interaction_metadata: Option<Option<Box<MessageInteractionMetadata>>>, |
526 | | - #[serde(default, deserialize_with = "deserialize_some")] |
527 | | - pub thread: Option<Option<Box<GuildChannel>>>, |
528 | | - pub components: Option<FixedArray<ActionRow>>, |
529 | | - pub sticker_items: Option<FixedArray<StickerItem>>, |
530 | | - pub position: Option<Option<NonMaxU64>>, |
531 | | - pub role_subscription_data: Option<Option<RoleSubscriptionData>>, |
532 | | - pub guild_id: Option<GuildId>, |
533 | | - pub member: Option<Option<Box<PartialMember>>>, |
534 | | -} |
535 | | - |
536 | | -impl MessageUpdateEvent { |
537 | | - #[expect(clippy::clone_on_copy)] // For consistency between fields |
538 | | - #[rustfmt::skip] |
539 | | - /// Writes the updated data in this message update event into the given [`Message`]. |
540 | | - pub fn apply_to_message(&self, message: &mut Message) { |
541 | | - // Destructure, so we get an `unused` warning when we forget to process one of the fields |
542 | | - // in this method |
543 | | - let Self { |
544 | | - id, |
545 | | - channel_id, |
546 | | - author, |
547 | | - content, |
548 | | - timestamp, |
549 | | - edited_timestamp, |
550 | | - tts, |
551 | | - mention_everyone, |
552 | | - mentions, |
553 | | - mention_roles, |
554 | | - mention_channels, |
555 | | - attachments, |
556 | | - embeds, |
557 | | - reactions, |
558 | | - pinned, |
559 | | - webhook_id, |
560 | | - kind, |
561 | | - activity, |
562 | | - application, |
563 | | - application_id, |
564 | | - message_reference, |
565 | | - flags, |
566 | | - referenced_message, |
567 | | - #[cfg(not(feature = "unstable"))] |
568 | | - interaction, |
569 | | - interaction_metadata, |
570 | | - thread, |
571 | | - components, |
572 | | - sticker_items, |
573 | | - position, |
574 | | - role_subscription_data, |
575 | | - guild_id, |
576 | | - member, |
577 | | - } = self; |
578 | | - |
579 | | - // Discord won't send a MessageUpdateEvent with a different MessageId and ChannelId than we |
580 | | - // already have. But let's set the fields anyways, in case the user calls this method with |
581 | | - // a self-constructed MessageUpdateEvent that does change these fields. |
582 | | - message.id = *id; |
583 | | - message.channel_id = *channel_id; |
584 | | - |
585 | | - if let Some(x) = author { message.author = x.clone() } |
586 | | - if let Some(x) = content { message.content.clone_from(x) } |
587 | | - if let Some(x) = timestamp { message.timestamp = x.clone() } |
588 | | - message.edited_timestamp = *edited_timestamp; |
589 | | - if let Some(x) = tts { message.set_tts(*x) } |
590 | | - if let Some(x) = mention_everyone { message.set_mention_everyone(*x) } |
591 | | - if let Some(x) = mentions { message.mentions.clone_from(x) } |
592 | | - if let Some(x) = mention_roles { message.mention_roles.clone_from(x) } |
593 | | - if let Some(x) = mention_channels { message.mention_channels.clone_from(x) } |
594 | | - if let Some(x) = attachments { message.attachments.clone_from(x) } |
595 | | - if let Some(x) = embeds { message.embeds.clone_from(x) } |
596 | | - if let Some(x) = reactions { message.reactions.clone_from(x) } |
597 | | - if let Some(x) = pinned { message.set_pinned(*x) } |
598 | | - if let Some(x) = webhook_id { message.webhook_id.clone_from(x) } |
599 | | - if let Some(x) = kind { message.kind = x.clone() } |
600 | | - if let Some(x) = activity { message.activity.clone_from(x) } |
601 | | - if let Some(x) = application { message.application.clone_from(x) } |
602 | | - if let Some(x) = application_id { message.application_id.clone_from(x) } |
603 | | - if let Some(x) = message_reference { message.message_reference.clone_from(x) } |
604 | | - if let Some(x) = flags { message.flags.clone_from(x) } |
605 | | - if let Some(x) = referenced_message { message.referenced_message.clone_from(x) } |
606 | | - #[cfg(not(feature = "unstable"))] |
607 | | - if let Some(x) = interaction { message.interaction.clone_from(x) } |
608 | | - if let Some(x) = interaction_metadata { message.interaction_metadata.clone_from(x) } |
609 | | - if let Some(x) = thread { message.thread.clone_from(x) } |
610 | | - if let Some(x) = components { message.components.clone_from(x) } |
611 | | - if let Some(x) = sticker_items { message.sticker_items.clone_from(x) } |
612 | | - if let Some(x) = position { message.position.clone_from(x) } |
613 | | - if let Some(x) = role_subscription_data { message.role_subscription_data.clone_from(x) } |
614 | | - message.guild_id = *guild_id; |
615 | | - if let Some(x) = member { message.member.clone_from(x) } |
616 | | - } |
| 473 | + #[serde(flatten)] |
| 474 | + pub message: Message, |
617 | 475 | } |
618 | 476 |
|
619 | 477 | /// Requires [`GatewayIntents::GUILD_PRESENCES`]. |
|
0 commit comments