Luca | LeCarbonator
Luca | LeCarbonator
Explore posts from servers
DIAdiscord.js - Imagine an app
Created by Luca | LeCarbonator on 6/26/2023 in #djs-questions
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()?
9 replies
DIAdiscord.js - Imagine an app
Created by Luca | LeCarbonator on 2/8/2023 in #djs-questions
InteractionCollectorError ignores try/catch block
3 replies
DIAdiscord.js - Imagine an app
Created by Luca | LeCarbonator on 10/31/2022 in #djs-questions
createMessageCollector doesn't work, but only in one guild
15 replies
DIAdiscord.js - Imagine an app
Created by Luca | LeCarbonator on 10/30/2022 in #djs-questions
Calling promise function twice causes error
I have the following function that should await either a message from the user, or await a button click.
async function awaitInput() {
// awaits a response from the user. Returns either the message
// string or the button id.
const inputFilter = (m) => {
return m.author.id === interaction.user.id;
};
const btnFilter = async (i) => {
await i.deferUpdate();
return i.user.id === interaction.user.id;
};
// const message listed at the bottom of the post
const promises = [
interaction.channel.awaitMessages({
filter: inputFilter,
max: 1,
time: 60_000,
errors: ["time"],
}),
message.awaitMessageComponent({
filter: btnFilter,
ComponentType: ComponentType.Button,
time: 120_000,
}),
];

return Promise.any(promises).then((res) => {
if (res instanceof Collection) {
res.first().delete();
return res.first().content;
} else {
return res.customId;
}
});
}
async function awaitInput() {
// awaits a response from the user. Returns either the message
// string or the button id.
const inputFilter = (m) => {
return m.author.id === interaction.user.id;
};
const btnFilter = async (i) => {
await i.deferUpdate();
return i.user.id === interaction.user.id;
};
// const message listed at the bottom of the post
const promises = [
interaction.channel.awaitMessages({
filter: inputFilter,
max: 1,
time: 60_000,
errors: ["time"],
}),
message.awaitMessageComponent({
filter: btnFilter,
ComponentType: ComponentType.Button,
time: 120_000,
}),
];

return Promise.any(promises).then((res) => {
if (res instanceof Collection) {
res.first().delete();
return res.first().content;
} else {
return res.customId;
}
});
}
The first time works as intended. Either button clicks or message inputs cause the customId or message content to be available and used. However, if I try to call the function twice, I get this error:
node:events:504 // gateway timeout error??
{
message: 'Interaction has already been acknowledged.',
code: 40060
},
code: 40060,
status: 400,
method: 'POST',
url: '<<url that leads to the object below>>'
}

{"message": "405: Method Not Allowed", "code": 0}
node:events:504 // gateway timeout error??
{
message: 'Interaction has already been acknowledged.',
code: 40060
},
code: 40060,
status: 400,
method: 'POST',
url: '<<url that leads to the object below>>'
}

{"message": "405: Method Not Allowed", "code": 0}
The code has stopped working ever since implementing this function, so I think it's likely I am missing some problem with it. However, it's also possible that it's not the function that causes the problem and it's something else instead. I'd appreciate it if someone else could look at the function and tell me if there's any problems with the execution. The message variable is the following:
const message = await interaction.editReply({
embeds: [replyEmbed],
components: [btnFirstRow, btnSecondRow, cancelRow],
});
const message = await interaction.editReply({
embeds: [replyEmbed],
components: [btnFirstRow, btnSecondRow, cancelRow],
});
11 replies
DIAdiscord.js - Imagine an app
Created by Luca | LeCarbonator on 10/30/2022 in #djs-questions
Is there a way to simultaneously use awaitMessages and awaitMessageComponent?
I'm currently using a do while loop to modify entries in a list. If the user enters "done", it exits that loop. However, I am curious if there's a way to perform that action with a button instead. So the process would be as follows: - Display list - Await message and append entry to the list - ... but if a button is pressed instead, exit the loop. Up until now, I have worked exclusively with the two methods mentioned in the title. The problem with collector.on listeners is that they do not work in a while loop, since they can't be awaited. If collector.on listeners are the way to go, I'd like to know if it's possible to place them in a promise.
10 replies
DIAdiscord.js - Imagine an app
Created by Luca | LeCarbonator on 10/29/2022 in #djs-questions
awaitMessages filter firing off even though it returns false
11 replies
DIAdiscord.js - Imagine an app
Created by Luca | LeCarbonator on 10/27/2022 in #djs-questions
awaitMessageComponent 'Interaction has already been acknowledged' Error
13 replies
DIAdiscord.js - Imagine an app
Created by Luca | LeCarbonator on 10/27/2022 in #djs-questions
Client or message undefined
I'm currently trying to code a bot that does the following: - Awaits slash command, executes the corresponding command file - File displays entered data in an embed - User presses a button either to edit provided or add missing info. The problem is that (as far as I understood), you need a collector to handle it. The guide lists the following command: const collector = message.createMessageComponentCollector({ componentType: ComponentType.Button, time: 15000 }); However, I'm not sure what to replace the message object with in this case. Replacing it with interaction seems to cause an error. A couple hours ago, I had the same problem with client, where I wanted to use the bot's profile picture in the slash command, but it claimed that client isn't defined. The answer I received back then was that "client's available even in command files", but it doesn't seem to be the case. How can I access - the bot itself - the bot's embed message for button interactions from a commands/foo.js file?
15 replies
DIAdiscord.js - Imagine an app
Created by Luca | LeCarbonator on 10/27/2022 in #djs-questions
Accessing .addUserOption Pfp and bot pfp
I'm currently working alongside the online guide, and I've come across an issue I couldn't find. The current project structure contains a folder "commands", in which each file represents a command. I'm working on one file and try to obtain a target user's profile picture, as well as the bot's profile picture to display in an Embed. While interaction.user.displayAvatarURL() works as expected, I can't seem to figure out how to get the same URL from interaction.options.getUser("user"). Additionally, I don't know where I can locate the bot's profile picture to begin with. I assumed it's in the client object, but how do I access that from a separate file? Thanks in advance.
11 replies
DIAdiscord.js - Imagine an app
Created by Luca | LeCarbonator on 10/27/2022 in #djs-questions
Is it possible for an embed field to span multiple rows?
11 replies