Search code examples
javascriptnode.jsasync-awaitdiscorddiscord.js

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

Solution

  • 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
    const replymsg = await interaction.reply({ content : 'Yes !' })
    // ✅ replymsg will be the reply of the interaction
    const replymsg = await interaction.reply({ content : 'Yes !', fetchReply: true })
    

    Simply replaced the first line with the second