awaitMessageComponent breaks if separate message is deleted

I have the following code that awaits either a message, a string select menu or a button:
export default async function awaitResponse(
hook: Message,
interaction: ChatInputCommandInteraction
) {
if (interaction.channel instanceof StageChannel
|| interaction.channel === null)
Error("Interaction Channel is invalid for awaiting a response.");

return Promise.race([
interaction.channel.awaitMessages({
filter: (m) => {
return m.author.id === interaction.user.id;
},
time: CONFIG.maxAwaitInputTime,
max: 1,
})
,
hook.awaitMessageComponent({
time: CONFIG.maxAwaitInputTime,
filter: async (i) => {
console.log(i);
// only StringSelectMenuInteraction and ButtonInteraction
if (!i.deferred) await i.deferUpdate();
return i.user.id === interaction.user.id
&& (i.isStringSelectMenu() || i.isButton());
},
}),
])
}
export default async function awaitResponse(
hook: Message,
interaction: ChatInputCommandInteraction
) {
if (interaction.channel instanceof StageChannel
|| interaction.channel === null)
Error("Interaction Channel is invalid for awaiting a response.");

return Promise.race([
interaction.channel.awaitMessages({
filter: (m) => {
return m.author.id === interaction.user.id;
},
time: CONFIG.maxAwaitInputTime,
max: 1,
})
,
hook.awaitMessageComponent({
time: CONFIG.maxAwaitInputTime,
filter: async (i) => {
console.log(i);
// only StringSelectMenuInteraction and ButtonInteraction
if (!i.deferred) await i.deferUpdate();
return i.user.id === interaction.user.id
&& (i.isStringSelectMenu() || i.isButton());
},
}),
])
}
I also have the following while loop that determines what was chosen:
// Context:
// entry and page are defined
// buildMenu returns BaseMessageOptions
//
do {
// entry and page are defined outside of loop
const msg = await interaction.editReply(await buildMenu(entry, page));
const response = await awaitResponse(msg, interaction);

if (response instanceof Collection) {
// didn't want to nest ifs like that unnecessarily, but
// typescript doesn't really get the memo and keeps
// implying it can be undefined.
const responseMsg = response.first();
if (typeof responseMsg !== 'undefined') {
// ... parse responseMsg.content
if (responseMsg.deletable) await responseMsg.delete();
}
} else if (response.isStringSelectMenu()) {
// ...
} else switch (response.customId) {
// ...
}
} while (true)
// Context:
// entry and page are defined
// buildMenu returns BaseMessageOptions
//
do {
// entry and page are defined outside of loop
const msg = await interaction.editReply(await buildMenu(entry, page));
const response = await awaitResponse(msg, interaction);

if (response instanceof Collection) {
// didn't want to nest ifs like that unnecessarily, but
// typescript doesn't really get the memo and keeps
// implying it can be undefined.
const responseMsg = response.first();
if (typeof responseMsg !== 'undefined') {
// ... parse responseMsg.content
if (responseMsg.deletable) await responseMsg.delete();
}
} else if (response.isStringSelectMenu()) {
// ...
} else switch (response.customId) {
// ...
}
} while (true)
The response should either be parsed as String Select Menu, Button, or a Message that was sent in the same channel. If it's a message, it should be deleted and the content stored. When first starting it up, it works fine. I can change the button / select menu as often as I want. When sending a message, it gets properly parsed the first time. However, anything after that returns the following error:
throw er; // Unhandled 'error' event
^

DiscordAPIError[10062]: Unknown interaction
at handleErrors (C:\Users\...\node_modules\@discordjs\rest\dist\index.js:640:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async BurstHandler.runRequest (C:\Users\...\node_modules\@discordjs\rest\dist\index.js:736:23)
at async REST.request (C:\Users\...\node_modules\@discordjs\rest\dist\index.js:1387:22)
at async ButtonInteraction.deferUpdate (C:\Users\...\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:200:5)
at async InteractionCollector.filter (C:\Users\...\public\inc\functions\collectors\awaitResponse.js:35:21)
at async InteractionCollector.handleCollect (C:\Users\...\node_modules\discord.js\src\structures\interfaces\Collector.js:124:28)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:394:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21) {
requestBody: { files: undefined, json: { type: 6 } },
rawError: { message: 'Unknown interaction', code: 10062 },
code: 10062,
status: 404,
method: 'POST',
url: '<link emitted because post character limit>'
}
throw er; // Unhandled 'error' event
^

DiscordAPIError[10062]: Unknown interaction
at handleErrors (C:\Users\...\node_modules\@discordjs\rest\dist\index.js:640:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async BurstHandler.runRequest (C:\Users\...\node_modules\@discordjs\rest\dist\index.js:736:23)
at async REST.request (C:\Users\...\node_modules\@discordjs\rest\dist\index.js:1387:22)
at async ButtonInteraction.deferUpdate (C:\Users\...\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:200:5)
at async InteractionCollector.filter (C:\Users\...\public\inc\functions\collectors\awaitResponse.js:35:21)
at async InteractionCollector.handleCollect (C:\Users\...\node_modules\discord.js\src\structures\interfaces\Collector.js:124:28)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:394:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21) {
requestBody: { files: undefined, json: { type: 6 } },
rawError: { message: 'Unknown interaction', code: 10062 },
code: 10062,
status: 404,
method: 'POST',
url: '<link emitted because post character limit>'
}
Does channel.awaitMessages() conflict with message.awaitMessageComponent()?
4 Replies
d.js toolkit
d.js toolkit15mo ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Luca | LeCarbonator
discordjs@14.11.0 node v18.16.0
d.js docs
d.js docs15mo ago
Common causes of DiscordAPIError[10062]: Unknown interaction: - Initial response took more than 3 seconds ➞ defer the response *. - Wrong interaction object inside a collector. * Note: you cannot defer modal or autocomplete value responses
Luca | LeCarbonator
well, * I deferUpdate it if it's a component, * this is the only place where I call awaitResponse(), so there's no conflicting files/commands * it only happens after a message is collected that's the part that confuses me I tried the same without deleting the collected message, and the error still happens I see. So I'll have to rewrite the awaitResponse function to not use the promisified versions of the collectors That makes sense now. Thanks a lot!
Want results from more Discord servers?
Add your server