K
K
ka4ergaa2022-01-17 16:13:53
Node.js
ka4ergaa, 2022-01-17 16:13:53

How to catch a click on a Discord.js button?

It is necessary to make it so that when you click on the button with the id click_to_function_in_row, the text is sent. So far this has only worked, but nothing is working. At the very least it throws an error.

TypeError: message.channel.createMessageComponentCollector is not a function
.

const Discord = require('discord.js');
const client = new Discord.Client();
const MessageEmbed = require('discord.js');
const disbut = require('discord-buttons');
const MessageButton = require('discord-buttons')
const createMessageComponentCollector = require('discord.js')

disbut(client);

exports.run = async (client, message) => {

  const button = new disbut.MessageButton()
    .setStyle('url')
    .setURL(`https://youtube.com/aquamine`)
    .setLabel(`Создатель бота`);

  const nsfwbtn = new disbut.MessageButton()
    .setStyle('red')
    .setID('click_to_function_in_row') 
    .setLabel('NSFW команды') 

  const row = new disbut.MessageActionRow()
    .addComponent(button)
    .addComponent(nsfwbtn);

  const embed = new Discord.MessageEmbed()
    .setColor('#2f3136')
    .setTitle('Список команд:')
    .addField('Серверные команды', '`ping`, `serverinfo`, `user`, `avatar`, `8ball`')
    .addField('Музыкальные команды', '`play`, `pause`, `resume`, `queue`, `clear-queue`, `shuffle`, `np`, `loop`, `volume`, `skip`, `stop`')
    .addField('Модерирование', '`say`, `sayembed`, `check`, `checkping`, `clear`')
    .setThumbnail(client.user.displayAvatarURL({ dynamic:true }))

  await message.channel.send(embed, { component: row });

  const collector = message.channel.createMessageComponentCollector()

  collector.on('collect', async i => {

    if (i.customId === 'click_to_function_in_row') {
      await i.update({ content: 'на кнопку нажали', components: [] });

    }
  });   
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2022-01-18
@ka4ergaa

the version of discord.js you are using (12.5.3) is now out of date and no longer officially supported.
.createMessageComponentCollector()was added in version 13.0.0, so you need to upgrade to use it.
to do this, send the following to the terminal:

npm uninstall discord.js
npm install discord.js

also you must have node.js version 16.6+ to upgrade.
to see the current one, node -vin the terminal.
about
await message.channel.send(embed, { component: row });

...in recent versions, the message sending structure has been changed and now looks a little different:
- message.channel.send(embed, { component: row });
+ message.channel.send({ embeds: [embed], components: [row] });

about all changes: https://discordjs.guide/additional-info/changes-in...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question