在消息中执行 Button 时,如何获取触发此消息的 SlashCommand 的参数?

When executing a Button in a message, how to get arguments of a SlashCommand that triggered this message?

想象一个 SlashCommand /gallery san_francisco,它以一个简单的嵌入响应,显示来自某个网​​络 API 的带有 san_francisco 标记的随机图像,以及一个标题为 “另一个”的按钮一个!”san_francisco部分是用户提供的字符串参数。

该按钮触发了一个 ComponentInteraction,它应该用另一个标记为 san_francisco 的随机图像替换 Embed。

是否可以在 ComponentInteraction 处理程序中获取 /gallery san_francisco 命令的原始参数?

示例代码:

public class GalleryModule : InteractionModuleBase<SocketInteractionContext>
{
    [SlashCommand("Gallery", "Start an image gallery of specified subject")]
    public async Task Gallery(string tag)
    {
        await RespondAsync(
            $"Pictures of {tag}:",
            embed: _galleryService.GetRandomImage(tag),
            components: new ComponentBuilder().WithButton("Another one!", "next-image").Build());
    }

    [ComponentInteraction("next-image")]
    public async Task ShowNextImage()
    {
        var interaction = ((SocketMessageComponent)Context.Interaction;
        var originalSlashInteraction = interaction.Message.Interaction;

        var galleryTag = originalSlashInteraction[...?];

        await interaction.UpdateAsync(mp =>
        {
            mp.Content = $"Another picture of {galleryTag}:";
            mp.Embed = _galleryService.GetRandomImage(galleryTag);
            mp.Components = new ComponentBuilder().WithButton("Another one!", "next-image").Build();
        });
    }
}

originalSlashInteraction 是类型 MessageInteraction<SocketUser>,它只有属性 IdTypeNameUser,但没有没有用于存储参数的 属性。

我考虑过将原始的 san_francisco 斜杠参数存储在 Button 的自定义 ID 中(例如 next-image san_francisco)并在 ComponentInteraction 处理程序中对其进行解析,但这些参数最多只能有 100 个字符长并且不会'适用于更长的标签。

我认为它在 API 中不可用。您可以获得的最好的是 Context.Interaction.Message.Interaction,它只为您提供顶级元数据(命令的名称、用户和类型)。

更简单的方法是使用交互框架的通配符参数。就像你在 post 中提到的那样,它被限制为 100 个字符,所以如果你超过了这个限制,你将需要一些东西来缩短它(也许将标签存储在数据库中并将键值放在按钮的 ID 中)

    [ComponentInteraction("next-image:*")]
    public async Task ShowNextImage(string tag)
    { ... }