Fix permission error on message edit handler

Signed-off-by: Ske <voltasalt@gmail.com>
This commit is contained in:
Ske 2021-05-03 10:29:22 +02:00
parent 65387bfea4
commit aa2a234f8d
3 changed files with 48 additions and 26 deletions

View File

@ -53,7 +53,7 @@ namespace PluralKit.Bot
private bool IsDuplicateMessage(Message msg) =>
// We consider a message duplicate if it has the same ID as the previous message that hit the gateway
_lastMessageCache.GetLastMessage(msg.ChannelId)?.mid == msg.Id;
_lastMessageCache.GetLastMessage(msg.ChannelId)?.Id == msg.Id;
public async Task Handle(Shard shard, MessageCreateEvent evt)
{

View File

@ -11,6 +11,8 @@ using Myriad.Types;
using PluralKit.Core;
using Serilog;
namespace PluralKit.Bot
{
@ -25,9 +27,9 @@ namespace PluralKit.Bot
private readonly IDiscordCache _cache;
private readonly Bot _bot;
private readonly DiscordApiClient _rest;
private readonly ILogger _logger;
public MessageEdited(LastMessageCacheService lastMessageCache, ProxyService proxy, IDatabase db, IMetrics metrics, ModelRepository repo, Cluster client, IDiscordCache cache, Bot bot, DiscordApiClient rest)
public MessageEdited(LastMessageCacheService lastMessageCache, ProxyService proxy, IDatabase db, IMetrics metrics, ModelRepository repo, Cluster client, IDiscordCache cache, Bot bot, DiscordApiClient rest, ILogger logger)
{
_lastMessageCache = lastMessageCache;
_proxy = proxy;
@ -38,6 +40,7 @@ namespace PluralKit.Bot
_cache = cache;
_bot = bot;
_rest = rest;
_logger = logger.ForContext<MessageEdited>();
}
public async Task Handle(Shard shard, MessageUpdateEvent evt)
@ -55,7 +58,7 @@ namespace PluralKit.Bot
var lastMessage = _lastMessageCache.GetLastMessage(evt.ChannelId);
// Only react to the last message in the channel
if (lastMessage?.mid != evt.Id)
if (lastMessage?.Id != evt.Id)
return;
// Just run the normal message handling code, with a flag to disable autoproxying
@ -64,7 +67,22 @@ namespace PluralKit.Bot
using (_metrics.Measure.Timer.Time(BotMetrics.MessageContextQueryTime))
ctx = await _repo.GetMessageContext(conn, evt.Author.Value!.Id, channel.GuildId!.Value, evt.ChannelId);
Message referencedMessage = (lastMessage.referenced_message != null) ? await _rest.GetMessage(evt.ChannelId, lastMessage.referenced_message.Value) : null;
var equivalentEvt = await GetMessageCreateEvent(evt, lastMessage, channel);
var botPermissions = _bot.PermissionsIn(channel.Id);
await _proxy.HandleIncomingMessage(shard, equivalentEvt, ctx, allowAutoproxy: false, guild: guild, channel: channel, botPermissions: botPermissions);
}
private async Task<MessageCreateEvent> GetMessageCreateEvent(MessageUpdateEvent evt, CachedMessage lastMessage, Channel channel)
{
var referencedMessage = await GetReferencedMessage(evt.ChannelId, lastMessage.ReferencedMessage);
var messageReference = lastMessage.ReferencedMessage != null
? new Message.Reference(channel.GuildId, evt.ChannelId, lastMessage.ReferencedMessage.Value)
: null;
var messageType = lastMessage.ReferencedMessage != null
? Message.MessageType.Reply
: Message.MessageType.Default;
// TODO: is this missing anything?
var equivalentEvt = new MessageCreateEvent
@ -76,12 +94,27 @@ namespace PluralKit.Bot
Member = evt.Member.Value,
Content = evt.Content.Value,
Attachments = evt.Attachments.Value ?? Array.Empty<Message.Attachment>(),
MessageReference = (lastMessage.referenced_message != null) ? new (channel.GuildId, evt.ChannelId, lastMessage.referenced_message.Value) : null,
MessageReference = messageReference,
ReferencedMessage = referencedMessage,
Type = (lastMessage.referenced_message != null) ? Message.MessageType.Reply : Message.MessageType.Default,
Type = messageType,
};
var botPermissions = _bot.PermissionsIn(channel.Id);
await _proxy.HandleIncomingMessage(shard, equivalentEvt, ctx, allowAutoproxy: false, guild: guild, channel: channel, botPermissions: botPermissions);
return equivalentEvt;
}
private async Task<Message?> GetReferencedMessage(ulong channelId, ulong? referencedMessageId)
{
if (referencedMessageId == null)
return null;
var botPermissions = _bot.PermissionsIn(channelId);
if (!botPermissions.HasFlag(PermissionSet.ReadMessageHistory))
{
_logger.Warning("Tried to get referenced message in channel {ChannelId} to reply but bot does not have Read Message History",
channelId);
return null;
}
return await _rest.GetMessage(channelId, referencedMessageId.Value);
}
}
}

View File

@ -1,4 +1,5 @@
using System.Collections.Concurrent;
#nullable enable
using System.Collections.Concurrent;
using System.Collections.Generic;
using Myriad.Types;
@ -12,26 +13,14 @@ namespace PluralKit.Bot
public void AddMessage(Message msg)
{
_cache[msg.ChannelId] = new CachedMessage(msg);
_cache[msg.ChannelId] = new CachedMessage(msg.Id, msg.ReferencedMessage.Value?.Id);
}
public CachedMessage GetLastMessage(ulong channel)
public CachedMessage? GetLastMessage(ulong channel)
{
if (_cache.TryGetValue(channel, out var message)) return message;
return null;
return _cache.TryGetValue(channel, out var message) ? message : null;
}
}
public class CachedMessage
{
public ulong mid;
public ulong? referenced_message;
public CachedMessage(Message msg)
{
mid = msg.Id;
if (msg.ReferencedMessage.Value != null)
referenced_message = msg.ReferencedMessage.Value.Id;
}
}
public record CachedMessage(ulong Id, ulong? ReferencedMessage);
}