Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
Discord Bot - Wait for Reply after Interaction

Probably I didn't understand quite well how Discord API works when we use awaitMessages. What I'm trying to do is to wait for a message from the user after a button was clicked in a private channel:

client.on('interactionCreate', async interaction => {

if (interaction.isButton()) {
    if (interaction.customId.startsWith('dialogue-')) {
        const embed = new MessageEmbed()
            .setColor('#1a8175')
            .setTitle('๐Ÿ“– Dialogue')
            .setDescription('Please type your dialgoue')

        await interaction.channel.send({embeds: [embed]})

        // My problem lies here
        const filter = m => m.author.id === interaction.author.id;
        await interaction.channel.awaitMessages(filter, {
            max: 1,
            time: 60000,
            errors: ['time']
        }).then(
            async(collected) => {
                await interaction.channel.send('Received: ' + collected.first().content.toLowerCase())
            })
    }
}

As you can see, the user clicks on the button, a message is sent asking for the dialogue. After that the bot should receive the next message.

After debugging I saw that everything that I type after the message is sent to the user, triggers the messageCreate event, which is why my code is not working. In my understanding, when we use awaitMessages the bot should wait for the Promise to be completed. I can't figure out what I am missing here. Any ideas? Thanks in advance

about 4 years ago ยท Juan Pablo Isaza
3 answers
Answer question

0

As @GodderE2D said you need to change interaction.author.id to interaction.user.id in your filter.

You also need to move your filter in the object according to the docs like this:

const filter = m => m.author.id === interaction.user.id
interaction.channel.awaitMessages({ filter, max: 1, time: 60000 })
about 4 years ago ยท Juan Pablo Isaza Report

0

Reading more the documentation, I found another way to do the same task: Using MessageCollectors

const filter = m => m.author.id === interaction.user.id
        const collector = interaction.channel.createMessageCollector(filter, {max: 1, time: 60000})
        collector.once('collect', async (message) => {
            const embed = new MessageEmbed()
                .setColor('#1a8175')
                .setTitle(`๐Ÿ“– Dialogue ${dialogueNumber} received with success!!`)
                .setDescription(`Dialogue received: ${message.content}`)

            await interaction.channel.send({embeds: [embed]})
        })

It does the job and works well. However the time directive is not working properly. I have set the time to 4s in order to send a message back to the user if it takes too long to reply. Using the listener end should do the job, somehow is not working and the bot is waiting for the reply for a long time (I prefer that way) but I would like to understand why the bot is still hanging there, waiting for the user to reply. I have a feeling that the filter must be wrong:

        collector.on('end', collected => {
            if (collected.size === 0) {
                interaction.channel.send('Timeout - You did not send a dialogue')
            }
        });
about 4 years ago ยท Juan Pablo Isaza Report

0

To get the user who initialised the interaction, you should use Interaction#user. Whilst you should use Message#author to access the author of a message, you would need to use user for interactions.

const filter = m => m.author.id === interaction.user.id;

You can always refer to the documentation if you don't know about certain properties or methods.

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!