How to get never timeout buttons

I want my buttons in messages to get interactions forever. Even when I re-run the code, it must still give interaction response. My code now ( it follows discordjs guide ) - https://pastebin.com/h3kKzszT I thought of removing the time, but it didn't work as I expected it to be. Please provide a detailed explanation about how I can achieve this.
Pastebin
Code - Pastebin.com
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.
10 Replies
d.js toolkit
d.js toolkit8mo 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! - Marked as resolved by OP
NyR
NyR8mo ago
Buttons never expire, what matters is if you are responding to its event in interactionCreate event listener, what you have there in the snippet is a collector, which is basically an event listener that temporarily listens to 'interactionCreate' event (in your case), and disposes itself when it's condition is met or if time expires, so it is not permanent, you should listen to the buttons interaction in your main interaction handler file if you want to handle the buttons all times
Gripz
GripzOP8mo ago
I have no idea how do I handle buttons. Please elaborate. Here's my part of code that handles slash command interactions.
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);

if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}

try {
await command.execute(interaction, client);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
const errorEmbed = new EmbedBuilder()
.setTitle("Error")
.setDescription(
"There was an error while executing this command! Please [contact the developer](https://discord.com/users/844863061907210251).",
)
.setTimestamp()
.setColor(0xff0000)
.setThumbnail(client.user.displayAvatarURL());
await interaction.followUp({ embeds: [errorEmbed], ephemeral: true });
} else {
const errorEmbed = new EmbedBuilder()
.setTitle("Error")
.setDescription(
"There was an error while executing this command! Please [contact the developer](https://discord.com/users/844863061907210251).",
)
.setTimestamp()
.setColor(0xff0000)
.setThumbnail(client.user.displayAvatarURL());
await interaction.reply({ embeds: [errorEmbed], ephemeral: true });
}
}
});
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);

if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}

try {
await command.execute(interaction, client);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
const errorEmbed = new EmbedBuilder()
.setTitle("Error")
.setDescription(
"There was an error while executing this command! Please [contact the developer](https://discord.com/users/844863061907210251).",
)
.setTimestamp()
.setColor(0xff0000)
.setThumbnail(client.user.displayAvatarURL());
await interaction.followUp({ embeds: [errorEmbed], ephemeral: true });
} else {
const errorEmbed = new EmbedBuilder()
.setTitle("Error")
.setDescription(
"There was an error while executing this command! Please [contact the developer](https://discord.com/users/844863061907210251).",
)
.setTimestamp()
.setColor(0xff0000)
.setThumbnail(client.user.displayAvatarURL());
await interaction.reply({ embeds: [errorEmbed], ephemeral: true });
}
}
});
NyR
NyR8mo ago
Right, so what you do here is return if the interaction is not from slash commands, you need to slightly modify this, so instead of returning, wrap it all inside the if block using curly braces ({}) and then add another statement for buttons handling like
if (interaction.isButton()) {
// Handle button interaction here
}
if (interaction.isButton()) {
// Handle button interaction here
}
Now the handling button would be the same as how you are doing in your original code inside the collector, just check by it's ID and do what you want to do! If you had advanced further in the guide, you would have known this. https://discordjs.guide/message-components/interactions.html#the-client-interactioncreate-event this part explains how to handle different types of interactions, i suggest you give this a read
Gripz
GripzOP8mo ago
Yep. That works. But I have one more thing to ask. This method is good but everything looks cluttered now. Can I use the same
client.on(Events.InteractionCreate, async (interaction) => {})
client.on(Events.InteractionCreate, async (interaction) => {})
in my command files too? like for buttons in a file, I will make a new client.on in that file only.
NyR
NyR8mo ago
No you should not create duplicate listeners everytime you want to, you should only have one unique listener ideally, what you can do is create a button handler similar to how you have command handler Or separate things in files, make it a function and call that function inside your event listener and pass interaction to it
The Gobbler
The Gobbler8mo ago
You shouldn't. That'll create a new event listener every time the command is ran. In general, you should only have 1 event listener per event. Instead, you can create a function to handle the button interaction, which could make it less cluttered.
// could be in a seperate file
async function handleButton(interaction) {
await interaction.reply('Button clicked!');
}

if (interaction.isButton()) {
handleButton(interaction);
}
// could be in a seperate file
async function handleButton(interaction) {
await interaction.reply('Button clicked!');
}

if (interaction.isButton()) {
handleButton(interaction);
}
Gripz
GripzOP8mo ago
I ain't getting what are you saying. I am kinda new to discordjs. I added this line to the file where interactions are handled, inside the client.on.
if (interaction.isButton()) { await handlebuttons(interaction);
}
if (interaction.isButton()) { await handlebuttons(interaction);
}
And added this inside my command file -
async function handlebuttons(interaction) {
if (interaction.customId === "rules") {
await interaction.reply({content:"Rules"})
}
if (interaction.customId === "posts") {
await interaction.reply({content:"Posts"})
}
}
async function handlebuttons(interaction) {
if (interaction.customId === "rules") {
await interaction.reply({content:"Rules"})
}
if (interaction.customId === "posts") {
await interaction.reply({content:"Posts"})
}
}
I can't figure out by what The Gobbler said. Which line should I put in which file. Sorry to mention but still waiting for an answer. @NyR
NyR
NyR8mo ago
Umm, no! They didn't tell you to add it to your command file, you can make another file and put this function there, export it and then import it in you interaction handler file and call it like you're doing This is like basic js
Gripz
GripzOP8mo ago
Yes. I got what you said now. Thanks for help. yes
Want results from more Discord servers?
Add your server