How to access interaction.options inside button callback

Is it possible to get interaction.options from command handler to the button itself so i could continue with the handler?
if (interaction.isButton()) {
if (interaction.customId.includes("join")) {
///
}
if (interaction.isButton()) {
if (interaction.customId.includes("join")) {
///
}
5 Replies
d.js toolkit
d.js toolkit17mo ago
- 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!
shullex
shullexOP17mo ago
According to this https://discordjs.guide/message-components/interactions.html#component-collectors If i understand it correctly i should do this.
const joinCollector = joinClicked.createMessageComponentCollector({
componentType: ComponentType.Button,
});

joinCollector.on("collect", async (i) => {
console.log(i);
});
const joinCollector = joinClicked.createMessageComponentCollector({
componentType: ComponentType.Button,
});

joinCollector.on("collect", async (i) => {
console.log(i);
});
But after clicking on my button, it doesnt get fired.
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
shullex
shullexOP17mo ago
I can share the full file code here, but should i use default discord formatting or hatebin ?
shullex
shullexOP17mo ago
Pastebin
const { PrismaClient } = require("@prisma/client");const { SlashCo...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
shullex
shullexOP17mo ago
announcementMessage ? Oh Still doesnt get fired:(
const joinCollector = announcementMessage.createMessageComponentCollector(
{
componentType: ComponentType.Button,
}
);

joinCollector.on("collect", async (i) => {
console.log(i);
});
const joinCollector = announcementMessage.createMessageComponentCollector(
{
componentType: ComponentType.Button,
}
);

joinCollector.on("collect", async (i) => {
console.log(i);
});
Shouldn't collector.on('collector') fire the console.log(i) anyways?
const joinCollector = announcementMessage.createMessageComponentCollector(
{
componentType: ComponentType.Button,
}
);

joinCollector.on("collect", async (i) => {
console.log(i);
});

if (
announcementMessage.customId ===
`join-${interaction.options.getString("event-name")}`
) {
console.log(`Clicked button with customId ${joinClicked.customId}`);
try {
const userId = await prisma.user.findFirst({
where: {
discord_id: parseInt(interaction.user.id),
},
select: {
id: true,
},
});
await joinEvent(
interaction.options.getString("event-name"),
userId.id
);
const user = interaction.guild.members.cache.get(interaction.user.id);
const role = interaction.guild.roles.cache.find(
(role) =>
role.name ==
interaction.options.getString("event-name") + " participator"
);

user.roles.add(role);

console.log(
`User ${user.username} joined event ${interaction.options.getString(
"event-name"
)}`
);

interaction.reply({
content: "✅U have sucessfully joined the event.✅",
ephemeral: true,
});
} catch (e) {
console.log(e);
}
}
const joinCollector = announcementMessage.createMessageComponentCollector(
{
componentType: ComponentType.Button,
}
);

joinCollector.on("collect", async (i) => {
console.log(i);
});

if (
announcementMessage.customId ===
`join-${interaction.options.getString("event-name")}`
) {
console.log(`Clicked button with customId ${joinClicked.customId}`);
try {
const userId = await prisma.user.findFirst({
where: {
discord_id: parseInt(interaction.user.id),
},
select: {
id: true,
},
});
await joinEvent(
interaction.options.getString("event-name"),
userId.id
);
const user = interaction.guild.members.cache.get(interaction.user.id);
const role = interaction.guild.roles.cache.find(
(role) =>
role.name ==
interaction.options.getString("event-name") + " participator"
);

user.roles.add(role);

console.log(
`User ${user.username} joined event ${interaction.options.getString(
"event-name"
)}`
);

interaction.reply({
content: "✅U have sucessfully joined the event.✅",
ephemeral: true,
});
} catch (e) {
console.log(e);
}
}
This is my full code for the button i believe Should the if use the aMessage.customId or collector.customId ?
TypeError: announcementMessage.awaitMessageComponent is not a function
at Object.execute (C:\WebDev\wbgl-league-discord-bot\commands\event\create.js:130:55)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Object.execute (C:\WebDev\wbgl-league-discord-bot\events\interactionCreate.js:16:9)
TypeError: announcementMessage.awaitMessageComponent is not a function
at Object.execute (C:\WebDev\wbgl-league-discord-bot\commands\event\create.js:130:55)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Object.execute (C:\WebDev\wbgl-league-discord-bot\events\interactionCreate.js:16:9)
now im getting this error. Huh? i actually do have await in my code, not sure why its not in my snippet above oh , that should be the interaction right i dont have collect event rn
try {
const joinClicked = await announcementMessage.awaitMessageComponent({
componentyType: ComponentType.Button,
});
if (
joinClicked.customId ==
`join-${interaction.options.getString("event-name")}`
) {
console.log(this);
}
} catch (e) {
console.log(e);
}
try {
const joinClicked = await announcementMessage.awaitMessageComponent({
componentyType: ComponentType.Button,
});
if (
joinClicked.customId ==
`join-${interaction.options.getString("event-name")}`
) {
console.log(this);
}
} catch (e) {
console.log(e);
}
this code gives me error i gave above
const announcementMessage = interaction.client.channels.cache
.get("1145330533975466044")
.send({
content: "🎺🎺 @everyone 🎺🎺",
embeds: [announcementEmbed],
components: [row],
});
const announcementMessage = interaction.client.channels.cache
.get("1145330533975466044")
.send({
content: "🎺🎺 @everyone 🎺🎺",
embeds: [announcementEmbed],
components: [row],
});
i guess i did not added await to this ^ and got method not allowed response
DiscordAPIError[10062]: Unknown interaction
at handleErrors (C:\WebDev\wbgl-league-discord-bot\node_modules\@discordjs\rest\dist\index.js:687:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async BurstHandler.runRequest (C:\WebDev\wbgl-league-discord-bot\node_modules\@discordjs\rest\dist\index.js:786:23)
at async _REST.request (C:\WebDev\wbgl-league-discord-bot\node_modules\@discordjs\rest\dist\index.js:1218:22)
at async ChatInputCommandInteraction.reply (C:\WebDev\wbgl-league-discord-bot\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:111:5) {
DiscordAPIError[10062]: Unknown interaction
at handleErrors (C:\WebDev\wbgl-league-discord-bot\node_modules\@discordjs\rest\dist\index.js:687:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async BurstHandler.runRequest (C:\WebDev\wbgl-league-discord-bot\node_modules\@discordjs\rest\dist\index.js:786:23)
at async _REST.request (C:\WebDev\wbgl-league-discord-bot\node_modules\@discordjs\rest\dist\index.js:1218:22)
at async ChatInputCommandInteraction.reply (C:\WebDev\wbgl-league-discord-bot\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:111:5) {
So got it working when it actually give me some console.log() Should be button be callable only once? no... i mean, doesnt matter how myn times i clicked the button , it got fired only once Im just super dumb lol. Thanks for your assistance.
Want results from more Discord servers?
Add your server