【问题标题】:Get interaction's reponse message object discord.js获取交互的响应消息对象 discord.js
【发布时间】:2022-07-17 20:17:00
【问题描述】:

我有这个非常简单的命令:

let row = new Discord.MessageActionRow().addComponents(...) // The .. is too long so i'll just remove it for this question
int.reply({ content : 'pong', components : [row]})

完美运行。它使用组件发送消息并且工作得很好。 问题是现在我想听按钮。 在消息上,我可以做

message.reply({ content : 'ok', components : [row]})
.then(msg =>{
  let collector = msg.createMessageComponentCollector({ componentType : 'BUTTON', time : 10e5 })
  // Collector thingys

})
.catch(console.error)

这也很完美,我可以听消息并做点什么:D 现在的问题是,在回复消息时,promise 会返回 undefined

int.reply('Replied to your message')

如何获得回复并能够听到它的按钮? :/

编辑: 我真的找到了。 我只需要在发送交互响应时添加 { fetchReply : true } 例如:

const reply = await interaction.reply({ content : 'Test !', components : [row], fetchReply : true})

// Do something with "reply"..

【问题讨论】:

    标签: javascript node.js async-await discord discord.js


    【解决方案1】:

    所以我会这样构建它:

    const row = new MessageActionRow()
        .addComponents(
            new MessageButton()
            .setCustomId('ButtonIdChangeThis')
            .setLabel('WhatButtonSaysChangeThis')
            .setStyle('SUCCESS')
    // can use ‘SUCCESS’ for green, ‘DANGER’ for red, ‘PRIMARY’ for blurple, ‘SECONDARY’ for grey
        )
    

    然后您将使用此代码在您的 .on(“interactionCreate”) 事件下收集它:

    client.on(“interactionCreate”, async interaction => {
        if (interaction.isButton()) {
            const buttonID = interaction.customId
            if (buttonID === 'ButtonIdChangeThis') {
    //put what button does here
        }
    })
    

    然后,如果有人点击它(即使在重新启动之间),按钮点击就会被监听并响应。

    【讨论】:

      【解决方案2】:

      我真的找到了。在发送交互响应时,我只需要添加 { fetchReply : true } 作为附加选项。 例如:

      // replymsg will be undefined
      let replymsg = await interaction.reply({ content : 'Yes !' })
      // replymsg will be the reply of the interaction
      let replymsg = await interaction.reply({ content : 'Yes !', fetchReply: true })
      

      只需将 - 替换为 +

      【讨论】:

        猜你喜欢
        • 2022-01-21
        • 1970-01-01
        • 2021-10-17
        • 2020-08-17
        • 2021-04-11
        • 2021-06-24
        • 2021-07-13
        • 2021-07-02
        • 1970-01-01
        相关资源
        最近更新 更多