获取交互的响应消息对象不和谐.js



Si我有一个非常简单的命令:

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"..

所以我会这样构建它:

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
}
})

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

我真的找到了。我只需要在发送交互响应时添加{ 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 })

简单地用第二个替换第一行

相关内容

最新更新