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

183
Visualizações
Need the bot to not heal user if their health is full

I've recently made this "heal" command that increases health by increments of ten when used. However, I'd like to try and cap the health at 100 so users aren't just accumulating it. Below is the code and an attempt I made.

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);
    }
    }
};

Basically the goal would be that the bot checks the mentioned user's health and if it is at 100, the command cannot be performed on them. Any advice would help.

Edit: The console log gives ReferenceError: targetData is not defined at if(targetData.health === 100) return message.channel.send('This user is at already at max health'); . When I execute the command, nothings happens, as opposed to the mentioned user getting healed (their health# is not at 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);
    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since you are trying to check the equality between a variable and a number and not assigning a number to a variable you need to use double equals:

if(targetData.health == 100) {}
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