I am currently coding a bot discord. I have an error about the buttons. The question has already been asked on the forum, but I haven't found a relevant answer... When running the first command, everything works fine, when running the second command, this message appears...
==> Here is the code:
const Discord = require('discord.js');
const { MessageActionRow, MessageButton, MessageEmbed } = require('discord.js');
module.exports.run = async (client, message, args) => {
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('yes')
.setLabel('YES')
.setStyle('SUCCESS'),
new MessageButton()
.setCustomId('no')
.setLabel('NO')
.setStyle('DANGER'),
);
const embed = new MessageEmbed()
.setColor('#0099ff')
.setTitle('Some title')
await message.reply({ embeds: [embed], components: [row] });
client.on('interactionCreate', async interaction => {
if (interaction.customId == 'yes') {
await interaction.reply({ content: 'You click on YES !', ephemeral: true });
};
});
};
module.exports.info = {
names: ['t4', 'test4'],
};
==> Here is the error:
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: Interaction has already been acknowledged.
at RequestHandler.execute (C:\Users\famou\OneDrive\Bureau\RPG Bot\node_modules\discord.js\src\rest\RequestHandler.js:298:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (C:\Users\famou\OneDrive\Bureau\RPG Bot\node_modules\discord.js\src\rest\RequestHandler.js:50:14)
at async ButtonInteraction.reply (C:\Users\famou\OneDrive\Bureau\RPG Bot\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:99:5)
at async Client.<anonymous> (C:\Users\famou\OneDrive\Bureau\RPG Bot\commands\test4.js:45:13) {
method: 'post',
path: '/interactions/997619679268970577/aW50ZXJhY3Rpb246OTk3NjE5Njc5MjY4OTcwNTc3OjJsZmhuWlJWaVJYNnJlcnhJNkFtSXhhcEllM2Vhd0VQdVJOYTQwdzF0Y3IyakpGWVh1Vkd1a3BXeHZyc25aTFFOYk9uMVQ2QkNFTGJPQmFaVjdvWG90eTB3V1I3ckR3ZFd3TGd3YjdGejREajBZeGhKUWtRc3F3c3BMdlR2QW1a/callback',
code: 40060,
httpStatus: 400,
requestData: {
json: {
type: 4,
data: {
content: 'Ready! You closed the channel!',
tts: false,
nonce: undefined,
embeds: undefined,
components: undefined,
username: undefined,
avatar_url: undefined,
allowed_mentions: undefined,
flags: 64,
message_reference: undefined,
attachments: undefined,
sticker_ids: undefined
}
},
files: []
}
}
Thanks ^^
CodePudding user response:
You are creating nested listeners. You are creating a listener whenever the command is run, doing no validation at all in the callback, making it take every interaction, even from a different command. A better option is to use collectors.
const msg = await message.reply({ embeds: [embed], components: [row] });
const collector = msg.createMessageComponentCollector({
componentType: "BUTTON",
time: 15_000 // how long you want it to collect for, in ms (this is 15 seconds)
})
collector.on('collect', async interaction => {
if (interaction.customId == 'yes') {
await interaction.reply({ content: 'You click on YES !', ephemeral: true });
};
});