skip to Main Content

EDIT: Fixed the code as per waki285’s answer. The problem still persists.

As per the discord.js guide on component interactions, I’ve been trying to get my select menu to work using an .awaitMessageComponent() call on the interaction reply. This is my code:

const selectMenu = new StringSelectMenuBuilder()
    .setCustomId("roleselect")
    .setPlaceholder("Select your role!")
    .addOptions(
        { label: "role1", value: "role1" },
        { label: "role2", value: "role2" }
    );

const prompt = await this.interaction.reply({
    content: "Please select your role!",
    ephemeral: true,
    components: [
        new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(
            selectMenu
        ),
    ],
});

const menuInteraction = await prompt.awaitMessageComponent({
    filter: (interaction) =>
        interaction.user.id === this.interaction.user.id,
    time: 600 * 1000,
    componentType: ComponentType.StringSelect,
});

const selectedRoleValue = menuInteraction.values[0];

The select menu renders just fine:

enter image description here

But after choosing an option, the interaction fails and the bot never gets to the selectedRoleValue assignment line:

enter image description here

The interaction IS being picked up by my client.on("interactionCreate", ...) code (where it is logged and ignored), but it is not picked up by the prompt.awaitMessageComponent() code. I tried the equivalent code but with a component collector instead and that code also didn’t pick up any interactions.

Any help would be greatly appreciated!

2

Answers


  1. Chosen as BEST ANSWER

    I figured out the problem. I am unsure why this is an issue, but the issue lied with the fact that I was using this.interaction.reply(). For some reason, the awaitMessageComponent() was not listening for any more interactions after that, even though that's how it's written in the documentation.

    I updated it so that it instead uses this.interaction.followUp() (and made sure that somewhere earlier to that, I ran this.interaction.deferReply()). This worked.

    waki285's answer did point out a valid issue in the first version of the code though, which was that my filter was originally

    interaction.user.id === this.interaction.id
    

    instead of

    interaction.user.id === this.interaction.user.id
    

    which would have also caused it to fail.


  2. this.interaction.id is NOT interacted user id but id used to handle interactions internally.

    So try this code:

    const menuInteraction = await prompt.awaitMessageComponent({
        filter: (interaction) =>
            interaction.user.id === this.interaction.user.id,
        time: 600 * 1000,
        componentType: ComponentType.StringSelect,
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search