Merge pull request #292 from dev-kittens/fix/edit-replies
Fix handling replies in edited messages
This commit is contained in:
commit
ffae424a6c
@ -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) == msg.Id;
|
_lastMessageCache.GetLastMessage(msg.ChannelId)?.mid == msg.Id;
|
||||||
|
|
||||||
public async Task Handle(Shard shard, MessageCreateEvent evt)
|
public async Task Handle(Shard shard, MessageCreateEvent evt)
|
||||||
{
|
{
|
||||||
@ -66,7 +66,7 @@ namespace PluralKit.Bot
|
|||||||
|
|
||||||
// Log metrics and message info
|
// Log metrics and message info
|
||||||
_metrics.Measure.Meter.Mark(BotMetrics.MessagesReceived);
|
_metrics.Measure.Meter.Mark(BotMetrics.MessagesReceived);
|
||||||
_lastMessageCache.AddMessage(evt.ChannelId, evt.Id);
|
_lastMessageCache.AddMessage(evt);
|
||||||
|
|
||||||
// Get message context from DB (tracking w/ metrics)
|
// Get message context from DB (tracking w/ metrics)
|
||||||
MessageContext ctx;
|
MessageContext ctx;
|
||||||
|
@ -6,6 +6,7 @@ using App.Metrics;
|
|||||||
using Myriad.Cache;
|
using Myriad.Cache;
|
||||||
using Myriad.Extensions;
|
using Myriad.Extensions;
|
||||||
using Myriad.Gateway;
|
using Myriad.Gateway;
|
||||||
|
using Myriad.Rest;
|
||||||
using Myriad.Types;
|
using Myriad.Types;
|
||||||
|
|
||||||
using PluralKit.Core;
|
using PluralKit.Core;
|
||||||
@ -23,8 +24,10 @@ namespace PluralKit.Bot
|
|||||||
private readonly Cluster _client;
|
private readonly Cluster _client;
|
||||||
private readonly IDiscordCache _cache;
|
private readonly IDiscordCache _cache;
|
||||||
private readonly Bot _bot;
|
private readonly Bot _bot;
|
||||||
|
private readonly DiscordApiClient _rest;
|
||||||
|
|
||||||
public MessageEdited(LastMessageCacheService lastMessageCache, ProxyService proxy, IDatabase db, IMetrics metrics, ModelRepository repo, Cluster client, IDiscordCache cache, Bot bot)
|
|
||||||
|
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;
|
||||||
@ -34,6 +37,7 @@ namespace PluralKit.Bot
|
|||||||
_client = client;
|
_client = client;
|
||||||
_cache = cache;
|
_cache = cache;
|
||||||
_bot = bot;
|
_bot = bot;
|
||||||
|
_rest = rest;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Handle(Shard shard, MessageUpdateEvent evt)
|
public async Task Handle(Shard shard, MessageUpdateEvent evt)
|
||||||
@ -48,9 +52,10 @@ namespace PluralKit.Bot
|
|||||||
if (channel.Type != Channel.ChannelType.GuildText)
|
if (channel.Type != Channel.ChannelType.GuildText)
|
||||||
return;
|
return;
|
||||||
var guild = _cache.GetGuild(channel.GuildId!.Value);
|
var guild = _cache.GetGuild(channel.GuildId!.Value);
|
||||||
|
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 (_lastMessageCache.GetLastMessage(evt.ChannelId) != evt.Id)
|
if (lastMessage?.mid != 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
|
||||||
@ -59,6 +64,8 @@ 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;
|
||||||
|
|
||||||
// TODO: is this missing anything?
|
// TODO: is this missing anything?
|
||||||
var equivalentEvt = new MessageCreateEvent
|
var equivalentEvt = new MessageCreateEvent
|
||||||
{
|
{
|
||||||
@ -68,7 +75,10 @@ namespace PluralKit.Bot
|
|||||||
Author = evt.Author.Value,
|
Author = evt.Author.Value,
|
||||||
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,
|
||||||
|
ReferencedMessage = referencedMessage,
|
||||||
|
Type = (lastMessage.referenced_message != null) ? Message.MessageType.Reply : Message.MessageType.Default,
|
||||||
};
|
};
|
||||||
var botPermissions = _bot.PermissionsIn(channel.Id);
|
var botPermissions = _bot.PermissionsIn(channel.Id);
|
||||||
await _proxy.HandleIncomingMessage(shard, equivalentEvt, ctx, allowAutoproxy: false, guild: guild, channel: channel, botPermissions: botPermissions);
|
await _proxy.HandleIncomingMessage(shard, equivalentEvt, ctx, allowAutoproxy: false, guild: guild, channel: channel, botPermissions: botPermissions);
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
using Myriad.Types;
|
||||||
|
|
||||||
namespace PluralKit.Bot
|
namespace PluralKit.Bot
|
||||||
{
|
{
|
||||||
// Doing things like this instead of enabling D.NET's message cache because the message cache is, let's face it,
|
// Doing things like this instead of enabling D.NET's message cache because the message cache is, let's face it,
|
||||||
@ -10,17 +12,30 @@ namespace PluralKit.Bot
|
|||||||
// TODO: is this still needed after the D#+ migration?
|
// TODO: is this still needed after the D#+ migration?
|
||||||
public class LastMessageCacheService
|
public class LastMessageCacheService
|
||||||
{
|
{
|
||||||
private readonly IDictionary<ulong, ulong> _cache = new ConcurrentDictionary<ulong, ulong>();
|
private readonly IDictionary<ulong, CachedMessage> _cache = new ConcurrentDictionary<ulong, CachedMessage>();
|
||||||
|
|
||||||
public void AddMessage(ulong channel, ulong message)
|
public void AddMessage(Message msg)
|
||||||
{
|
{
|
||||||
_cache[channel] = message;
|
_cache[msg.ChannelId] = new CachedMessage(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ulong? GetLastMessage(ulong channel)
|
public CachedMessage GetLastMessage(ulong channel)
|
||||||
{
|
{
|
||||||
if (_cache.TryGetValue(channel, out var message)) return message;
|
if (_cache.TryGetValue(channel, out var message)) return message;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class CachedMessage
|
||||||
|
{
|
||||||
|
public ulong mid;
|
||||||
|
public ulong? referenced_message;
|
||||||
|
|
||||||
|
public CachedMessage(Message msg)
|
||||||
|
{
|
||||||
|
mid = msg.Id;
|
||||||
|
if (msg.ReferencedMessage.HasValue)
|
||||||
|
referenced_message = msg.ReferencedMessage.Value.Id;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user