Skip to content

Commit

Permalink
Add message_snapshots on Message
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbt365 committed Jan 15, 2025
1 parent bceffa2 commit 2bca33f
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/model/channel/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ pub struct Message {
pub flags: Option<MessageFlags>,
/// The message that was replied to using this message.
pub referenced_message: Option<Box<Message>>, // Boxed to avoid recursion
/// An array of message snapshots, known as forwarded messages.
#[serde(default, deserialize_with = "deserialize_snapshots")]
pub message_snapshots: Vec<MessageSnapshot>,
#[cfg_attr(not(ignore_serenity_deprecated), deprecated = "Use interaction_metadata")]
pub interaction: Option<Box<MessageInteraction>>,
/// Sent if the message is a response to an [`Interaction`].
Expand Down Expand Up @@ -1190,6 +1193,47 @@ pub struct ChannelMention {
pub name: String,
}

/// [Discord docs](https://discord.com/developers/docs/resources/message#message-snapshot-structure)
///
/// For field documentation, see [`Message`].
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct MessageSnapshot {
pub content: String,
pub timestamp: Timestamp,
pub edited_timestamp: Option<Timestamp>,
pub mentions: Vec<User>,
#[serde(default)]
pub mention_roles: Vec<RoleId>,
pub attachments: Vec<Attachment>,
pub embeds: Vec<Embed>,
#[serde(rename = "type")]
pub kind: MessageType,
pub flags: Option<MessageFlags>,
#[serde(default)]
pub components: Vec<ActionRow>,
#[serde(default)]
pub sticker_items: Vec<StickerItem>,
}

/// Custom deserialization function to handle the nested "message" field
fn deserialize_snapshots<'de, D>(deserializer: D) -> Result<Vec<MessageSnapshot>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct MessageSnapshotWrapper {
pub message: MessageSnapshot,
}

let snapshots: Vec<MessageSnapshotWrapper> = Deserialize::deserialize(deserializer)?;

let result = snapshots.into_iter().map(|wrapper| wrapper.message).collect();

Ok(result)
}

bitflags! {
/// Describes extra features of the message.
///
Expand Down

0 comments on commit 2bca33f

Please sign in to comment.