#respond to button

1 messages · Page 1 of 1 (latest)

turbid dawn
#

Hi

#

You can make a Component collector in text channel if you are sure user will click the button within minutes or hours

#

Or it you want to make it permanent, then use "interactionCreate" event

plucky sigil
#

I set up a collector, let me show. I am wondering if this should work.

#
const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageActionRow, MessageButton, MessageEmbed } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('ping')
        .setDescription('Get the AI latency'),
    
    async execute(client, interaction) {
        const row = new MessageActionRow()
            .addComponents(
                new MessageButton()
                    .setCustomId('refresh-ping')
                    .setLabel('Refresh')
                    .setStyle('PRIMARY'),
            );

        const embed = new MessageEmbed()
            .setColor('AQUA')
            .setTitle('Pong!')
            .setDescription(`AI Latency: ${client.ws.ping}ms`)
            .setTimestamp();
        
        interaction.reply({ embeds: [embed], components: [row] });

        const filter = i => i.customId === 'refresh-ping';

        const collector = interaction.channel.createMessageComponentCollector({ filter, time: 15000 });

        collector.on('collect', async i => {
            if (i.customId === 'primary') {
                await i.update({ embeds: [embed], components: [] });
            }
        });
    }
}
#

Going to up the time right now though as 15 seconds is low, changing it to 60 seconds.

#

@turbid dawn is this how it's supposed to be written? ^^^

turbid dawn
#

Ya

#

Oh wait

#

You are checking if customId == 'primary'

#

It's a button style

plucky sigil
#

Oh yes forgot to change it to refresh-ping

turbid dawn
#

And you have already declared check custom I'd in filter

plucky sigil
#

Copied it off the documentation/guide lol

turbid dawn
#

So you can remove it

#

It will emit only if it matches the id

plucky sigil
#

So now I should have this correct?

#
        collector.on('collect', async i => {
            if (i.customId === 'refresh-ping') {
                await i.update({ embeds: [embed], components: [row] });
            }
        });
turbid dawn
#

Yup

silent pelican
turbid dawn
#

🙂

#

Having them in interaction event will make it work after restart too

silent pelican
#

True