Search code examples
javascriptdiscorddiscord.js

Collecting the first one to respond's userID


I recently made this command where users have 15 seconds to type 'catch' to win coins. The only issue is that I'm not sure how to make it so that the coins go to the person who typed 'catch' first. Right now it's set up so that the coins always goes to the person who triggered the command. I tried using the discord.js guide for collectors but I kept getting errors. I'm still pretty new at this, anything helps thanks.

const profileModel = require("../models/profileSchema");

module.exports = {
    name: "catch",
    description: "users must type catch first to catch the animal",
    async execute(client, message, msg, args, cmd, Discord, profileData) {
        
        const prey = [
            "rabbit",
            "rat",
            "bird",
        ];

        const caught = [
            "catch",
        ];

        const chosenPrey = prey.sort(() => Math.random() - Math.random()).slice(0, 1);
        const whenCaught = caught.sort(() => Math.random() - Math.random()).slice(0, 1);
        const earnings = Math.floor(Math.random() * (20 - 5 + 1)) + 5;

        const filter = ({ content }) => whenCaught.some((caught) => caught.toLowerCase() == content.toLowerCase());

        const collector = message.channel.createMessageCollector({ max: 1, filter, time: 15000 });

        collector.on('collect', async (m) => {
            if(m.content?.toLowerCase() === 'catch') {
                message.channel.send(`You caught the ${chosenPrey}! You gained ${earnings} coins.`);
            }
            
            await profileModel.findOneAndUpdate(
                {
                    userID: message.author.id,
                },
                {
                    $inc: {
                        coins: earnings,
                    },
                }
            );
        });

        collector.on('end', (collected, reason) => {
            if (reason == "time") {
                message.channel.send('Too slow');
            }
        });

        message.channel.send(`Look out, a ${chosenPrey}! Type CATCH before it gets away!`);
    }
}

The profileSchema just in case

const profileModel = require("../../models/profileSchema");

const cooldowns = new Map();
module.exports = async (Discord, client, message) => {

    let profileData;
    try {
        profileData = await profileModel.findOne({ userID: message.author.id });
        if(!profileData){
            let profile = await profileModel.create({
                name: message.member.user.tag,
                userID: message.author.id, 
                serverID: message.guild.id, 
                coins: 0,
            });
            profile.save();
        }
    } catch (err) {
      console.log(err);
    }

Solution

  • It looks like your collector is using two different variables for message:

            collector.on('collect', async (m) => {
                if(m.content?.toLowerCase() === 'catch') {
                    message.channel.send(`You caught the ${chosenPrey}! You gained ${earnings} coins.`);
                }
                
                await profileModel.findOneAndUpdate(
                    {
                        userID: message.author.id,
                    },
                    {
                        $inc: {
                            coins: earnings,
                        },
                    }
                );
            });
    

    You're checking whether the message m (collected by your collector) has the content 'catch', and then finding the userID of the user that checking the author of the message (from your execute() parameters up top, so, the message that triggered this command) when you should be using m.

    So, to fix this, you should change userID: message.author.id to userID: m.author.id