Skip to content

Commit

Permalink
Add more fields to MessageUpdateEvent (#2154)
Browse files Browse the repository at this point in the history
  • Loading branch information
kangalio authored Sep 17, 2022
1 parent 2eea7d7 commit 96429aa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 48 deletions.
73 changes: 32 additions & 41 deletions src/cache/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,48 +480,39 @@ impl CacheUpdate for MessageCreateEvent {
impl CacheUpdate for MessageUpdateEvent {
type Output = Message;

#[rustfmt::skip]
fn update(&mut self, cache: &Cache) -> Option<Self::Output> {
if let Some(messages) = cache.messages.get_mut(&self.channel_id) {
if let Some(mut message) = messages.get_mut(&self.id) {
let item = message.clone();

if let Some(attachments) = self.attachments.clone() {
message.attachments = attachments;
}

if let Some(content) = self.content.clone() {
message.content = content;
}

if let Some(edited_timestamp) = self.edited_timestamp {
message.edited_timestamp = Some(edited_timestamp);
}

if let Some(mentions) = self.mentions.clone() {
message.mentions = mentions;
}

if let Some(mention_everyone) = self.mention_everyone {
message.mention_everyone = mention_everyone;
}

if let Some(mention_roles) = self.mention_roles.clone() {
message.mention_roles = mention_roles;
}

if let Some(pinned) = self.pinned {
message.pinned = pinned;
}

if self.flags.is_some() {
message.flags = self.flags;
}

return Some(item);
}
}

None
// Destructure, so we get an `unused` warning when we forget to process one of the fields
// in this method
#[allow(deprecated)] // yes rust, exhaustive means exhaustive, even the deprecated ones
let Self {
id, channel_id, content, edited_timestamp, tts, mention_everyone, mentions,
mention_roles, mention_channels, attachments, embeds, reactions, pinned, flags,
components, sticker_items,

author: _, timestamp: _, nonce: _, kind: _, stickers: _, guild_id: _,
} = &self;

let messages = cache.messages.get_mut(channel_id)?;
let mut message = messages.get_mut(id)?;
let old_message = message.clone();

if let Some(x) = attachments { message.attachments = x.clone() }
if let Some(x) = content { message.content = x.clone() }
if let Some(x) = edited_timestamp { message.edited_timestamp = Some(*x) }
if let Some(x) = mentions { message.mentions = x.clone() }
if let Some(x) = mention_everyone { message.mention_everyone = *x }
if let Some(x) = mention_roles { message.mention_roles = x.clone() }
if let Some(x) = mention_channels { message.mention_channels = x.clone() }
if let Some(x) = pinned { message.pinned = *x }
if let Some(x) = flags { message.flags = Some(*x) }
if let Some(x) = tts { message.tts = *x }
if let Some(x) = embeds { message.embeds = x.clone() }
if let Some(x) = reactions { message.reactions = x.clone() }
if let Some(x) = components { message.components = x.clone() }
if let Some(x) = sticker_items { message.sticker_items = x.clone() }

Some(old_message)
}
}

Expand Down
22 changes: 15 additions & 7 deletions src/model/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::fmt;

use serde::de::{Error as DeError, IgnoredAny, MapAccess};

use super::application::component::ActionRow;
use super::prelude::*;
use super::utils::{emojis, roles, stickers};
use crate::constants::OpCode;
Expand Down Expand Up @@ -406,22 +407,29 @@ pub struct MessageDeleteEvent {
#[non_exhaustive]
pub struct MessageUpdateEvent {
pub id: MessageId,
pub guild_id: Option<GuildId>,
pub channel_id: ChannelId,
pub kind: Option<MessageType>,
pub author: Option<User>, // TODO: Is this a Message field that can even change?
pub content: Option<String>,
pub nonce: Option<String>,
pub tts: Option<bool>,
pub pinned: Option<bool>,
pub timestamp: Option<Timestamp>,
pub timestamp: Option<Timestamp>, // TODO: Is this a Message field that can even change?
pub edited_timestamp: Option<Timestamp>,
pub author: Option<User>,
pub tts: Option<bool>,
pub mention_everyone: Option<bool>,
pub mentions: Option<Vec<User>>,
pub mention_roles: Option<Vec<RoleId>>,
pub mention_channels: Option<Vec<ChannelMention>>,
pub attachments: Option<Vec<Attachment>>,
pub embeds: Option<Vec<Embed>>,
pub reactions: Option<Vec<MessageReaction>>,
pub nonce: Option<String>, // TODO: Is this a Message field that can even change?
pub pinned: Option<bool>,
pub kind: Option<MessageType>, // TODO: Is this a Message field that can even change?
pub flags: Option<MessageFlags>,
pub components: Option<Vec<ActionRow>>,
#[deprecated(note = "deprecated by Discord")]
pub stickers: Option<Vec<StickerItem>>,
pub sticker_items: Option<Vec<StickerItem>>,

pub guild_id: Option<GuildId>, // TODO: Is this a Message field that can even change?
}

/// [Discord docs](https://discord.com/developers/docs/topics/gateway#presence-update).
Expand Down

0 comments on commit 96429aa

Please sign in to comment.