Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
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);
    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda