簡體   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