PluralKit/PluralKit.Bot/Services/CommandMessageService.cs

43 lines
1.3 KiB
C#
Raw Normal View History

2020-10-23 10:18:28 +00:00
using NodaTime;
using PluralKit.Core;
using Serilog;
namespace PluralKit.Bot;
2021-08-27 15:03:47 +00:00
public class CommandMessageService
{
private readonly RedisService _redis;
private readonly ILogger _logger;
private static readonly TimeSpan CommandMessageRetention = TimeSpan.FromHours(24);
2020-10-23 10:18:28 +00:00
public CommandMessageService(RedisService redis, IClock clock, ILogger logger)
{
_redis = redis;
_logger = logger.ForContext<CommandMessageService>();
}
2020-10-23 10:18:28 +00:00
public async Task RegisterMessage(ulong messageId, ulong channelId, ulong authorId)
{
if (_redis.Connection == null) return;
_logger.Debug(
"Registering command response {MessageId} from author {AuthorId} in {ChannelId}",
messageId, authorId, channelId
);
await _redis.Connection.GetDatabase().StringSetAsync(messageId.ToString(), $"{authorId}-{channelId}", expiry: CommandMessageRetention);
2020-10-23 10:18:28 +00:00
}
public async Task<(ulong?, ulong?)> GetCommandMessage(ulong messageId)
{
var str = await _redis.Connection.GetDatabase().StringGetAsync(messageId.ToString());
if (str.HasValue)
{
var split = ((string)str).Split("-");
return (ulong.Parse(split[0]), ulong.Parse(split[1]));
}
return (null, null);
}
2020-10-23 10:18:28 +00:00
}