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);
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
});
For discord.js v13, clickButton is not an event. You need to use onInteraction then check if the interaction is a button.
For example:
client.on('interaction' (interaction) => {
if (interaction.isButton()) {
// do stuff
}
});