feat(bot): always allow reproxying latest message in channel

This commit is contained in:
Iris System 2022-08-27 17:02:50 +12:00
parent f54cfbdff3
commit d9fe78bdf2
2 changed files with 24 additions and 3 deletions

View File

@ -47,6 +47,15 @@ public class DiscordApiClient
_client.Get<GuildMember>($"/guilds/{guildId}/members/{userId}",
("GetGuildMember", guildId));
public Task<Message[]> GetChannelMessages(ulong channelId, int? limit)
{
var url = $"/channels/{channelId}/messages";
if (limit != null)
url += $"?limit={limit}";
return _client.Get<Message[]>(url, ("GetChannelMessages", channelId))!;
}
public Task<Message> CreateMessage(ulong channelId, MessageRequest request, MultipartFile[]? files = null) =>
_client.PostMultipart<Message>($"/channels/{channelId}/messages", ("CreateMessage", channelId), request,
files)!;

View File

@ -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,8 +188,14 @@ 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)
if (isReproxy && !isLatestMessage)
if (SystemClock.Instance.GetCurrentInstant() - msgTimestamp > timeout)
throw new PKError($"The message is too old to be {editTypeAction}.");
return msg;