Button callback not working
i had this button function that used to work properly, now that im checking in after a while of refactoring it just doesnt work anymore and i have not the slightest clue why.
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!async function battleButtons(player1Id, player2Id, interaction, user1Cards, user2Cards, battleEmbed, time, battleSession, callback) {
const row = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setCustomId('switch')
.setLabel('Switch')
.setStyle(ButtonStyle.Primary)
.setEmoji('🔀'),
new ButtonBuilder()
.setCustomId('fight')
.setLabel('Fight')
.setStyle(ButtonStyle.Primary)
.setEmoji('⚔️'),
new ButtonBuilder()
.setCustomId('forfeit')
.setLabel('Forfeit')
.setStyle(ButtonStyle.Danger)
.setEmoji('🏳️'),
);
const embed = battleEmbed;
const response = await interaction.followUp({
embeds: [embed],
components: [row],
});
const filter = (interaction) => {
return interaction.user.id === player1Id || interaction.user.id === player2Id;
};
const collector = response.createMessageComponentCollector({ filter, time});
collector.on('collect', async interaction => {
let userCards = [];
if (interaction.customId === 'forfeit') {
if (battleSession.state === "Player1Won" || battleSession.state === "Player2Won") {
const newBattleEmbed = new EmbedBuilder(response.embeds[0]).setColor('#808080'); // Edit the initial embed
await response.edit({ // Edit the initial message
embeds: [newBattleEmbed],
components: [], // Remove all components (buttons)
});
return;
}
const confirmEmbed = new EmbedBuilder()
.setColor(0x028A0F)
.setTitle("Would you like to end the battle?")
confirmButtons(interaction.user.id, interaction, response, confirmEmbed, 30 * 1000, callback); // Pass the 'response' as 'battleMessage' to 'confirmButtons'
}
async function battleButtons(player1Id, player2Id, interaction, user1Cards, user2Cards, battleEmbed, time, battleSession, callback) {
const row = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setCustomId('switch')
.setLabel('Switch')
.setStyle(ButtonStyle.Primary)
.setEmoji('🔀'),
new ButtonBuilder()
.setCustomId('fight')
.setLabel('Fight')
.setStyle(ButtonStyle.Primary)
.setEmoji('⚔️'),
new ButtonBuilder()
.setCustomId('forfeit')
.setLabel('Forfeit')
.setStyle(ButtonStyle.Danger)
.setEmoji('🏳️'),
);
const embed = battleEmbed;
const response = await interaction.followUp({
embeds: [embed],
components: [row],
});
const filter = (interaction) => {
return interaction.user.id === player1Id || interaction.user.id === player2Id;
};
const collector = response.createMessageComponentCollector({ filter, time});
collector.on('collect', async interaction => {
let userCards = [];
if (interaction.customId === 'forfeit') {
if (battleSession.state === "Player1Won" || battleSession.state === "Player2Won") {
const newBattleEmbed = new EmbedBuilder(response.embeds[0]).setColor('#808080'); // Edit the initial embed
await response.edit({ // Edit the initial message
embeds: [newBattleEmbed],
components: [], // Remove all components (buttons)
});
return;
}
const confirmEmbed = new EmbedBuilder()
.setColor(0x028A0F)
.setTitle("Would you like to end the battle?")
confirmButtons(interaction.user.id, interaction, response, confirmEmbed, 30 * 1000, callback); // Pass the 'response' as 'battleMessage' to 'confirmButtons'
}
async function confirmButtons(userId, message, battleMessage, embeds, time, callback) {
const row = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setCustomId('yes')
.setLabel('Yes')
.setStyle(ButtonStyle.Success)
.setEmoji('✔️'),
new ButtonBuilder()
.setCustomId('no')
.setLabel('No')
.setStyle(ButtonStyle.Danger)
.setEmoji('✖️'),
);
const embed = embeds;
const response = await message.reply({
embeds: [embed],
components: [row],
ephemeral: true
});
const filter = (interaction) => {
return interaction.user.id === userId;
};
const collector = response.createMessageComponentCollector({ filter, time});
collector.on('collect', async interaction => {
if (interaction.customId === 'yes') {
const newBattleEmbed = new EmbedBuilder(battleMessage.embeds[0]).setColor('#808080');
await battleMessage.edit({
embeds: [newBattleEmbed],
components: [],
});
callback(["yes", interaction.user]);
response.delete();
collector.stop();
}
else if (interaction.customId === 'no') {
callback(["no", interaction.user]);
response.delete();
collector.stop();
}
});
collector.on('end', async () => {
});
}
async function confirmButtons(userId, message, battleMessage, embeds, time, callback) {
const row = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setCustomId('yes')
.setLabel('Yes')
.setStyle(ButtonStyle.Success)
.setEmoji('✔️'),
new ButtonBuilder()
.setCustomId('no')
.setLabel('No')
.setStyle(ButtonStyle.Danger)
.setEmoji('✖️'),
);
const embed = embeds;
const response = await message.reply({
embeds: [embed],
components: [row],
ephemeral: true
});
const filter = (interaction) => {
return interaction.user.id === userId;
};
const collector = response.createMessageComponentCollector({ filter, time});
collector.on('collect', async interaction => {
if (interaction.customId === 'yes') {
const newBattleEmbed = new EmbedBuilder(battleMessage.embeds[0]).setColor('#808080');
await battleMessage.edit({
embeds: [newBattleEmbed],
components: [],
});
callback(["yes", interaction.user]);
response.delete();
collector.stop();
}
else if (interaction.customId === 'no') {
callback(["no", interaction.user]);
response.delete();
collector.stop();
}
});
collector.on('end', async () => {
});
}