2020-05-01 23:52:52 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
2020-11-15 12:53:31 +00:00
|
|
|
using DSharpPlus;
|
2020-05-01 23:52:52 +00:00
|
|
|
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-08-29 11:46:27 +00:00
|
|
|
private readonly IDatabase _db;
|
|
|
|
private readonly ModelRepository _repo;
|
2020-06-11 21:20:46 +00:00
|
|
|
private readonly ILogger _logger;
|
2020-05-01 23:52:52 +00:00
|
|
|
|
2020-08-29 11:46:27 +00:00
|
|
|
public MessageDeleted(ILogger logger, IDatabase db, ModelRepository repo)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2020-08-29 11:46:27 +00:00
|
|
|
_db = db;
|
|
|
|
_repo = repo;
|
2020-06-11 21:20:46 +00:00
|
|
|
_logger = logger.ForContext<MessageDeleted>();
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 12:53:31 +00:00
|
|
|
public async Task Handle(DiscordClient shard, 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
|
2020-08-29 11:46:27 +00:00
|
|
|
// Most of the data in the given message is wrong/missing, so always delete just to be sure.
|
|
|
|
await _db.Execute(c => _repo.DeleteMessage(c, evt.Message.Id));
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 12:53:31 +00:00
|
|
|
public async Task Handle(DiscordClient shard, 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);
|
2020-08-29 11:46:27 +00:00
|
|
|
await _db.Execute(c => _repo.DeleteMessagesBulk(c, evt.Messages.Select(m => m.Id).ToList()));
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|