disabling an interaction menu

im just trying to disable an interaction after input from a player so that the messages can be properly deleted
27 Replies
d.js toolkit
d.js toolkit13mo 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.
Xu Xiaolan
Xu Xiaolan13mo ago
async function switchMenu(userId, message, battleMessage, userCards, embeds, time, callback) {
const select = new StringSelectMenuBuilder()
.setCustomId('Team switch:')
.setPlaceholder('Select your card')
for (let i = 0; i < userCards.length; i++) {
select.addOptions(
{
label: `${userCards[i].cardName}`,
description: `Test for ${userCards[i].cardName} card.`,
value: `${userCards[i].slot}`
}
);
}

const row = new ActionRowBuilder()
.addComponents(select);

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 => {
try {
if (interaction.values && interaction.values.length > 0) {
let selectedcard = parseInt(interaction.values[0]);
console.log(selectedcard);
callback([selectedcard, interaction.user]);

// Disabling the select menu
select.setDisabled(true);
await interaction.reply(`You have switched to slot # ${selectedcard} ${userCards[selectedcard - 1].cardName}`);
// Deleting the response
await response.delete;
}
} catch (err) {
console.log(err);
}
});

collector.on('end', async () => {
});
}
async function switchMenu(userId, message, battleMessage, userCards, embeds, time, callback) {
const select = new StringSelectMenuBuilder()
.setCustomId('Team switch:')
.setPlaceholder('Select your card')
for (let i = 0; i < userCards.length; i++) {
select.addOptions(
{
label: `${userCards[i].cardName}`,
description: `Test for ${userCards[i].cardName} card.`,
value: `${userCards[i].slot}`
}
);
}

const row = new ActionRowBuilder()
.addComponents(select);

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 => {
try {
if (interaction.values && interaction.values.length > 0) {
let selectedcard = parseInt(interaction.values[0]);
console.log(selectedcard);
callback([selectedcard, interaction.user]);

// Disabling the select menu
select.setDisabled(true);
await interaction.reply(`You have switched to slot # ${selectedcard} ${userCards[selectedcard - 1].cardName}`);
// Deleting the response
await response.delete;
}
} catch (err) {
console.log(err);
}
});

collector.on('end', async () => {
});
}
this code currently doesnt end the interaction menu and doesnt delete it either
treble/luna
treble/luna13mo ago
you run .setDisabled but never update your selectmenu
Xu Xiaolan
Xu Xiaolan13mo ago
select.setDisabled(true); row.addComponents(select); await interaction.message.edit({components: [row]}); something like this?
treble/luna
treble/luna13mo ago
assuming your row is a new actianrowbuilder it should work
Xu Xiaolan
Xu Xiaolan13mo ago
im getting unknown message error
Xu Xiaolan
Xu Xiaolan13mo ago
specifically on line 307
Xu Xiaolan
Xu Xiaolan13mo ago
the one with interaction.message.edit
treble/luna
treble/luna13mo ago
because you never reply, only in your collector
Xu Xiaolan
Xu Xiaolan13mo ago
sorry im a bit dum, wdym by that?
treble/luna
treble/luna13mo ago
you use message.reply, but never interaction.reply
Xu Xiaolan
Xu Xiaolan13mo ago
oh, what should i do then?
treble/luna
treble/luna13mo ago
resolve the promise that it returns and edit that also, response.delete isnt a property but a method and i dont see response being defined anywhere nvm
Xu Xiaolan
Xu Xiaolan13mo ago
const response = await message.reply({ embeds: [embed], components: [row], ephemeral: true it was defined oh lol
treble/luna
treble/luna13mo ago
you need to edit that and what is message? An interaction or a regular message?
Xu Xiaolan
Xu Xiaolan13mo ago
an interaction, cuz its supposed to pop up an ephemeral select menu for the user to pick an option, then disable the menu and delete it for them
treble/luna
treble/luna13mo ago
use response.edit
Xu Xiaolan
Xu Xiaolan13mo ago
now im getting an error about invalid form body, component custom id cannot be duplicated, that means im using the same custom id as another row actionrowbuilder right?
treble/luna
treble/luna13mo ago
create a new actionrowbuilder or response.components[0].data.disabled = true
Xu Xiaolan
Xu Xiaolan13mo ago
is one method more efficient than the other or does it not matter too much?
treble/luna
treble/luna13mo ago
It depends on how you prefer but you have to wrap it in a new actionrow
Xu Xiaolan
Xu Xiaolan13mo ago
// Disabling the select menu and wrapping it into a new ActionRowBuilder
select.setDisabled(true);
const newRow = new ActionRowBuilder()
.addComponents(select);
await response.edit({components: [newRow]});
await interaction.reply(`You have switched to slot # ${selectedcard} ${userCards[selectedcard - 1].cardName}`);
// Disabling the select menu and wrapping it into a new ActionRowBuilder
select.setDisabled(true);
const newRow = new ActionRowBuilder()
.addComponents(select);
await response.edit({components: [newRow]});
await interaction.reply(`You have switched to slot # ${selectedcard} ${userCards[selectedcard - 1].cardName}`);
this just almost works the only problem i've been having a lot is that, when i use the select menu again, its like it runs the code twice in a row so when it runs smthn like await response.delete(); it just gives me unknown message error cuz theres no message to delete on the first iteration it works fine, afterwards it breaks like that
treble/luna
treble/luna13mo ago
why are you setting your selectmenu to disabled if you delete the message anyways?
Xu Xiaolan
Xu Xiaolan13mo ago
cuz theres a button to open the selectMenu again, that they can press after a turn has passed, so they can open the selectmenu again after the turn ends for both players the first turn goes fine without any errors, but after that if user 1 or 2 uses the selectMenu more than once, i get the blue errors about unknown message, and if both players have used selectmenu more than once, i get the red unkown message error its really weird i cant wrap my head around it
Xu Xiaolan
Xu Xiaolan13mo ago
this is how it looks like, the other buttons work, switch opens up the switch menu, which is a select menu for cards i put in an array, all works just fine except all the unknown message errors im getting, they feel really random too, it never happens on the two player's first switch, but afterwards i always get this error
Xu Xiaolan
Xu Xiaolan13mo ago
Xu Xiaolan
Xu Xiaolan13mo ago
i figured it out, i must've worded my question wrong sorry, i wanted the collector to end so i didnt realise that there was a collector.stop() thanks for dealing with this huge mess lol