2021-01-31 13:42:28 +00:00
|
|
|
using System;
|
2020-05-01 23:52:52 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
2020-06-14 20:19:12 +00:00
|
|
|
using App.Metrics;
|
|
|
|
|
2021-01-31 13:42:28 +00:00
|
|
|
using Myriad.Cache;
|
|
|
|
using Myriad.Extensions;
|
2020-12-22 12:15:26 +00:00
|
|
|
using Myriad.Gateway;
|
2021-03-22 07:07:33 +00:00
|
|
|
using Myriad.Rest;
|
2021-01-31 13:42:28 +00:00
|
|
|
using Myriad.Types;
|
2020-05-01 23:52:52 +00:00
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
2021-05-03 08:29:22 +00:00
|
|
|
using Serilog;
|
|
|
|
|
2020-05-01 23:52:52 +00:00
|
|
|
|
|
|
|
namespace PluralKit.Bot
|
|
|
|
{
|
2020-12-22 12:15:26 +00:00
|
|
|
public class MessageEdited: IEventHandler<MessageUpdateEvent>
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
|
|
|
private readonly LastMessageCacheService _lastMessageCache;
|
|
|
|
private readonly ProxyService _proxy;
|
2020-06-13 17:36:43 +00:00
|
|
|
private readonly IDatabase _db;
|
2020-08-29 11:46:27 +00:00
|
|
|
private readonly ModelRepository _repo;
|
2020-06-14 20:19:12 +00:00
|
|
|
private readonly IMetrics _metrics;
|
2021-01-31 13:42:28 +00:00
|
|
|
private readonly Cluster _client;
|
|
|
|
private readonly IDiscordCache _cache;
|
|
|
|
private readonly Bot _bot;
|
2021-03-22 07:07:33 +00:00
|
|
|
private readonly DiscordApiClient _rest;
|
2021-05-03 08:29:22 +00:00
|
|
|
private readonly ILogger _logger;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-05-03 08:29:22 +00:00
|
|
|
public MessageEdited(LastMessageCacheService lastMessageCache, ProxyService proxy, IDatabase db, IMetrics metrics, ModelRepository repo, Cluster client, IDiscordCache cache, Bot bot, DiscordApiClient rest, ILogger logger)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
|
|
|
_lastMessageCache = lastMessageCache;
|
|
|
|
_proxy = proxy;
|
2020-06-12 21:13:21 +00:00
|
|
|
_db = db;
|
2020-06-14 20:19:12 +00:00
|
|
|
_metrics = metrics;
|
2020-08-29 11:46:27 +00:00
|
|
|
_repo = repo;
|
2020-09-20 19:49:52 +00:00
|
|
|
_client = client;
|
2021-01-31 13:42:28 +00:00
|
|
|
_cache = cache;
|
|
|
|
_bot = bot;
|
2021-03-22 07:07:33 +00:00
|
|
|
_rest = rest;
|
2021-05-03 08:29:22 +00:00
|
|
|
_logger = logger.ForContext<MessageEdited>();
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
public async Task Handle(Shard shard, MessageUpdateEvent evt)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2021-11-22 00:42:35 +00:00
|
|
|
if (evt.Author.Value?.Id == await _cache.GetOwnUser()) return;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-01-31 13:42:28 +00:00
|
|
|
// Edit message events sometimes arrive with missing data; double-check it's all there
|
2021-08-27 15:03:47 +00:00
|
|
|
if (!evt.Content.HasValue || !evt.Author.HasValue || !evt.Member.HasValue)
|
2021-01-31 13:42:28 +00:00
|
|
|
return;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-18 01:41:02 +00:00
|
|
|
var channel = await _cache.GetChannel(evt.ChannelId);
|
2021-07-08 16:45:59 +00:00
|
|
|
if (!DiscordUtils.IsValidGuildChannel(channel))
|
2021-01-31 13:42:28 +00:00
|
|
|
return;
|
2021-11-18 01:41:02 +00:00
|
|
|
var guild = await _cache.GetGuild(channel.GuildId!.Value);
|
2021-08-08 19:56:24 +00:00
|
|
|
var lastMessage = _lastMessageCache.GetLastMessage(evt.ChannelId)?.Current;
|
2021-01-31 13:42:28 +00:00
|
|
|
|
|
|
|
// Only react to the last message in the channel
|
2021-05-03 08:29:22 +00:00
|
|
|
if (lastMessage?.Id != evt.Id)
|
2021-01-31 13:42:28 +00:00
|
|
|
return;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-01-31 13:42:28 +00:00
|
|
|
// Just run the normal message handling code, with a flag to disable autoproxying
|
|
|
|
MessageContext ctx;
|
|
|
|
using (_metrics.Measure.Timer.Time(BotMetrics.MessageContextQueryTime))
|
2021-09-30 01:51:38 +00:00
|
|
|
ctx = await _repo.GetMessageContext(evt.Author.Value!.Id, channel.GuildId!.Value, evt.ChannelId);
|
2021-01-31 13:42:28 +00:00
|
|
|
|
2021-05-03 08:29:22 +00:00
|
|
|
var equivalentEvt = await GetMessageCreateEvent(evt, lastMessage, channel);
|
2021-11-22 00:42:35 +00:00
|
|
|
var botPermissions = await _cache.PermissionsIn(channel.Id);
|
2021-08-12 00:52:29 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await _proxy.HandleIncomingMessage(shard, equivalentEvt, ctx, allowAutoproxy: false, guild: guild,
|
|
|
|
channel: channel, botPermissions: botPermissions);
|
|
|
|
}
|
|
|
|
// Catch any failed proxy checks so they get ignored in the global error handler
|
2021-08-27 15:03:47 +00:00
|
|
|
catch (ProxyService.ProxyChecksFailedException) { }
|
2021-05-03 08:29:22 +00:00
|
|
|
}
|
2021-03-22 07:17:10 +00:00
|
|
|
|
2021-05-03 08:29:22 +00:00
|
|
|
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;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
|
|
|
var messageType = lastMessage.ReferencedMessage != null
|
|
|
|
? Message.MessageType.Reply
|
2021-05-03 08:29:22 +00:00
|
|
|
: Message.MessageType.Default;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-01-31 13:42:28 +00:00
|
|
|
// TODO: is this missing anything?
|
|
|
|
var equivalentEvt = new MessageCreateEvent
|
|
|
|
{
|
|
|
|
Id = evt.Id,
|
|
|
|
ChannelId = evt.ChannelId,
|
|
|
|
GuildId = channel.GuildId,
|
|
|
|
Author = evt.Author.Value,
|
|
|
|
Member = evt.Member.Value,
|
|
|
|
Content = evt.Content.Value,
|
2021-03-22 07:07:33 +00:00
|
|
|
Attachments = evt.Attachments.Value ?? Array.Empty<Message.Attachment>(),
|
2021-05-03 08:29:22 +00:00
|
|
|
MessageReference = messageReference,
|
2021-03-22 07:17:10 +00:00
|
|
|
ReferencedMessage = referencedMessage,
|
2021-05-03 08:29:22 +00:00
|
|
|
Type = messageType,
|
2021-01-31 13:42:28 +00:00
|
|
|
};
|
2021-05-03 08:29:22 +00:00
|
|
|
return equivalentEvt;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<Message?> GetReferencedMessage(ulong channelId, ulong? referencedMessageId)
|
|
|
|
{
|
|
|
|
if (referencedMessageId == null)
|
|
|
|
return null;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-22 00:42:35 +00:00
|
|
|
var botPermissions = await _cache.PermissionsIn(channelId);
|
2021-05-03 08:29:22 +00:00
|
|
|
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);
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|