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