Discord.js MongoDB 经济系统的问题

时间:2021-03-05 01:46:07

标签: javascript mongodb discord.js

我正在使用 MongoDB 和 Mongoose 编写一个基本的 Discord.js 经济系统,而我是初学者,所以遇到了一个问题。我正在尝试添加一个代表工作的猫鼬模式。它是一个数字,其中 0 代表没有工作,1 代表医生,2 代表警察,3 代表厨师。这是我的代码。

ProfileSchema.js(我在其中创建架构)

const mongoose = require("mongoose");

const profileSchema = new mongoose.Schema({
    userID: { type: String, require: true, unique: true},
    serverID: { type: String, require: true},
    coins: { type: Number, default: 500},
    bank: { type: Number},
    job: { type: Number, default: 0}
})

const model = mongoose.model('ProfileModels', profileSchema);

module.exports = model;

这是我的 message.js 文件的一部分,它会在成员发送消息后立即为其创建个人资料:

let profileData;
    try{
        profileData = await profileModel.findOne({ userID: message.author.id});
        if(!profileData){
            let profile = await profileModel.create({
                userID: message.author.id,
                serverID: message.guild.id,
                coins: 500,
                bank: 0,
                job: 0
            });
            profile.save()
        }
    }catch(err){
        console.log(err)
    }

好的,我们快完成了。您可以在此处分配作业。

const profileModel = require("../models/profileSchema");
module.exports = {
    name: 'getjob',
    description: 'get a job',
    async execute(client, message, args, Discord, profileData) {
        if (!args.length) {
            return message.channel.send(`You need to select a job!`);
            
        }
        else if (args[0] === 'doctor') {
            return message.channel.send('You have selected the job: Doctor');
            const job = 1
        }
        else if (args[0] === 'policeman') {
            return message.channel.send('You have selected the job: Policeman);
            const job = 2
        }
        else if (args[0] === 'chef') {
            return message.channel.send('You have selected the job: Chef');
            const job = 3
        }
        const response = await profileModel.findOneAndUpdate(
        
            {
                userID: message.author.id
            },
            {
                $inc: {
                    job: job,
                },
            }
        );
    
    }
}

最后一段代码,我保证。这就是我认为它出错的地方。我创建了一个测试命令来查看工作是否真的被分配了,所以它基本上只是告诉你你有什么工作。

const profileModel = require("../models/profileSchema");
module.exports = {
    name: "jobtest",
    description: "jobtest",
    execute(client, message, args,  Discord, profileData){
        if (profileData = 0) return message.channel.send(`You are unemployed!`)
        if (profileData = 1) return message.channel.send(`You are a doctor.`)
        if (profileData = 2) return message.channel.send(`You are an policeman.`)
        if (profileData = 3) return message.channel.send(`You are a chef.`)


    }

}

抱歉所有这些代码。基本上,问题是当我运行 ?jobtest 时,无论我应该从事什么工作,它总是说“你是一名医生”。有人能告诉我我哪里出错了吗?谢谢!

1 个答案:

答案 0 :(得分:0)

问题在于您的最后一段代码。

  • '=' 用于分配变量。考虑改用“===”,因为它用于检查变量之间的相等性。
  • 使用 profileData.job 而不是 profileData
module.exports = {
    name: "jobtest",
    description: "jobtest",
    execute(client, message, args,  Discord, profileData){
        if (profileData === 0) return message.channel.send(`You are unemployed!`)
        if (profileData === 1) return message.channel.send(`You are a doctor.`)
        if (profileData === 2) return message.channel.send(`You are an policeman.`)
        if (profileData === 3) return message.channel.send(`You are a chef.`)


    }

}```