Fix permission error on message edit handler
Signed-off-by: Ske <voltasalt@gmail.com>
This commit is contained in:
parent
65387bfea4
commit
aa2a234f8d
@ -53,7 +53,7 @@ namespace PluralKit.Bot
|
|||||||
|
|
||||||
private bool IsDuplicateMessage(Message msg) =>
|
private bool IsDuplicateMessage(Message msg) =>
|
||||||
// We consider a message duplicate if it has the same ID as the previous message that hit the gateway
|
// 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)
|
public async Task Handle(Shard shard, MessageCreateEvent evt)
|
||||||
{
|
{
|
||||||
|
@ -11,6 +11,8 @@ using Myriad.Types;
|
|||||||
|
|
||||||
using PluralKit.Core;
|
using PluralKit.Core;
|
||||||
|
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
|
|
||||||
namespace PluralKit.Bot
|
namespace PluralKit.Bot
|
||||||
{
|
{
|
||||||
@ -25,9 +27,9 @@ namespace PluralKit.Bot
|
|||||||
private readonly IDiscordCache _cache;
|
private readonly IDiscordCache _cache;
|
||||||
private readonly Bot _bot;
|
private readonly Bot _bot;
|
||||||
private readonly DiscordApiClient _rest;
|
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, ILogger logger)
|
||||||
public MessageEdited(LastMessageCacheService lastMessageCache, ProxyService proxy, IDatabase db, IMetrics metrics, ModelRepository repo, Cluster client, IDiscordCache cache, Bot bot, DiscordApiClient rest)
|
|
||||||
{
|
{
|
||||||
_lastMessageCache = lastMessageCache;
|
_lastMessageCache = lastMessageCache;
|
||||||
_proxy = proxy;
|
_proxy = proxy;
|
||||||
@ -38,6 +40,7 @@ namespace PluralKit.Bot
|
|||||||
_cache = cache;
|
_cache = cache;
|
||||||
_bot = bot;
|
_bot = bot;
|
||||||
_rest = rest;
|
_rest = rest;
|
||||||
|
_logger = logger.ForContext<MessageEdited>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Handle(Shard shard, MessageUpdateEvent evt)
|
public async Task Handle(Shard shard, MessageUpdateEvent evt)
|
||||||
@ -55,7 +58,7 @@ namespace PluralKit.Bot
|
|||||||
var lastMessage = _lastMessageCache.GetLastMessage(evt.ChannelId);
|
var lastMessage = _lastMessageCache.GetLastMessage(evt.ChannelId);
|
||||||
|
|
||||||
// Only react to the last message in the channel
|
// Only react to the last message in the channel
|
||||||
if (lastMessage?.mid != evt.Id)
|
if (lastMessage?.Id != evt.Id)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Just run the normal message handling code, with a flag to disable autoproxying
|
// 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))
|
using (_metrics.Measure.Timer.Time(BotMetrics.MessageContextQueryTime))
|
||||||
ctx = await _repo.GetMessageContext(conn, evt.Author.Value!.Id, channel.GuildId!.Value, evt.ChannelId);
|
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?
|
// TODO: is this missing anything?
|
||||||
var equivalentEvt = new MessageCreateEvent
|
var equivalentEvt = new MessageCreateEvent
|
||||||
@ -76,12 +94,27 @@ namespace PluralKit.Bot
|
|||||||
Member = evt.Member.Value,
|
Member = evt.Member.Value,
|
||||||
Content = evt.Content.Value,
|
Content = evt.Content.Value,
|
||||||
Attachments = evt.Attachments.Value ?? Array.Empty<Message.Attachment>(),
|
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,
|
ReferencedMessage = referencedMessage,
|
||||||
Type = (lastMessage.referenced_message != null) ? Message.MessageType.Reply : Message.MessageType.Default,
|
Type = messageType,
|
||||||
};
|
};
|
||||||
var botPermissions = _bot.PermissionsIn(channel.Id);
|
return equivalentEvt;
|
||||||
await _proxy.HandleIncomingMessage(shard, equivalentEvt, ctx, allowAutoproxy: false, guild: guild, channel: channel, botPermissions: botPermissions);
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Concurrent;
|
#nullable enable
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
using Myriad.Types;
|
using Myriad.Types;
|
||||||
@ -12,26 +13,14 @@ namespace PluralKit.Bot
|
|||||||
|
|
||||||
public void AddMessage(Message msg)
|
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 _cache.TryGetValue(channel, out var message) ? message : null;
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class CachedMessage
|
public record CachedMessage(ulong Id, ulong? ReferencedMessage);
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user