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

[Feature] Message Forwards #2918

Merged
merged 5 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Discord.Net.Core/Entities/Messages/IUserMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public interface IUserMessage : IMessage
/// </remarks>
IMessageInteractionMetadata InteractionMetadata { get; }

/// <summary>
/// Gets a collection of partial messages that were forwarded with this message.
/// </summary>
IReadOnlyCollection<MessageSnapshot> ForwardedMessages { get; }

/// <summary>
/// Gets the poll sent with this message.
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion src/Discord.Net.Core/Entities/Messages/MessageReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public class MessageReference
/// </summary>
public Optional<bool> FailIfNotExists { get; internal set; }

/// <summary>
/// Gets the type of message reference.
/// </summary>
public Optional<MessageReferenceType> ReferenceType { get; internal set; }

/// <summary>
/// Initializes a new instance of the <see cref="MessageReference"/> class.
/// </summary>
Expand All @@ -48,12 +53,14 @@ public class MessageReference
/// <param name="failIfNotExists">
/// Whether to error if the referenced message doesn't exist instead of sending as a normal (non-reply) message. Defaults to true.
/// </param>
public MessageReference(ulong? messageId = null, ulong? channelId = null, ulong? guildId = null, bool? failIfNotExists = null)
public MessageReference(ulong? messageId = null, ulong? channelId = null, ulong? guildId = null, bool? failIfNotExists = null,
MessageReferenceType referenceType = MessageReferenceType.Default)
{
MessageId = messageId ?? Optional.Create<ulong>();
InternalChannelId = channelId ?? Optional.Create<ulong>();
GuildId = guildId ?? Optional.Create<ulong>();
FailIfNotExists = failIfNotExists ?? Optional.Create<bool>();
ReferenceType = referenceType;
}

private string DebuggerDisplay
Expand Down
17 changes: 17 additions & 0 deletions src/Discord.Net.Core/Entities/Messages/MessageReferenceType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Discord;

/// <summary>
/// Determines how associated data is populated.
/// </summary>
public enum MessageReferenceType
{
/// <summary>
/// A standard reference used by replies.
/// </summary>
Default = 0,

/// <summary>
/// Reference used to point to a message at a point in time.
/// </summary>
Forward = 1,
}
17 changes: 17 additions & 0 deletions src/Discord.Net.Core/Entities/Messages/MessageSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Discord;

/// <summary>
/// Represents a snapshot of a message.
/// </summary>
public readonly struct MessageSnapshot
{
/// <summary>
/// Gets the partial message that was forwarded.
/// </summary>
public readonly IMessage Message;

internal MessageSnapshot(IMessage message)
{
Message = message;
}
}
3 changes: 3 additions & 0 deletions src/Discord.Net.Rest/API/Common/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ internal class Message
[JsonProperty("interaction_metadata")]
public Optional<MessageInteractionMetadata> InteractionMetadata { get; set; }

[JsonProperty("message_snapshot")]
public Optional<MessageSnapshot[]> MessageSnapshots { get; set; }

[JsonProperty("poll")]
public Optional<Poll> Poll { get; set; }

Expand Down
3 changes: 3 additions & 0 deletions src/Discord.Net.Rest/API/Common/MessageReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ namespace Discord.API
{
internal class MessageReference
{
[JsonProperty("type")]
public Optional<MessageReferenceType> Type { get; set; }

[JsonProperty("message_id")]
public Optional<ulong> MessageId { get; set; }

Expand Down
9 changes: 9 additions & 0 deletions src/Discord.Net.Rest/API/Common/MessageSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Newtonsoft.Json;

namespace Discord.API;

internal class MessageSnapshot
{
[JsonProperty("message")]
public Message Message { get; set; }
}
3 changes: 2 additions & 1 deletion src/Discord.Net.Rest/Entities/Messages/RestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ internal virtual void Update(Model model)
GuildId = model.Reference.Value.GuildId,
InternalChannelId = model.Reference.Value.ChannelId,
MessageId = model.Reference.Value.MessageId,
FailIfNotExists = model.Reference.Value.FailIfNotExists
FailIfNotExists = model.Reference.Value.FailIfNotExists,
ReferenceType = model.Reference.Value.Type
};
}

Expand Down
11 changes: 11 additions & 0 deletions src/Discord.Net.Rest/Entities/Messages/RestUserMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public class RestUserMessage : RestMessage, IUserMessage
/// <inheritdoc />
public MessageResolvedData ResolvedData { get; internal set; }

/// <inheritdoc />
public IReadOnlyCollection<MessageSnapshot> ForwardedMessages { get; internal set; }

internal RestUserMessage(BaseDiscordClient discord, ulong id, IMessageChannel channel, IUser author, MessageSource source)
: base(discord, id, channel, author, source)
{
Expand Down Expand Up @@ -171,6 +174,14 @@ internal override void Update(Model model)
if (model.InteractionMetadata.IsSpecified)
InteractionMetadata = model.InteractionMetadata.Value.ToInteractionMetadata(Discord);

if (model.MessageSnapshots.IsSpecified)
{
ForwardedMessages = model.MessageSnapshots.Value.Select(x =>
new MessageSnapshot(RestMessage.Create(Discord, null, null, x.Message))).ToImmutableArray();
}
else
ForwardedMessages = ImmutableArray<MessageSnapshot>.Empty;

if (model.Poll.IsSpecified)
Poll = model.Poll.Value.ToEntity();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Discord.Net.WebSocket/Entities/Messages/SocketMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ internal virtual void Update(ClientState state, Model model)
GuildId = model.Reference.Value.GuildId,
InternalChannelId = model.Reference.Value.ChannelId,
MessageId = model.Reference.Value.MessageId,
FailIfNotExists = model.Reference.Value.FailIfNotExists
FailIfNotExists = model.Reference.Value.FailIfNotExists,
ReferenceType = model.Reference.Value.Type
};
}

Expand Down
11 changes: 11 additions & 0 deletions src/Discord.Net.WebSocket/Entities/Messages/SocketUserMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public class SocketUserMessage : SocketMessage, IUserMessage
/// <inheritdoc />
public MessageResolvedData ResolvedData { get; internal set; }

/// <inheritdoc />
public IReadOnlyCollection<MessageSnapshot> ForwardedMessages { get; internal set; }

internal SocketUserMessage(DiscordSocketClient discord, ulong id, ISocketMessageChannel channel, SocketUser author, MessageSource source)
: base(discord, id, channel, author, source)
{
Expand Down Expand Up @@ -213,6 +216,14 @@ internal override void Update(ClientState state, Model model)
if (model.InteractionMetadata.IsSpecified)
InteractionMetadata = model.InteractionMetadata.Value.ToInteractionMetadata(Discord);

if (model.MessageSnapshots.IsSpecified)
{
ForwardedMessages = model.MessageSnapshots.Value.Select(x =>
new MessageSnapshot(RestMessage.Create(Discord, null, null, x.Message))).ToImmutableArray();
}
else
ForwardedMessages = ImmutableArray<MessageSnapshot>.Empty;

if (model.Poll.IsSpecified)
Poll = model.Poll.Value.ToEntity();
}
Expand Down