How to refresh Select Menu after interaction?

Hello! I'm trying create a role select menu that refreshes after after a selection so that people can select a role and then immeadietly drop that role if they want to. Currently it working so that you have to add another role before you can drop your current role. Here's a video on how it's working right now. https://cdn.discordapp.com/attachments/891316170165194842/1166375404358078625/Recording_2023-10-23_235148.mp4?ex=654a42be&is=6537cdbe&hm=8216478a1a39f06ab85c5727bf031341333c8a90e77c52c75e0cd58952b22b4f& Here is the current code that handles the interaction:
client.on(Events.InteractionCreate, async interaction => {
const role = interaction.guild.roles.cache.get(interaction.values[0]);
if (!role) {
interaction.reply({
content: 'I could not find that role.',
ephemeral: true,
});
return;
}

const hasRole = interaction.member.roles.cache.has(role.id);

if (hasRole) {
await interaction.member.roles.remove(role);
await interaction.reply({
content: `The role ${role} has been removed.`,
ephemeral: true,
});
return;
}

await interaction.member.roles.add(role);
await interaction.reply({
content:`The role ${role} has been added.`,
ephemeral:true,
});
});
client.on(Events.InteractionCreate, async interaction => {
const role = interaction.guild.roles.cache.get(interaction.values[0]);
if (!role) {
interaction.reply({
content: 'I could not find that role.',
ephemeral: true,
});
return;
}

const hasRole = interaction.member.roles.cache.has(role.id);

if (hasRole) {
await interaction.member.roles.remove(role);
await interaction.reply({
content: `The role ${role} has been removed.`,
ephemeral: true,
});
return;
}

await interaction.member.roles.add(role);
await interaction.reply({
content:`The role ${role} has been added.`,
ephemeral:true,
});
});
And here is the code that I used to send the message to the channel. I would like to keep it so that it is a permanant message in a channel and not one that is recreated with a command or something.
client.on('ready', async (c) => {
try {
const channel = client.channels.cache.get('x');

const roleMenu = new RoleSelectMenuBuilder()
.setCustomId('x')
.setMinValues(0)
.setMaxValues(1);

const row = new ActionRowBuilder().setComponents(roleMenu);

await channel.send({
content: 'Add or remove role from the drop down menu.',
components: [row],
});

return;
}
catch (error) {
console.log(error);

return;
}
});
client.on('ready', async (c) => {
try {
const channel = client.channels.cache.get('x');

const roleMenu = new RoleSelectMenuBuilder()
.setCustomId('x')
.setMinValues(0)
.setMaxValues(1);

const row = new ActionRowBuilder().setComponents(roleMenu);

await channel.send({
content: 'Add or remove role from the drop down menu.',
components: [row],
});

return;
}
catch (error) {
console.log(error);

return;
}
});
Thanks for any help!
2 Replies
d.js toolkit
d.js toolkit15mo 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
Kat
KatOP15mo ago
ubuntu@DESKTOP-T5R5SLE:~$ npm list discord.js
ubuntu@ /home/ubuntu
ubuntu@DESKTOP-T5R5SLE:~$ npm list discord.js
ubuntu@ /home/ubuntu
ubuntu@DESKTOP-T5R5SLE:~$ node -v
v21.0.0
ubuntu@DESKTOP-T5R5SLE:~$ node -v
v21.0.0
Want results from more Discord servers?
Add your server