I am part of a team that runs a small wiki for a game 99% of the population never heard of (don’t ask me what it is), so I wanted to make a discord bot that used our API to pull info quickly, how it works is you do /search name:[enter name of item] and you get a list of, at most, the top 5 search results with emojis at the bottom to react with, then the bot should spit out another embed message with item description and stats, etc. The issue is that for the life of me, I can’t seem to make the bot recognize what emoji the user has selected, as it always just returns ‘0’ emojis selected. I am noob at JS, plz help

Here is what I got: `

async execute(interaction) {
    const query = interaction.options.getString("name");

    let itemData;

    try {
        // Get data from api
        const itemAPIList = await fetch(
            `the link for our api, not important q=${query}`
        ).then((res) => res.json());
        itemData = itemAPIList.data;

        const emojis = ["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣"];

        // Check if itemData from api is not empty
        if (itemData.length > 0) {
            // Create the embed message
            const itemEmbed = new EmbedBuilder()
                .setColor(0x00ff80)
                .setDescription(
                    "React with the corresponding emoji to select an item"
                );
            // List out the top 5 results from api with an emoji next to it
            for (let i = 0; i < 5 && i < itemData.length; i++) {
                itemEmbed.addFields({
                    name: `${emojis[i]}: ${itemData[i].name}`,
                    value: " ",
                });
            }
            // Sends the embedded message
            const sentItemList = await interaction
                .reply({
                    embeds: [itemEmbed],
                    fetchReply: true,
                })
                .then(async (itemListMsg) => {
                    // Reacts to the message with correct number of emoji numbers
                    for (
                        let i = 0;
                        i < emojis.length && i < itemData.length;
                        i++
                    ) {
                        await itemListMsg.react(emojis[i]);
                    }

                    // Filter for checking if emoji and user are correct
                    const collectorFilter = (reaction, user) => {
                        return (
                            emojis.includes(reaction.emoji.name) &&
                            user.id === interaction.user.id
                        );
                    };

                    const collector =
                        await itemListMsg.createReactionCollector({
                            filter: collectorFilter,
                            time: 5000,
                        });

                    collector.on("collect", (reaction, user) => {
                        console.log(
                            `Collected ${reaction.emoji.name} from ${user.tag}`
                        );
                    });

                    collector.on("end", (collected) => {
                        console.log(`Collected ${collected.size} items`);
                    });
                });
        }

`

  • SatouKazuma@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    edit-2
    11 months ago

    I’m going to follow this post. I’m not the best with JS myself (trying to improve at it as I get ready to apply for some senior engineer positions soon), but intuitively from my own reading of this code, I want to say it’s your collection filter function. Why are you checking reaction emoji names? Wouldn’t it be easier to check if the user reacts with is in the emojis array?