Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MessageReferenceKind enum #2996

Merged
merged 11 commits into from
Nov 11, 2024
2 changes: 1 addition & 1 deletion src/builder/create_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl CreateMessage {
self
}

/// Set the reference message this message is a reply to.
/// Set the message this reply or forward is referring to.
pub fn reference_message(mut self, reference: impl Into<MessageReference>) -> Self {
self.message_reference = Some(reference.into());
self
Expand Down
71 changes: 71 additions & 0 deletions src/builder/create_message_reference.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this completely unused now? This shouldn't be added for current, you can make a separate PR after this is merged and next is rebased to add this builder and swap CreateMessage::reference_message to use this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can remove this if you want, but I made an from impl to convert a CreateMessageReference into a MessageReference, so not 100% unused but I get your point.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, please remove it for now.

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use crate::model::prelude::*;

/// Reference data sent with crossposted messages.
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#message-reference-object-message-reference-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct CreateMessageReference {
/// The type of Message Reference.
#[serde(rename = "type", default = "MessageReferenceKind::default")]
pub kind: MessageReferenceKind,
/// The ID of the originating messages's channel.
pub channel_id: ChannelId,
/// The ID of the originating message.
pub message_id: Option<MessageId>,
/// The ID of the originating messages's guild.
pub guild_id: Option<GuildId>,
/// When sending, whether to error if the referenced message doesn't exist instead of sending
/// as a normal(non-reply) message, default true.
pub fail_if_not_exists: Option<bool>,
}

impl CreateMessageReference {
#[must_use]
pub fn new(kind: MessageReferenceKind, message_id: MessageId, channel_id: ChannelId) -> Self {
Self {
kind,
channel_id,
message_id: Some(message_id),
guild_id: None,
fail_if_not_exists: None,
}
}

#[must_use]
pub fn guild(mut self, guild: GuildId) -> Self {
self.guild_id = Some(guild);
self
}

#[must_use]
pub fn fail_if_not_exists(mut self, fail_if_not_exists: bool) -> Self {
self.fail_if_not_exists = Some(fail_if_not_exists);
self
}
}

impl From<MessageReference> for CreateMessageReference {
fn from(value: MessageReference) -> Self {
Self {
kind: value.kind,
channel_id: value.channel_id,
message_id: value.message_id,
guild_id: value.guild_id,
fail_if_not_exists: value.fail_if_not_exists,
}
}
}

impl From<&Message> for CreateMessageReference {
fn from(value: &Message) -> Self {
Self {
kind: MessageReferenceKind::default(),
channel_id: value.channel_id,
message_id: Some(value.id),
guild_id: value.guild_id,
fail_if_not_exists: None,
}
}
}
2 changes: 2 additions & 0 deletions src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ mod create_interaction_response;
mod create_interaction_response_followup;
mod create_invite;
mod create_message;
mod create_message_reference;
pub mod create_poll;
mod create_scheduled_event;
mod create_stage_instance;
Expand Down Expand Up @@ -93,6 +94,7 @@ pub use create_interaction_response::*;
pub use create_interaction_response_followup::*;
pub use create_invite::*;
pub use create_message::*;
pub use create_message_reference::*;
pub use create_poll::{CreatePoll, CreatePollAnswer};
pub use create_scheduled_event::*;
pub use create_stage_instance::*;
Expand Down
37 changes: 37 additions & 0 deletions src/model/channel/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::fmt::Display;
#[cfg(all(feature = "cache", feature = "model"))]
use std::fmt::Write;

#[cfg(feature = "builder")]
use crate::builder::CreateMessageReference;
#[cfg(all(feature = "model", feature = "utils"))]
use crate::builder::{Builder, CreateAllowedMentions, CreateMessage, EditMessage};
#[cfg(all(feature = "cache", feature = "model"))]
Expand Down Expand Up @@ -1086,13 +1088,32 @@ pub struct MessageActivity {
pub party_id: Option<String>,
}

enum_number! {
/// Message Reference Type information
///
/// [Discord docs](https://discord.com/developers/docs/resources/message#message-reference-types)
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum MessageReferenceKind {
#[default]
Default = 0,
Forward = 1,
_ => Unknown(u8),
}
}

/// Reference data sent with crossposted messages.
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#message-reference-object-message-reference-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct MessageReference {
/// The Type of Message Reference
#[serde(rename = "type", default = "MessageReferenceKind::default")]
pub kind: MessageReferenceKind,
/// ID of the originating message.
pub message_id: Option<MessageId>,
/// ID of the originating message's channel.
Expand All @@ -1107,6 +1128,7 @@ pub struct MessageReference {
impl From<&Message> for MessageReference {
fn from(m: &Message) -> Self {
Self {
kind: MessageReferenceKind::default(),
message_id: Some(m.id),
channel_id: m.channel_id,
guild_id: m.guild_id,
Expand All @@ -1115,9 +1137,24 @@ impl From<&Message> for MessageReference {
}
}

#[cfg(feature = "builder")]
impl From<CreateMessageReference> for MessageReference {
fn from(value: CreateMessageReference) -> Self {
Self {
kind: value.kind,
message_id: value.message_id,
channel_id: value.channel_id,
guild_id: value.guild_id,
fail_if_not_exists: value.fail_if_not_exists,
}
}
}

impl From<(ChannelId, MessageId)> for MessageReference {
// TODO(next): Remove this
fn from(pair: (ChannelId, MessageId)) -> Self {
Self {
kind: MessageReferenceKind::default(),
message_id: Some(pair.1),
channel_id: pair.0,
guild_id: None,
Expand Down
Loading