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;
|
2022-12-29 01:34:50 +00:00
|
|
|
using Myriad.Rest.Types.Requests;
|
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;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Bot;
|
2020-05-01 23:52:52 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public class MessageEdited: IEventHandler<MessageUpdateEvent>
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly Bot _bot;
|
2022-09-06 09:52:37 +00:00
|
|
|
private readonly BotConfig _config;
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly IDiscordCache _cache;
|
|
|
|
private readonly Cluster _client;
|
|
|
|
private readonly IDatabase _db;
|
|
|
|
private readonly LastMessageCacheService _lastMessageCache;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly IMetrics _metrics;
|
|
|
|
private readonly ProxyService _proxy;
|
|
|
|
private readonly ModelRepository _repo;
|
|
|
|
private readonly DiscordApiClient _rest;
|
|
|
|
|
|
|
|
public MessageEdited(LastMessageCacheService lastMessageCache, ProxyService proxy, IDatabase db,
|
|
|
|
IMetrics metrics, ModelRepository repo, Cluster client, IDiscordCache cache, Bot bot,
|
2022-09-06 09:52:37 +00:00
|
|
|
BotConfig config, DiscordApiClient rest, ILogger logger)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
_lastMessageCache = lastMessageCache;
|
|
|
|
_proxy = proxy;
|
|
|
|
_db = db;
|
|
|
|
_metrics = metrics;
|
|
|
|
_repo = repo;
|
|
|
|
_client = client;
|
|
|
|
_cache = cache;
|
|
|
|
_bot = bot;
|
2022-09-06 09:52:37 +00:00
|
|
|
_config = config;
|
2021-11-27 02:10:56 +00:00
|
|
|
_rest = rest;
|
|
|
|
_logger = logger.ForContext<MessageEdited>();
|
|
|
|
}
|
|
|
|
|
2022-01-14 23:39:03 +00:00
|
|
|
public async Task Handle(int shardId, MessageUpdateEvent evt)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
2022-09-06 09:52:37 +00:00
|
|
|
if (evt.Author.Value?.Id == _config.ClientId) return;
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
// Edit message events sometimes arrive with missing data; double-check it's all there
|
|
|
|
if (!evt.Content.HasValue || !evt.Author.HasValue || !evt.Member.HasValue)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var channel = await _cache.GetChannel(evt.ChannelId);
|
|
|
|
if (!DiscordUtils.IsValidGuildChannel(channel))
|
|
|
|
return;
|
2023-05-10 01:16:16 +00:00
|
|
|
var rootChannel = await _cache.GetRootChannel(channel.Id);
|
2021-11-27 02:10:56 +00:00
|
|
|
var guild = await _cache.GetGuild(channel.GuildId!.Value);
|
|
|
|
var lastMessage = _lastMessageCache.GetLastMessage(evt.ChannelId)?.Current;
|
|
|
|
|
|
|
|
// Only react to the last message in the channel
|
|
|
|
if (lastMessage?.Id != evt.Id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Just run the normal message handling code, with a flag to disable autoproxying
|
|
|
|
MessageContext ctx;
|
|
|
|
using (_metrics.Measure.Timer.Time(BotMetrics.MessageContextQueryTime))
|
2023-05-10 01:16:16 +00:00
|
|
|
ctx = await _repo.GetMessageContext(evt.Author.Value!.Id, channel.GuildId!.Value, rootChannel.Id, evt.ChannelId);
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
var equivalentEvt = await GetMessageCreateEvent(evt, lastMessage, channel);
|
|
|
|
var botPermissions = await _cache.PermissionsIn(channel.Id);
|
2020-05-01 23:52:52 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
try
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2022-01-14 23:39:03 +00:00
|
|
|
await _proxy.HandleIncomingMessage(equivalentEvt, ctx, allowAutoproxy: false, guild: guild,
|
2021-11-27 02:10:56 +00:00
|
|
|
channel: channel, botPermissions: botPermissions);
|
2021-05-03 08:29:22 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
// Catch any failed proxy checks so they get ignored in the global error handler
|
|
|
|
catch (ProxyService.ProxyChecksFailedException) { }
|
2022-12-29 01:34:50 +00:00
|
|
|
|
|
|
|
catch (PKError e)
|
|
|
|
{
|
|
|
|
// User-facing errors, print to the channel properly formatted
|
|
|
|
if (botPermissions.HasFlag(PermissionSet.SendMessages))
|
|
|
|
await _rest.CreateMessage(evt.ChannelId,
|
|
|
|
new MessageRequest { Content = $"{Emojis.Error} {e.Message}" });
|
|
|
|
}
|
2021-11-27 02:10:56 +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;
|
|
|
|
|
|
|
|
var messageType = lastMessage.ReferencedMessage != null
|
|
|
|
? Message.MessageType.Reply
|
|
|
|
: Message.MessageType.Default;
|
2021-03-22 07:17:10 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// TODO: is this missing anything?
|
|
|
|
var equivalentEvt = new MessageCreateEvent
|
2021-05-03 08:29:22 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
Id = evt.Id,
|
|
|
|
ChannelId = evt.ChannelId,
|
|
|
|
GuildId = channel.GuildId,
|
|
|
|
Author = evt.Author.Value,
|
|
|
|
Member = evt.Member.Value,
|
|
|
|
Content = evt.Content.Value,
|
|
|
|
Attachments = evt.Attachments.Value ?? Array.Empty<Message.Attachment>(),
|
|
|
|
MessageReference = messageReference,
|
|
|
|
ReferencedMessage = referencedMessage,
|
|
|
|
Type = messageType,
|
|
|
|
};
|
|
|
|
return equivalentEvt;
|
|
|
|
}
|
2021-05-03 08:29:22 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private async Task<Message?> GetReferencedMessage(ulong channelId, ulong? referencedMessageId)
|
|
|
|
{
|
|
|
|
if (referencedMessageId == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
var botPermissions = await _cache.PermissionsIn(channelId);
|
|
|
|
if (!botPermissions.HasFlag(PermissionSet.ReadMessageHistory))
|
2021-05-03 08:29:22 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
_logger.Warning(
|
|
|
|
"Tried to get referenced message in channel {ChannelId} to reply but bot does not have Read Message History",
|
|
|
|
channelId
|
|
|
|
);
|
|
|
|
return null;
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
return await _rest.GetMessage(channelId, referencedMessageId.Value);
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
}
|