discord.js 编辑斜线命令交互消息

时间:2021-04-07 03:31:22

标签: javascript discord discord.js

所以我在这里使用带有此代码的 discord.js:

client.api.interactions(interaction.id, interaction.token).callback.post({
  data: {
    type: 4,
    data: {
      content: "Getting Data..."
    }
  }
})

我希望能够在之后编辑此消息,但我所看到的所有内容都需要一个消息 ID,而我似乎无法从此代码中获取消息 ID。

1 个答案:

答案 0 :(得分:0)

您可以使用此补丁请求编辑交互响应(请参阅:Followup Messages):

PATCH /webhooks/<application_id>/<interaction_token>/messages/@original

使用 axios library 的基本示例:

axios.patch(`https://discord.com/api/v8/webhooks/${appId}/${interaction.token}/messages/@original`, { content: 'New content' });

对此请求的答复也将包含消息 ID。

这是一个示例函数,它将编辑原始消息作为纯文本或嵌入对象并返回不和谐消息对象以供进一步使用(例如添加回复表情符号等):

const editInteraction = async (client, interaction, response) => {
    // Set the data as embed if reponse is an embed object else as content
    const data = typeof response === 'object' ? { embeds: [ response ] } : { content: response };
    // Get the channel object by channel id:
    const channel = await client.channels.resolve(interaction.channel_id);
    // Edit the original interaction response:
    return axios
        .patch(`https://discord.com/api/v8/webhooks/${appId}/${interaction.token}/messages/@original`, data)
        .then((answer) => {
            // Return the message object:
            return channel.messages.fetch(answer.data.id)
        })
};

此外,您还可以发送类型 5 的空响应,而不是发送诸如“正在获取数据...”之类的初始消息。这是内置方法,也显示了一些加载动画 :) See here (一个优点是这样就不会出现“已编辑”。)

client.api.interactions(interaction.id, interaction.token).callback.post({
    data: {
        type: 5,
    },
})