最近,我发出了这个“治疗”命令,在使用时增加10点的生命值。然而,我想尝试将健康度限制在100,这样用户就不会只是积累起来了。下面是我所做的代码和尝试。
const profileModel = require("../models/profileSchema");
module.exports = {
name: "heal",
description: "heal a user's health by 10",
cooldown: 120,
async execute(client, message, args, cmd, discord, profileData) {
if (!message.member.roles.cache.has('919453438813823017')) return message.channel.send("You aren't a medic!");
if (!args.length) return message.channel.send("You need to mention the user you are trying to heal.");
const Number = 10;
const target = message.mentions.users.first();
if (!target) return message.channel.send("This user does not exist.");
try {
const targetData = await profileModel.findOne({ userID: target.id });
if (!targetData) return message.channel.send(`This user doens't exist in the db`);
///
if(targetData.health = 100) return message.channel.send('This user is at already at max health');
///
await profileModel.findOneAndUpdate(
{
userID: target.id,
},
{
$inc: {
health: Number,
},
}
);
return message.channel.send(`${target}'s health has been healed for ${Number}!`);
} catch (err) {
console.log(err);
}
}
};基本上,目标是机器人检查上述用户的健康状况,如果是100,则不能对其执行命令。任何建议都会有帮助。
编辑:控制台日志给出了ReferenceError: targetData没有在if(targetData.health === 100) return message.channel.send('This user is at already at max health');中定义。当我执行该命令时,不会发生任何事情,而不是上述的用户被治愈(他们的health#不是100)。
const mongoose = require("mongoose");
const profileSchema = new mongoose.Schema({
name: { type: String, require: true, unique: true },
userID: { type: String, require: true, unique: true },
serverID: { type: String, require: true },
reputation: { type: Number, default: 0 },
health: { type: Number, deafult: 100},
});
const model = mongoose.model("ProfileModels", profileSchema);
module.exports = model; let profileData;
try {
profileData = await profileModel.findOne({ userID: message.author.id });
if(!profileData){
let profile = await profileModel.create({
name: message.author.id,
userID: message.author.id,
serverID: message.guild.id,
reputation: 0,
health: 100,
});
profile.save();
}
} catch (err) {
console.log(err);
}发布于 2021-12-15 13:09:06
由于您试图检查变量和数字之间的相等性,而不是将数字分配给变量,所以需要使用双等号。
if(targetData.health == 100) {}https://stackoverflow.com/questions/70362037
复制相似问题