2020-05-01 23:52:52 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
using DSharpPlus.EventArgs;
|
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PluralKit.Bot
|
|
|
|
{
|
|
|
|
public class MessageEdited: IEventHandler<MessageUpdateEventArgs>
|
|
|
|
{
|
|
|
|
private readonly LastMessageCacheService _lastMessageCache;
|
|
|
|
private readonly ProxyService _proxy;
|
2020-06-12 21:13:21 +00:00
|
|
|
private readonly DbConnectionFactory _db;
|
2020-05-01 23:52:52 +00:00
|
|
|
|
2020-06-12 21:13:21 +00:00
|
|
|
public MessageEdited(LastMessageCacheService lastMessageCache, ProxyService proxy, DbConnectionFactory db)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
|
|
|
_lastMessageCache = lastMessageCache;
|
|
|
|
_proxy = proxy;
|
2020-06-12 21:13:21 +00:00
|
|
|
_db = db;
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Handle(MessageUpdateEventArgs evt)
|
|
|
|
{
|
2020-06-12 21:13:21 +00:00
|
|
|
// 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;
|
2020-05-01 23:52:52 +00:00
|
|
|
|
2020-06-12 21:13:21 +00:00
|
|
|
// Only react to the last message in the channel
|
2020-05-01 23:52:52 +00:00
|
|
|
if (_lastMessageCache.GetLastMessage(evt.Channel.Id) != evt.Message.Id) return;
|
|
|
|
|
2020-06-12 21:13:21 +00:00
|
|
|
// Just run the normal message handling code, with a flag to disable autoproxying
|
|
|
|
var ctx = await _db.Execute(c => c.QueryMessageContext(evt.Author.Id, evt.Channel.GuildId, evt.Channel.Id));
|
|
|
|
await _proxy.HandleIncomingMessage(evt.Message, ctx, allowAutoproxy: false);
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|