首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果用户的健康状况完全正常,则需要机器人不能治愈他们。

如果用户的健康状况完全正常,则需要机器人不能治愈他们。
EN

Stack Overflow用户
提问于 2021-12-15 10:24:35
回答 1查看 64关注 0票数 0

最近,我发出了这个“治疗”命令,在使用时增加10点的生命值。然而,我想尝试将健康度限制在100,这样用户就不会只是积累起来了。下面是我所做的代码和尝试。

代码语言:javascript
复制
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)。

代码语言:javascript
复制
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;
代码语言:javascript
复制
   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);
    }
EN

回答 1

Stack Overflow用户

发布于 2021-12-15 13:09:06

由于您试图检查变量和数字之间的相等性,而不是将数字分配给变量,所以需要使用双等号

代码语言:javascript
复制
if(targetData.health == 100) {}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70362037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档