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;
|
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
|
|
|
|
2022-06-19 20:28:55 -04:00
|
|
|
public CommandMessageService(RedisService redis, IClock clock, ILogger logger)
|
2021-11-26 21:10:56 -05:00
|
|
|
{
|
2022-06-19 20:28:55 -04:00
|
|
|
_redis = redis;
|
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 RegisterMessage(ulong messageId, ulong guildId, ulong channelId, ulong authorId)
|
2021-11-26 21:10:56 -05:00
|
|
|
{
|
2022-07-17 00:30:05 +02:00
|
|
|
if (_redis.Connection == null) return;
|
|
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
_logger.Debug(
|
|
|
|
|
"Registering command response {MessageId} from author {AuthorId} in {ChannelId}",
|
|
|
|
|
messageId, authorId, channelId
|
|
|
|
|
);
|
2022-06-19 20:28:55 -04:00
|
|
|
|
2024-09-14 12:19:47 +09:00
|
|
|
await _redis.Connection.GetDatabase().StringSetAsync(messageId.ToString(), $"{authorId}-{channelId}-{guildId}", expiry: CommandMessageRetention);
|
2020-10-23 12:18:28 +02:00
|
|
|
}
|
2021-11-26 21:10:56 -05:00
|
|
|
|
2024-09-14 12:19:47 +09:00
|
|
|
public async Task<CommandMessage?> GetCommandMessage(ulong messageId)
|
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
|
|
|
}
|
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);
|