2020-10-23 12:18:28 +02:00
|
|
|
using NodaTime;
|
|
|
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
|
|
|
|
using Serilog;
|
|
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
namespace PluralKit.Bot;
|
2021-08-27 11:03:47 -04:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
public class CommandMessageService
|
|
|
|
|
{
|
2022-06-19 20:28:55 -04:00
|
|
|
private readonly RedisService _redis;
|
2025-06-08 19:52:37 +00:00
|
|
|
private readonly ModelRepository _repo;
|
2021-11-26 21:10:56 -05:00
|
|
|
private readonly ILogger _logger;
|
2022-06-19 20:28:55 -04:00
|
|
|
private static readonly TimeSpan CommandMessageRetention = TimeSpan.FromHours(24);
|
2020-10-23 12:18:28 +02:00
|
|
|
|
2025-06-08 19:52:37 +00:00
|
|
|
public CommandMessageService(RedisService redis, ModelRepository repo, IClock clock, ILogger logger)
|
2021-11-26 21:10:56 -05:00
|
|
|
{
|
2022-06-19 20:28:55 -04:00
|
|
|
_redis = redis;
|
2025-06-08 19:52:37 +00:00
|
|
|
_repo = repo;
|
2021-11-26 21:10:56 -05:00
|
|
|
_logger = logger.ForContext<CommandMessageService>();
|
|
|
|
|
}
|
2020-10-23 12:18:28 +02:00
|
|
|
|
2024-09-14 12:19:47 +09:00
|
|
|
public async Task<CommandMessage?> GetCommandMessage(ulong messageId)
|
2022-06-19 20:28:55 -04:00
|
|
|
{
|
2025-06-08 19:52:37 +00:00
|
|
|
var repoMsg = await _repo.GetCommandMessage(messageId);
|
|
|
|
|
if (repoMsg != null)
|
|
|
|
|
return new CommandMessage(repoMsg.Sender, repoMsg.Channel, repoMsg.Guild);
|
|
|
|
|
|
2022-06-19 20:28:55 -04:00
|
|
|
var str = await _redis.Connection.GetDatabase().StringGetAsync(messageId.ToString());
|
|
|
|
|
if (str.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var split = ((string)str).Split("-");
|
2024-09-14 12:19:47 +09:00
|
|
|
return new CommandMessage(ulong.Parse(split[0]), ulong.Parse(split[1]), ulong.Parse(split[2]));
|
2022-06-19 20:28:55 -04:00
|
|
|
}
|
2025-03-29 12:20:11 +00:00
|
|
|
str = await _redis.Connection.GetDatabase().StringGetAsync("command_message:" + messageId.ToString());
|
|
|
|
|
if (str.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var split = ((string)str).Split("-");
|
|
|
|
|
return new CommandMessage(ulong.Parse(split[0]), ulong.Parse(split[1]), ulong.Parse(split[2]));
|
|
|
|
|
}
|
2024-09-14 12:19:47 +09:00
|
|
|
return null;
|
2022-06-19 20:28:55 -04:00
|
|
|
}
|
2024-09-14 12:19:47 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public record CommandMessage(ulong AuthorId, ulong ChannelId, ulong GuildId);
|