PluralKit/PluralKit.Bot/Services/CommandMessageService.cs

45 lines
1.5 KiB
C#
Raw Permalink Normal View History

2020-10-23 12:18:28 +02:00
using NodaTime;
using PluralKit.Core;
using Serilog;
namespace PluralKit.Bot;
2021-08-27 11:03:47 -04:00
public class CommandMessageService
{
private readonly RedisService _redis;
2025-06-08 19:52:37 +00:00
private readonly ModelRepository _repo;
private readonly ILogger _logger;
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)
{
_redis = redis;
2025-06-08 19:52:37 +00:00
_repo = repo;
_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)
{
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);
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]));
}
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;
}
2024-09-14 12:19:47 +09:00
}
public record CommandMessage(ulong AuthorId, ulong ChannelId, ulong GuildId);