繁体   English   中英

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

[英]discord.js Editting Slash Command Interaction Messages

所以我在这里使用 discord.js 和这个代码:

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

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

您可以使用此补丁请求编辑交互响应(请参阅: 后续消息):

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

使用axios 库的基本示例:

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

此请求的答案也将包含 message-id。

Here is an example function that will edit the original message either as plain text or an embed object and returns the discord message object for further usage (eg add reply emojis etc.):

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 的空响应,而不是发送诸如“正在获取数据...”之类的初始消息。这是内置方法,也显示了一点加载 animation :) 见这里(一个优点是这个方式没有“编辑”出现。)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM