Недавно я сделал эту команду, где у пользователей есть 15 секунд, чтобы ввести «поймать», чтобы выиграть монеты. Единственная проблема в том, что я не уверен, как сделать так, чтобы монеты доставались тому, кто первым набрал «поймать». Сейчас он настроен так, что монеты всегда достаются тому, кто запустил команду. Я пытался использовать руководство discord.js для коллекционеров, но продолжал получать ошибки. Я все еще довольно новичок в этом, что-нибудь поможет, спасибо.
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!`);
}
}
Схема профиля на всякий случай
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.info(err);
}



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Похоже, ваш сборщик использует две разные переменные для сообщения:
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,
},
}
);
});
Вы проверяете, содержит ли сообщение m (собранное вашим сборщиком) содержимое 'catch', а затем находите идентификатор пользователя, который проверяет автора message (из ваших параметров execute() вверху, поэтому сообщение которая вызвала эту команду), когда вы должны использовать m.
Итак, чтобы исправить это, вы должны изменить userID: message.author.id на userID: m.author.id