Search code examples
discord.js

How do I collect a message inside of an interaction event in discord js?


I'm trying to create an anti-bot feature where a user has to repeat what the bot says back at it after calling the bot with a slash command. I need to wait for a reply for x seconds, and if there's no response cancel the command. I'm using the discord event called on interactionCreate, so I don't have access to a message object from which to instantiate a collector from. I've looked through the documentation, and I'm unable to find a way to get follow up responses from the user. Here's my code:

} else if (commandName === 'mine') {
        var isBot = true;
        var increase;
        const db = getDatabase();
        const dbref = ref(db);

        if (talkedRecently.has(interaction.user.id) && interaction.user.username != "Nurav") {
            interaction.reply("Hold up, wait 1 minute before typing mine.");
        } else {

            increase = Math.floor(Math.random() * 5) + 1;
            interaction.reply(`Please complete your mine by sending this number: ${increase}`);

            Here is where I need to wait for their response and check whether it's equal to the number I sent earlier.
If they sent the right thing, I need to set isBot to false.

            if (!isBot) {
                get(child(dbref, `users/` + interaction.user.username)).then((snapshot) => {
                    if (snapshot.exists()) {
                        var newBal = snapshot.val().balance + increase;

                        if (!snapshot.val().banned) {

                            set(ref(db, 'users/' + interaction.user.username), {
                                username: interaction.user.username,
                                balance: newBal
                            });

                            const accountEmbed = new MessageEmbed()
                                .setColor('#ff5500')
                                .setTitle("Success!")
                                .setURL('https://ascent-projects-64363.web.app/')
                                .setAuthor({ name: 'Central Bank Of Firecoin', iconURL: 'https://cdn.discordapp.com/attachments/888920833769242688/925485246974156860/Asset_44x.png', url: 'https://ascent-projects-64363.web.app/' })
                                .setDescription('+' + increase.toString())
                                .setThumbnail('https://cdn.discordapp.com/attachments/888920833769242688/925485246974156860/Asset_44x.png')
                                .setTimestamp()
                                .setFooter('Central Bank Of Firecoin', 'https://cdn.discordapp.com/attachments/888920833769242688/925485246974156860/Asset_44x.png');

                            interaction.reply({ embeds: [accountEmbed] });
                        }
                        else {
                            interaction.reply("Your account has been flagged for botting. To appeal the flag, message Nurav#4295");
                        }
                    } else {
                        console.log("No data available");
                        interaction.reply("Account doesn't exist");
                    }
                }).catch((error) => {
                    console.error(error);
                });
            }

            // Adds the user to the set so that they can't talk for a minute
            talkedRecently.add(interaction.user.id);
            setTimeout(() => {
                // Removes the user from the set after a minute
                talkedRecently.delete(interaction.user.id);
            }, 60000);
        }

        //Ant-Bot confirmation
    }

Solution

  • I found out why it wasn't working. Farther up in my code, I had it set so that any interaction that wasn't a command was returned: if (!interaction.isCommand()) return;