Home > Net >  No message on buttonClick discord.js
No message on buttonClick discord.js

Time:03-12

When I click on button I get error "Interaction failed" This is full code I have written. What could be wrong here? I tried and interaction.create but no luck so far. Thank you.

Index.js

const { token } = require('./config.json');
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const { MessageActionRow, MessageButton, MessageEmbed } = require('discord.js');
const {gabriel} = require('./commands/gabriel.js')

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
  });

  client.on('messageCreate', message => {
    if(message.content === "$gabriel") {
        gabriel(message);
    }

});

client.on('clickButton', async (button) => {
  if(button.id === "11"){
    await button.reply.defer()
    await button.message.channel.send("Skills are posted in here!")
  } else if (button.id === "12"){
    await button.reply.defer()
    await button.message.channel.send("Constellation Data!")
  }
});

  client.login(token);

CodePudding user response:

First things first, there isn't an event called clickButton in discord.js unless you use extra npm packages such as discord.js-buttons. You can check the list of all the events at discord.js | Client. To execute a piece of code when a button is clicked, you would have to create a messageComponentCollector in the channel of your choice by doing:

var filter = (click) => click.user.id === message.author.id; // Creating a filter to only allow some users to click the button
const collector = message.channel.createMessageComponentCollector({
  time:
    1000 *
    10 /* Choosing how long the collector can work. (The time is in milliseconds) */,
  max: 1 /* Defining the maximum number of clicks on the button */,
  filter: filter /* Applying the filter */,
});
collector.on('collect', (click) => {
  // Execute some code
});

CodePudding user response:

You would want to download the package discord-buttons since that is the official package for this also you are using a v12 function of discord.js. If you use v13 then go read the documentation on it at https://discordjs.guide/interactions/buttons.html#building-and-sending-buttons but if you're using v12.5.3 of discord.js then run on your terminal npm i [email protected] or just npm i discord-buttons.

After you would want to do the example below i will give you on making and fixing buttons and events. Also you would want to add the discord-buttons to the client. Also you haven't created any buttons and that's why it may not work please do the following below if your using v12 and make sure to understand it

  • Related