this post was submitted on 21 Dec 2023
8 points (90.0% liked)

JavaScript

2003 readers
1 users here now

founded 1 year ago
MODERATORS
 

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`);
                    });
                });
        }

`

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 11 months ago* (last edited 11 months ago)

If you're curious to my solution, ditched reactions for this: https://discordjs.guide/message-components/select-menus.html