using System.Threading.Tasks; using NodaTime; using PluralKit.Core; using Serilog; namespace PluralKit.Bot { public class CommandMessageService { private static readonly Duration CommandMessageRetention = Duration.FromHours(24); private readonly IDatabase _db; private readonly ModelRepository _repo; private readonly IClock _clock; private readonly ILogger _logger; public CommandMessageService(IDatabase db, ModelRepository repo, IClock clock, ILogger logger) { _db = db; _repo = repo; _clock = clock; _logger = logger.ForContext(); } public async Task RegisterMessage(ulong messageId, ulong channelId, ulong authorId) { _logger.Debug("Registering command response {MessageId} from author {AuthorId} in {ChannelId}", messageId, authorId, channelId); await _repo.SaveCommandMessage(messageId, channelId, authorId); } public async Task GetCommandMessage(ulong messageId) { return await _repo.GetCommandMessage(messageId); } public async Task CleanupOldMessages() { var deleteThresholdInstant = _clock.GetCurrentInstant() - CommandMessageRetention; var deleteThresholdSnowflake = DiscordUtils.InstantToSnowflake(deleteThresholdInstant); var deletedRows = await _repo.DeleteCommandMessagesBefore(deleteThresholdSnowflake); _logger.Information("Pruned {DeletedRows} command messages older than retention {Retention} (older than {DeleteThresholdInstant} / {DeleteThresholdSnowflake})", deletedRows, CommandMessageRetention, deleteThresholdInstant, deleteThresholdSnowflake); } } }