Is there a way I can make it so you can't use a command if your level is higher than 5?

Ok so, Im making an rpg in discord.js using mongodb but I want to make it so if the users level is over 5 they can’t use the training grounds. The thing is, the level schema and stats schema are in different files, how would I make it so they can’t use the training grounds?

Profile Schema:

const mongoose = require('mongoose');

const yenSchema = mongoose.Schema({
    guildID: String,
    guildName: String,
    name: String,
    userID: String,
    lb: {
        type: String,
        default: "all"
    },
    yen: {
        type: Number,
        default: 0
    },
    daily: {
        type: Number,
        default: 0
    },
    xp: {
        type: Number,
        default: 0
    },
    level: {
        type: Number,
        default: 1
    },
    rank: {
        type: Number,
        default: 1
    },
    gear: {
        type: String,
        default: "None"
    },
    class: {
        type: String,
            default: "None"
    }
})

module.exports = mongoose.model("Yen", yenSchema)

stats schema:

const mongoose = require('mongoose');

const statsSchema = mongoose.Schema({
    guildID: String,
    guildName: String,
    name: String,
    userID: String,
    statslb: {
        type: String,
        default: "all"
    },
    strength: {
        type: Number,
        default: 0
    },
    speed: {
        type: Number,
        default: 0
    },
    agility: {
        type: Number,
        default: 0
    },
    defense: {
        type: Number,
        default: 0
    },
    trainTimeout: {
        type: Number,
        default: 0
    },
    health: {
        type: Number,
        default: 100
    },
    maxHealth: {
        type: Number,
        default: 100
    },
})

module.exports = mongoose.model("Stats", statsSchema)

A snippit of the training command:

const {
    MessageEmbed,
    Message
} = require('discord.js')
const mongoose = require('mongoose')
const config = require('../../config.json')
const ms = require('parse-ms')

mongoose.connect(config.mongodbURL, {
    useNewUrlParser: true,
    useUnifiedTopology: true
})
const Stats = require('../../models/stats');
const Yen = require('../../models/yen')

 if (!args[0]) {
            message.reply(`Please do ${config.prefix}train strength, speed, agility or defense`)
        }

        const user = message.author

        let statsreward = Math.floor(Math.random() * 3) + 1;
        const timeout = 43200000

        if (args[0] == "strength") {
            Stats.findOne({
                guildID: bot.guilds.cache.get(message.guild.id).id,
                userID: user.id
            }, (err, data) => {
                if (err) console.log(err)

                if (!data) {
                    const newData = new Stats({
                        guildID: bot.guilds.cache.get(message.guild.id).id,
                        guildName: message.guild.name,
                        name: bot.users.cache.get(user.id).username,
                        userID: user.id,
                        statslb: "all",
                        strength: statsreward,
                        speed: 0,
                        agility: 0,
                        defense: 0,
                        trainTimeout: Date.now(),
                        health: 100,
                        maxhealth: 100,
                    })
                    newData.save().catch(err => console.log(err));

                    let embed = new MessageEmbed()
                        .setColor("RANDOM")
                        .setDescription(`You have trained and your strength stat went up by **+${statsreward}**`)

                    return message.channel.send(embed)
                } else {
                    if (timeout - (Date.now() - data.trainTimeout) > 0) {
                        let time = ms(timeout - (Date.now() - data.trainTimeout))

                        let timelimit = new MessageEmbed()
                            .setDescription(`**You already trained!**`)
                            .addField(`Train again in: `, `**${time.hours}h ${time.minutes}m ${time.seconds}s**`)
                            .setColor("RANDOM")
                        return message.channel.send(timelimit)

                    } else {
                        data.strength += statsreward;
                        data.trainTimeout = Date.now();
                        data.save().catch(err => console.log(err));



                        let trained = new MessageEmbed()
                            .setDescription(`You have trained and your strength stat went up by **+${statsreward}** (**${data.strength.toLocaleString()}** strength total)`)
                            .setColor("#75beff")
                        return message.channel.send(trained)
                    }
                }
            })
        }

So again, as you can see, in the stats schema there is no level, its just the stats and some other stuff, how would I make it so it can get the level and if the level is higher than 5, it will return a message saying that you can’t use the training grounds?

Can anyone help?

You would just check. It may look like

if(!user.rank >= 5) return message.reply("You don\'t have a high enough rank to usr this command")

How would I get the rank of the user tho???

It is your code. You just need to get the model and use findOne.

Where should I put it?

I can’t spoon feed you. You can put it were ever it seems fit. I recommend at the beginning of the command.

Oh, yep. I forgot to tell you I just figured it out.

2 Likes
        const yendata = await Yen.findOne({
            guildID: bot.guilds.cache.get(message.guild.id).id,
            userID: user.id
        }, (err) => {
            if (err) console.log(err)
        })
            if (yendata.level > 5) {
                message.reply("**You cannot use the training room after level 5**");
            } else {
                if (yendata.xp > 500) {
                    message.reply(`**You can't trick me! You have ${yendata.xp.toLocaleString()} xp! (You can't use the training room after level 5)**`)
                } else {

//My Code

     }
}

Use => 6 instead of >. However, switch out the the 500 number to maybe 401 and use => since they would have a higher experience than 401 in level 5.

That first else should be else if

        const yendata = await Yen.findOne({
            guildID: bot.guilds.cache.get(message.guild.id).id,
            userID: user.id
        }, (err) => {
            if (err) console.log(err)
        })
            if (yendata.level > 5) {
                message.reply("**You cannot use the training room after level 5**");
            } else if(yendata.xp > 500) {
                    message.reply(`**You can't trick me! You have ${yendata.xp.toLocaleString()} xp! (You can't use the training room after level 5)**`)
                } else {

//My Code

     }
}