简体   繁体   中英

Get interaction's reponse message object discord.js

Si I have this really simple command :

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

It works perfectly. It sends the message with the components and works just fine. The problem is now I want to listen to the buttons. On a message, I can do

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

})
.catch(console.error)

That also works perfectly, I can listen to the messages and do something :D Now the problem is that when replying to the message, the promise returns undefined

int.reply('Replied to your message')

How to get the reply and be able to listen to it's buttons ? :/

EDIT : I actually found it. I just had to add { fetchReply : true } when sending an interaction response For example:

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

// Do something with "reply"..

So I would build it like this:

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
    )

Then you would collect it under your .on(“interactionCreate”) event using this code:

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

Then if anyone clicks it (even between reboots) the button click is listened to and responded to.

I actually found it. I just had to add { fetchReply : true } as an additionnal option when sending an interaction response. For example:

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

Simply replaced the - by the +

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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