2020-10-23 10:18:28 +00:00
|
|
|
using NodaTime;
|
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
|
|
using Serilog;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Bot;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public class CommandMessageService
|
|
|
|
{
|
2022-06-20 00:28:55 +00:00
|
|
|
private readonly RedisService _redis;
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly ILogger _logger;
|
2022-06-20 00:28:55 +00:00
|
|
|
private static readonly TimeSpan CommandMessageRetention = TimeSpan.FromHours(24);
|
2020-10-23 10:18:28 +00:00
|
|
|
|
2022-06-20 00:28:55 +00:00
|
|
|
public CommandMessageService(RedisService redis, IClock clock, ILogger logger)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
2022-06-20 00:28:55 +00:00
|
|
|
_redis = redis;
|
2021-11-27 02:10:56 +00:00
|
|
|
_logger = logger.ForContext<CommandMessageService>();
|
|
|
|
}
|
2020-10-23 10:18:28 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task RegisterMessage(ulong messageId, ulong channelId, ulong authorId)
|
|
|
|
{
|
2022-07-16 22:30:05 +00:00
|
|
|
if (_redis.Connection == null) return;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
_logger.Debug(
|
|
|
|
"Registering command response {MessageId} from author {AuthorId} in {ChannelId}",
|
|
|
|
messageId, authorId, channelId
|
|
|
|
);
|
2022-06-20 00:28:55 +00:00
|
|
|
|
|
|
|
await _redis.Connection.GetDatabase().StringSetAsync(messageId.ToString(), $"{authorId}-{channelId}", expiry: CommandMessageRetention);
|
2020-10-23 10:18:28 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
|
2022-06-20 00:28:55 +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
|
|
|
}
|