diff --git a/Myriad/Rest/DiscordApiClient.cs b/Myriad/Rest/DiscordApiClient.cs index 38ff3635..347b7352 100644 --- a/Myriad/Rest/DiscordApiClient.cs +++ b/Myriad/Rest/DiscordApiClient.cs @@ -47,6 +47,15 @@ public class DiscordApiClient _client.Get($"/guilds/{guildId}/members/{userId}", ("GetGuildMember", guildId)); + public Task GetChannelMessages(ulong channelId, int? limit) + { + var url = $"/channels/{channelId}/messages"; + if (limit != null) + url += $"?limit={limit}"; + + return _client.Get(url, ("GetChannelMessages", channelId))!; + } + public Task CreateMessage(ulong channelId, MessageRequest request, MultipartFile[]? files = null) => _client.PostMultipart($"/channels/{channelId}/messages", ("CreateMessage", channelId), request, files)!; diff --git a/PluralKit.Bot/Commands/Message.cs b/PluralKit.Bot/Commands/Message.cs index 49384356..5c1c4b42 100644 --- a/PluralKit.Bot/Commands/Message.cs +++ b/PluralKit.Bot/Commands/Message.cs @@ -157,7 +157,13 @@ public class ProxiedMessage if (ctx.Guild == null) throw new PKSyntaxError($"You must use a message link to {editType} messages in DMs."); - var recent = await FindRecentMessage(ctx, timeout); + PKMessage? recent; + + if (isReproxy) + recent = await ctx.Repository.GetLastMessage(ctx.Guild.Id, ctx.Channel.Id, ctx.Author.Id); + else + recent = await FindRecentMessage(ctx, timeout); + if (recent == null) throw new PKSyntaxError($"Could not find a recent message to {editType}."); @@ -182,9 +188,15 @@ public class ProxiedMessage throw new PKError(error); } + var latestMessages = await _rest.GetChannelMessages(msg.Message.Channel, 2); + var isLatestMessage = latestMessages.LastOrDefault()?.Id == ctx.Message.Id + ? latestMessages.FirstOrDefault()?.Id == msg.Message.Mid + : latestMessages.LastOrDefault()?.Id == msg.Message.Mid; + var msgTimestamp = DiscordUtils.SnowflakeToInstant(msg.Message.Mid); - if (isReproxy && SystemClock.Instance.GetCurrentInstant() - msgTimestamp > timeout) - throw new PKError($"The message is too old to be {editTypeAction}."); + if (isReproxy && !isLatestMessage) + if (SystemClock.Instance.GetCurrentInstant() - msgTimestamp > timeout) + throw new PKError($"The message is too old to be {editTypeAction}."); return msg; }