2020-05-01 23:52:52 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
using DSharpPlus.EventArgs;
|
|
|
|
|
2020-06-11 21:20:46 +00:00
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
|
|
using Serilog;
|
2020-05-01 23:52:52 +00:00
|
|
|
|
|
|
|
namespace PluralKit.Bot
|
|
|
|
{
|
|
|
|
// Double duty :)
|
|
|
|
public class MessageDeleted: IEventHandler<MessageDeleteEventArgs>, IEventHandler<MessageBulkDeleteEventArgs>
|
|
|
|
{
|
2020-06-11 21:20:46 +00:00
|
|
|
private readonly IDataStore _data;
|
|
|
|
private readonly ILogger _logger;
|
2020-05-01 23:52:52 +00:00
|
|
|
|
2020-06-11 21:20:46 +00:00
|
|
|
public MessageDeleted(IDataStore data, ILogger logger)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2020-06-11 21:20:46 +00:00
|
|
|
_data = data;
|
|
|
|
_logger = logger.ForContext<MessageDeleted>();
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 21:20:46 +00:00
|
|
|
public async Task Handle(MessageDeleteEventArgs evt)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2020-06-11 21:20:46 +00:00
|
|
|
// Delete deleted webhook messages from the data store
|
|
|
|
// (if we don't know whether it's a webhook, delete it just to be safe)
|
|
|
|
if (!evt.Message.WebhookMessage) return;
|
|
|
|
await _data.DeleteMessage(evt.Message.Id);
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 21:20:46 +00:00
|
|
|
public async Task Handle(MessageBulkDeleteEventArgs evt)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2020-06-11 21:20:46 +00:00
|
|
|
// Same as above, but bulk
|
|
|
|
_logger.Information("Bulk deleting {Count} messages in channel {Channel}", evt.Messages.Count, evt.Channel.Id);
|
|
|
|
await _data.DeleteMessagesBulk(evt.Messages.Select(m => m.Id).ToList());
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|