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

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions src/Discord.Net.Core/Entities/Messages/IUserMessage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Discord
Expand Down Expand Up @@ -29,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>
/// Modifies 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,
}
23 changes: 23 additions & 0 deletions src/Discord.Net.Core/Entities/Messages/MessageSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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;

/// <summary>
/// Gets the id of the originating message's guild
/// </summary>
public readonly ulong? GuildId;

internal MessageSnapshot(IMessage message, ulong? guildId)
{
Message = message;
GuildId = guildId;
}
}
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 @@ -101,4 +101,7 @@ internal class Message

[JsonProperty("interaction_metadata")]
public Optional<MessageInteractionMetadata> InteractionMetadata { get; set; }

[JsonProperty("message_snapshot")]
public Optional<MessageSnapshot[]> MessageSnapshots { get; set; }
}
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
12 changes: 12 additions & 0 deletions src/Discord.Net.Rest/API/Common/MessageSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json;

namespace Discord.API;

internal class MessageSnapshot
{
[JsonProperty("message")]
public Message Message { get; set; }

[JsonProperty("guild_id")]
public Optional<ulong> GuildId { 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 @@ -158,7 +158,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
12 changes: 12 additions & 0 deletions src/Discord.Net.Rest/Entities/Messages/RestUserMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,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 @@ -167,6 +170,15 @@ internal override void Update(Model model)
}
if (model.InteractionMetadata.IsSpecified)
InteractionMetadata = model.InteractionMetadata.Value.ToInteractionMetadata();

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

/// <inheritdoc />
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 @@ -192,7 +192,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
13 changes: 13 additions & 0 deletions src/Discord.Net.WebSocket/Entities/Messages/SocketUserMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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 @@ -209,6 +212,16 @@ internal override void Update(ClientState state, Model model)

if (model.InteractionMetadata.IsSpecified)
InteractionMetadata = model.InteractionMetadata.Value.ToInteractionMetadata();


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

/// <inheritdoc />
Expand Down