Add a short delay when deleting DB messages

This commit is contained in:
Ske 2020-11-18 15:01:32 +01:00
parent aee91987c2
commit feebbf657d

View File

@ -1,3 +1,4 @@
using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -13,6 +14,8 @@ namespace PluralKit.Bot
// Double duty :) // Double duty :)
public class MessageDeleted: IEventHandler<MessageDeleteEventArgs>, IEventHandler<MessageBulkDeleteEventArgs> public class MessageDeleted: IEventHandler<MessageDeleteEventArgs>, IEventHandler<MessageBulkDeleteEventArgs>
{ {
private static readonly TimeSpan MessageDeleteDelay = TimeSpan.FromSeconds(15);
private readonly IDatabase _db; private readonly IDatabase _db;
private readonly ModelRepository _repo; private readonly ModelRepository _repo;
private readonly ILogger _logger; private readonly ILogger _logger;
@ -24,18 +27,35 @@ namespace PluralKit.Bot
_logger = logger.ForContext<MessageDeleted>(); _logger = logger.ForContext<MessageDeleted>();
} }
public async Task Handle(DiscordClient shard, MessageDeleteEventArgs evt) public Task Handle(DiscordClient shard, MessageDeleteEventArgs evt)
{ {
// Delete deleted webhook messages from the data store // Delete deleted webhook messages from the data store
// Most of the data in the given message is wrong/missing, so always delete just to be sure. // 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));
async Task Inner()
{
await Task.Delay(MessageDeleteDelay);
await _db.Execute(c => _repo.DeleteMessage(c, evt.Message.Id));
}
// Fork a task to delete the message after a short delay
// to allow for lookups to happen for a little while after deletion
_ = Inner();
return Task.CompletedTask;
} }
public async Task Handle(DiscordClient shard, MessageBulkDeleteEventArgs evt) public Task Handle(DiscordClient shard, MessageBulkDeleteEventArgs evt)
{ {
// Same as above, but bulk // Same as above, but bulk
_logger.Information("Bulk deleting {Count} messages in channel {Channel}", evt.Messages.Count, evt.Channel.Id); async Task Inner()
await _db.Execute(c => _repo.DeleteMessagesBulk(c, evt.Messages.Select(m => m.Id).ToList())); {
await Task.Delay(MessageDeleteDelay);
_logger.Information("Bulk deleting {Count} messages in channel {Channel}", evt.Messages.Count, evt.Channel.Id);
await _db.Execute(c => _repo.DeleteMessagesBulk(c, evt.Messages.Select(m => m.Id).ToList()));
}
_ = Inner();
return Task.CompletedTask;
} }
} }
} }