Trouble with collector and interactions
Here's the part of my code that's causing errors:
async function handleListReactions(interaction) { //line 237
if (!reactions || Object.keys(reactions).length === 0) {
console.log("No reactions configured.");
return interaction.reply({
content: "No reactions configured.",
ephemeral: true,
});
}
let page = 0;
const itemsPerPage = 5; //line 247
const keys = Object.keys(reactions);
const totalPages = Math.ceil(keys.length / itemsPerPage);
const createEmbed = () => {
const start = page * itemsPerPage;
const currentItems = keys.slice(start, start + itemsPerPage);
return new EmbedBuilder()
.setTitle("Reactions List")
.setDescription(
currentItems //line 257
.map(
(key) =>
`**Trigger:** ${key}\n**Emoji:** ${
reactions[key].emoji || "None"
}\n**Message:** ${reactions[key].message || "None"}`
)
.join("\n\n")
)
.setColor(0x0099ff)
.setFooter({ text: `Page ${page + 1} of ${totalPages}` }); //line 267
};
await interaction.reply({
embeds: [createEmbed()],
components: [createPaginationRow()],
ephemeral: true,
});
const message = await interaction.fetchReply();
const collector = message.createMessageComponentCollector({ //line 277
componentType: 2,
time: 600000,
});
collector.on("collect", async (i) => {
console.log("Button interaction received");
await i.deferUpdate().catch(console.error);
const action = i.customId;
if (action === "next" && page < totalPages - 1) { //line 287
page++;
} else if (action === "previous" && page > 0) {
page--;
} else {
return i.followUp({
content: "No more pages in that direction.",
ephemeral: true,
});
}
await i //line 298
.update({
embeds: [createEmbed()],
components: [createPaginationRow()],
})
.catch((error) => console.error("Failed to update interaction:", error));
});
collector.on("end", () => {
interaction //line 307
.editReply({ components: [] })
.catch((error) => console.error("Failed to clear components:", error));
});
function createPaginationRow() {
return new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId("previous")
.setLabel("Previous")
.setStyle(ButtonStyle.Primary) //line 317
.setDisabled(page === 0),
new ButtonBuilder()
.setCustomId("next")
.setLabel("Next")
.setStyle(ButtonStyle.Primary)
.setDisabled(page >= totalPages - 1)
);
}
} //line 326
async function handleListReactions(interaction) { //line 237
if (!reactions || Object.keys(reactions).length === 0) {
console.log("No reactions configured.");
return interaction.reply({
content: "No reactions configured.",
ephemeral: true,
});
}
let page = 0;
const itemsPerPage = 5; //line 247
const keys = Object.keys(reactions);
const totalPages = Math.ceil(keys.length / itemsPerPage);
const createEmbed = () => {
const start = page * itemsPerPage;
const currentItems = keys.slice(start, start + itemsPerPage);
return new EmbedBuilder()
.setTitle("Reactions List")
.setDescription(
currentItems //line 257
.map(
(key) =>
`**Trigger:** ${key}\n**Emoji:** ${
reactions[key].emoji || "None"
}\n**Message:** ${reactions[key].message || "None"}`
)
.join("\n\n")
)
.setColor(0x0099ff)
.setFooter({ text: `Page ${page + 1} of ${totalPages}` }); //line 267
};
await interaction.reply({
embeds: [createEmbed()],
components: [createPaginationRow()],
ephemeral: true,
});
const message = await interaction.fetchReply();
const collector = message.createMessageComponentCollector({ //line 277
componentType: 2,
time: 600000,
});
collector.on("collect", async (i) => {
console.log("Button interaction received");
await i.deferUpdate().catch(console.error);
const action = i.customId;
if (action === "next" && page < totalPages - 1) { //line 287
page++;
} else if (action === "previous" && page > 0) {
page--;
} else {
return i.followUp({
content: "No more pages in that direction.",
ephemeral: true,
});
}
await i //line 298
.update({
embeds: [createEmbed()],
components: [createPaginationRow()],
})
.catch((error) => console.error("Failed to update interaction:", error));
});
collector.on("end", () => {
interaction //line 307
.editReply({ components: [] })
.catch((error) => console.error("Failed to clear components:", error));
});
function createPaginationRow() {
return new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId("previous")
.setLabel("Previous")
.setStyle(ButtonStyle.Primary) //line 317
.setDisabled(page === 0),
new ButtonBuilder()
.setCustomId("next")
.setLabel("Next")
.setStyle(ButtonStyle.Primary)
.setDisabled(page >= totalPages - 1)
);
}
} //line 326
2 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!
- ✅
Marked as resolved by OPThe output in the console:
Well, when I type the command it fetches all the reactions and creates the embed, but when I press the 'Next' button (since the 'Previous' is disabled on the first page) it usually crashes. The weird thing is that sometimes it goes to the next page before crashing.
I'll have a look
@Qjuh Thank you!
I fixed it thank to you
Logged in as TestBotLouise#8536
index.js:122
Checking if reactions are configured...
index.js:238
Button interaction received
index.js:284
DiscordAPIError[10062]: Unknown interaction
at handleErrors (C:\Users\PtiteLouise\node_modules\@discordjs\rest\dist\index.js:722:13)
at process.processTicksAndRejections (c:\Users\PtiteLouise\Desktop\hehe\Reaction Bot\lib\internal\process\task_queues.js:95:5)
at async BurstHandler.runRequest (C:\Users\PtiteLouise\node_modules\@discordjs\rest\dist\index.js:826:23)
at async _REST.request (C:\Users\PtiteLouise\node_modules\@discordjs\rest\dist\index.js:1266:22)
at async ButtonInteraction.deferUpdate (c:\Users\PtiteLouise\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:200:5)
at async InteractionCollector.<anonymous> (C:\Users\PtiteLouise\Desktop\hehe\Reaction Bot\index.js:285:5) {requestBody: {…}, rawError: {…}, code: 10062, status: 404, method: 'POST', …}
index.js:285
Failed to update interaction: DiscordAPIError[40060]: Interaction has already been acknowledged.
at handleErrors (C:\Users\PtiteLouise\node_modules\@discordjs\rest\dist\index.js:722:13)
at process.processTicksAndRejections (c:\Users\PtiteLouise\Desktop\hehe\Reaction Bot\lib\internal\process\task_queues.js:95:5)
at async BurstHandler.runRequest (C:\Users\PtiteLouise\node_modules\@discordjs\rest\dist\index.js:826:23)
at async _REST.request (C:\Users\PtiteLouise\node_modules\@discordjs\rest\dist\index.js:1266:22)
at async ButtonInteraction.update (c:\Users\PtiteLouise\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:233:5)
at async InteractionCollector.<anonymous> (C:\Users\PtiteLouise\Desktop\hehe\Reaction Bot\index.js:299:5) {requestBody: {…}, rawError: {…}, code: 40060, status: 400, method: 'POST', …}
index.js:304
Logged in as TestBotLouise#8536
index.js:122
Checking if reactions are configured...
index.js:238
Button interaction received
index.js:284
DiscordAPIError[10062]: Unknown interaction
at handleErrors (C:\Users\PtiteLouise\node_modules\@discordjs\rest\dist\index.js:722:13)
at process.processTicksAndRejections (c:\Users\PtiteLouise\Desktop\hehe\Reaction Bot\lib\internal\process\task_queues.js:95:5)
at async BurstHandler.runRequest (C:\Users\PtiteLouise\node_modules\@discordjs\rest\dist\index.js:826:23)
at async _REST.request (C:\Users\PtiteLouise\node_modules\@discordjs\rest\dist\index.js:1266:22)
at async ButtonInteraction.deferUpdate (c:\Users\PtiteLouise\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:200:5)
at async InteractionCollector.<anonymous> (C:\Users\PtiteLouise\Desktop\hehe\Reaction Bot\index.js:285:5) {requestBody: {…}, rawError: {…}, code: 10062, status: 404, method: 'POST', …}
index.js:285
Failed to update interaction: DiscordAPIError[40060]: Interaction has already been acknowledged.
at handleErrors (C:\Users\PtiteLouise\node_modules\@discordjs\rest\dist\index.js:722:13)
at process.processTicksAndRejections (c:\Users\PtiteLouise\Desktop\hehe\Reaction Bot\lib\internal\process\task_queues.js:95:5)
at async BurstHandler.runRequest (C:\Users\PtiteLouise\node_modules\@discordjs\rest\dist\index.js:826:23)
at async _REST.request (C:\Users\PtiteLouise\node_modules\@discordjs\rest\dist\index.js:1266:22)
at async ButtonInteraction.update (c:\Users\PtiteLouise\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:233:5)
at async InteractionCollector.<anonymous> (C:\Users\PtiteLouise\Desktop\hehe\Reaction Bot\index.js:299:5) {requestBody: {…}, rawError: {…}, code: 40060, status: 400, method: 'POST', …}
index.js:304