收集第一个响应的用户 ID
Collecting the first one to respond's userID
我最近做了这个命令,用户有 15 秒的时间输入 'catch' 来赢得金币。唯一的问题是,我不确定如何才能让硬币送给第一个输入 'catch' 的人。现在它已经设置好,硬币总是交给触发命令的人。我尝试使用 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!`);
}
}
profileSchema 以防万一
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);
}
您的收集器似乎对消息使用了两个不同的变量:
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',然后找到检查 [=12= 作者的用户的用户 ID ] (从你的 execute() 参数开始,所以,触发这个命令的消息)当你应该使用 m
.
因此,要解决此问题,您应该将 userID: message.author.id
更改为 userID: m.author.id
我最近做了这个命令,用户有 15 秒的时间输入 'catch' 来赢得金币。唯一的问题是,我不确定如何才能让硬币送给第一个输入 'catch' 的人。现在它已经设置好,硬币总是交给触发命令的人。我尝试使用 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!`);
}
}
profileSchema 以防万一
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);
}
您的收集器似乎对消息使用了两个不同的变量:
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',然后找到检查 [=12= 作者的用户的用户 ID ] (从你的 execute() 参数开始,所以,触发这个命令的消息)当你应该使用 m
.
因此,要解决此问题,您应该将 userID: message.author.id
更改为 userID: m.author.id