PluralKit/PluralKit.Bot/Services/CommandMessageService.cs

50 lines
1.8 KiB
C#
Raw Normal View History

2021-08-27 15:03:47 +00:00
using System.Threading.Tasks;
2020-10-23 10:18:28 +00:00
using NodaTime;
using PluralKit.Core;
using Serilog;
namespace PluralKit.Bot
{
public class CommandMessageService
{
private static readonly Duration CommandMessageRetention = Duration.FromHours(2);
2021-08-27 15:03:47 +00:00
2020-10-23 10:18:28 +00:00
private readonly IDatabase _db;
private readonly ModelRepository _repo;
private readonly IClock _clock;
private readonly ILogger _logger;
2021-08-27 15:03:47 +00:00
2020-10-23 10:18:28 +00:00
public CommandMessageService(IDatabase db, ModelRepository repo, IClock clock, ILogger logger)
{
_db = db;
_repo = repo;
_clock = clock;
2021-06-10 12:21:05 +00:00
_logger = logger.ForContext<CommandMessageService>();
2020-10-23 10:18:28 +00:00
}
public async Task RegisterMessage(ulong messageId, ulong authorId)
{
_logger.Debug("Registering command response {MessageId} from author {AuthorId}", messageId, authorId);
await _db.Execute(conn => _repo.SaveCommandMessage(conn, messageId, authorId));
}
2021-09-03 20:20:07 +00:00
public async Task<CommandMessage?> GetCommandMessage(IPKConnection conn, ulong messageId)
2020-10-23 10:18:28 +00:00
{
return await _repo.GetCommandMessage(conn, messageId);
}
public async Task CleanupOldMessages()
{
var deleteThresholdInstant = _clock.GetCurrentInstant() - CommandMessageRetention;
var deleteThresholdSnowflake = DiscordUtils.InstantToSnowflake(deleteThresholdInstant);
var deletedRows = await _db.Execute(conn => _repo.DeleteCommandMessagesBefore(conn, deleteThresholdSnowflake));
2021-08-27 15:03:47 +00:00
2020-10-23 10:18:28 +00:00
_logger.Information("Pruned {DeletedRows} command messages older than retention {Retention} (older than {DeleteThresholdInstant} / {DeleteThresholdSnowflake})",
deletedRows, CommandMessageRetention, deleteThresholdInstant, deleteThresholdSnowflake);
}
}
}