feat(bot): lookup command messages

This commit is contained in:
alyssa 2025-06-08 19:52:37 +00:00
parent 301bf25d44
commit 539fc82342
6 changed files with 120 additions and 37 deletions

View file

@ -9,29 +9,23 @@ namespace PluralKit.Bot;
public class CommandMessageService
{
private readonly RedisService _redis;
private readonly ModelRepository _repo;
private readonly ILogger _logger;
private static readonly TimeSpan CommandMessageRetention = TimeSpan.FromHours(24);
public CommandMessageService(RedisService redis, IClock clock, ILogger logger)
public CommandMessageService(RedisService redis, ModelRepository repo, IClock clock, ILogger logger)
{
_redis = redis;
_repo = repo;
_logger = logger.ForContext<CommandMessageService>();
}
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
);
await _redis.Connection.GetDatabase().StringSetAsync("command_message:" + messageId.ToString(), $"{authorId}-{channelId}-{guildId}", expiry: CommandMessageRetention);
}
public async Task<CommandMessage?> GetCommandMessage(ulong messageId)
{
var repoMsg = await _repo.GetCommandMessage(messageId);
if (repoMsg != null)
return new CommandMessage(repoMsg.Sender, repoMsg.Channel, repoMsg.Guild);
var str = await _redis.Connection.GetDatabase().StringGetAsync(messageId.ToString());
if (str.HasValue)
{

View file

@ -420,6 +420,24 @@ public class EmbedService
return eb.Build();
}
public async Task<Embed> CreateCommandMessageInfoEmbed(Core.CommandMessage msg, bool showContent)
{
var content = "*(command message deleted or inaccessible)*";
if (showContent)
{
var discordMessage = await _rest.GetMessageOrNull(msg.Channel, msg.OriginalMid);
if (discordMessage != null)
content = discordMessage.Content;
}
return new EmbedBuilder()
.Title("Command response message")
.Description(content)
.Field(new("Original message", $"https://discord.com/channels/{msg.Guild}/{msg.Channel}/{msg.OriginalMid}", true))
.Field(new("Sent by", $"<@{msg.Sender}>", true))
.Build();
}
public Task<Embed> CreateFrontPercentEmbed(FrontBreakdown breakdown, PKSystem system, PKGroup group,
DateTimeZone tz, LookupContext ctx, string embedTitle,
bool ignoreNoFronters, bool showFlat)