PluralKit/PluralKit.Bot/Services/CommandMessageService.cs

51 lines
1.7 KiB
C#
Raw 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;
private readonly ILogger _logger;
private static readonly TimeSpan CommandMessageRetention = TimeSpan.FromHours(24);
2020-10-23 12:18:28 +02:00
public CommandMessageService(RedisService redis, IClock clock, ILogger logger)
{
_redis = redis;
_logger = logger.ForContext<CommandMessageService>();
}
2020-10-23 12:18:28 +02:00
2024-09-14 12:19:47 +09:00
public async Task RegisterMessage(ulong messageId, ulong guildId, ulong channelId, ulong authorId)
{
if (_redis.Connection == null) return;
_logger.Debug(
"Registering command response {MessageId} from author {AuthorId} in {ChannelId}",
messageId, authorId, channelId
);
2025-03-29 12:20:11 +00:00
await _redis.Connection.GetDatabase().StringSetAsync("command_message:" + messageId.ToString(), $"{authorId}-{channelId}-{guildId}", expiry: CommandMessageRetention);
2020-10-23 12:18:28 +02:00
}
2024-09-14 12:19:47 +09:00
public async Task<CommandMessage?> GetCommandMessage(ulong messageId)
{
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);