feat(bot): store command message info in redis

This commit is contained in:
spiral
2022-06-19 20:28:55 -04:00
parent 33b77470ee
commit 5c055871e3
6 changed files with 31 additions and 75 deletions

View File

@@ -8,16 +8,13 @@ namespace PluralKit.Bot;
public class CommandMessageService
{
private readonly IClock _clock;
private readonly IDatabase _db;
private readonly RedisService _redis;
private readonly ILogger _logger;
private readonly ModelRepository _repo;
private static readonly TimeSpan CommandMessageRetention = TimeSpan.FromHours(24);
public CommandMessageService(IDatabase db, ModelRepository repo, IClock clock, ILogger logger)
public CommandMessageService(RedisService redis, IClock clock, ILogger logger)
{
_db = db;
_repo = repo;
_clock = clock;
_redis = redis;
_logger = logger.ForContext<CommandMessageService>();
}
@@ -27,9 +24,18 @@ public class CommandMessageService
"Registering command response {MessageId} from author {AuthorId} in {ChannelId}",
messageId, authorId, channelId
);
await _repo.SaveCommandMessage(messageId, channelId, authorId);
await _redis.Connection.GetDatabase().StringSetAsync(messageId.ToString(), $"{authorId}-{channelId}", expiry: CommandMessageRetention);
}
public async Task<CommandMessage?> GetCommandMessage(ulong messageId) =>
await _repo.GetCommandMessage(messageId);
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);
}
}