diff --git a/Myriad/Gateway/Events/MessageUpdateEvent.cs b/Myriad/Gateway/Events/MessageUpdateEvent.cs index 63b34c1d..0bd1293b 100644 --- a/Myriad/Gateway/Events/MessageUpdateEvent.cs +++ b/Myriad/Gateway/Events/MessageUpdateEvent.cs @@ -1,10 +1,14 @@ -using Myriad.Utils; +using Myriad.Types; +using Myriad.Utils; namespace Myriad.Gateway { public record MessageUpdateEvent(ulong Id, ulong ChannelId): IGatewayEvent { public Optional Content { get; init; } + public Optional Author { get; init; } + public Optional Member { get; init; } + public Optional Attachments { get; init; } // TODO: lots of partials } } \ No newline at end of file diff --git a/PluralKit.Bot/Handlers/MessageEdited.cs b/PluralKit.Bot/Handlers/MessageEdited.cs index a88e271f..a00f7b22 100644 --- a/PluralKit.Bot/Handlers/MessageEdited.cs +++ b/PluralKit.Bot/Handlers/MessageEdited.cs @@ -1,10 +1,12 @@ +using System; using System.Threading.Tasks; using App.Metrics; -using DSharpPlus; - +using Myriad.Cache; +using Myriad.Extensions; using Myriad.Gateway; +using Myriad.Types; using PluralKit.Core; @@ -18,9 +20,11 @@ namespace PluralKit.Bot private readonly IDatabase _db; private readonly ModelRepository _repo; private readonly IMetrics _metrics; - private readonly DiscordShardedClient _client; + private readonly Cluster _client; + private readonly IDiscordCache _cache; + private readonly Bot _bot; - public MessageEdited(LastMessageCacheService lastMessageCache, ProxyService proxy, IDatabase db, IMetrics metrics, ModelRepository repo, DiscordShardedClient client) + public MessageEdited(LastMessageCacheService lastMessageCache, ProxyService proxy, IDatabase db, IMetrics metrics, ModelRepository repo, Cluster client, IDiscordCache cache, Bot bot) { _lastMessageCache = lastMessageCache; _proxy = proxy; @@ -28,25 +32,46 @@ namespace PluralKit.Bot _metrics = metrics; _repo = repo; _client = client; + _cache = cache; + _bot = bot; } public async Task Handle(Shard shard, MessageUpdateEvent evt) { - // TODO: fix - // if (evt.Author?.Id == _client.CurrentUser?.Id) return; - // - // // Edit message events sometimes arrive with missing data; double-check it's all there - // if (evt.Message.Content == null || evt.Author == null || evt.Channel.Guild == null) return; - // - // // Only react to the last message in the channel - // if (_lastMessageCache.GetLastMessage(evt.Channel.Id) != evt.Message.Id) return; - // - // // Just run the normal message handling code, with a flag to disable autoproxying - // MessageContext ctx; - // await using (var conn = await _db.Obtain()) - // using (_metrics.Measure.Timer.Time(BotMetrics.MessageContextQueryTime)) - // ctx = await _repo.GetMessageContext(conn, evt.Author.Id, evt.Channel.GuildId, evt.Channel.Id); - // await _proxy.HandleIncomingMessage(shard, evt.Message, ctx, allowAutoproxy: false); + if (evt.Author.Value?.Id == _client.User?.Id) return; + + // 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 = _cache.GetChannel(evt.ChannelId); + if (channel.Type != Channel.ChannelType.GuildText) + return; + var guild = _cache.GetGuild(channel.GuildId!.Value); + + // Only react to the last message in the channel + if (_lastMessageCache.GetLastMessage(evt.ChannelId) != evt.Id) + return; + + // Just run the normal message handling code, with a flag to disable autoproxying + MessageContext ctx; + await using (var conn = await _db.Obtain()) + using (_metrics.Measure.Timer.Time(BotMetrics.MessageContextQueryTime)) + ctx = await _repo.GetMessageContext(conn, evt.Author.Value!.Id, channel.GuildId!.Value, evt.ChannelId); + + // 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, + Attachments = evt.Attachments.Value ?? Array.Empty() + }; + var botPermissions = _bot.PermissionsIn(channel.Id); + await _proxy.HandleIncomingMessage(shard, equivalentEvt, ctx, allowAutoproxy: false, guild: guild, channel: channel, botPermissions: botPermissions); } } } \ No newline at end of file