简体   繁体   English

Node.js + Discord.js:无法读取未定义的属性“类”

[英]Node.js + Discord.js: Cannot read property 'class' of undefined

I have a problem with variables.我对变量有疑问。 I am working on a Discord RPG bot, but ran into a problem with player data.我正在研究 Discord RPG 机器人,但遇到了玩家数据问题。 When I start the bot up with the console and sent a message to build my stats, the console would return当我使用控制台启动机器人并发送消息以构建我的统计信息时,控制台将返回

C:\Users\willi\OneDrive\Desktop\discord-bot\homies.js:395
                    player[id].class = reply;
                                     ^

TypeError: Cannot set property 'class' of undefined

I use the below code.我使用下面的代码。 This is a fragment of the whole program.这是整个程序的一个片段。 All files required by this exists.这需要的所有文件都存在。

const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const client = new Discord.Client();
const fs = require('fs');

const player = fs.readFileSync('user.json', 'utf-8');

client.on('message', message => {
    var id = message.author.id;

    if (player[id] === undefined) {
            player[id] = {
                username: message.author.username,
                class: undefined,
                xp: 0,
                lvl: 1,
                xpReq: 100 + this.lvl * 75,
                hp: 0,
                maxHp: 0,
                str: 0,
                def: 0,
                int: 0,
                dex: 0,
                luck: 0,
                mana: 0,
                maxMana: 0,
                equip: {
                    top: undefined,
                    bottom: undefined,
                    hand: undefined,
                    acc1: undefined,
                    acc2: undefined
                },
                spellcaster: undefined,
                spells: []
            };
            var authorVerifier = m => m.author.id === id;
            var classAnswer = message.channel.createMessageCollector(authorVerifier, { time: 300000, max: 1 });
            var classInterpreter = function(reply) {
                if (reply !== 'fighter' && reply !== 'adventurer' && reply !== 'mage'
                && reply !== 'healer' && reply !== 'rouge' && reply !== 'tank') {
                    return message.channel.send('That was not a valid class!');
                }
                else {
                    player[id].class = reply;
                    fs.writeFileSync('user.json', player[id], (err) => {
                        if (err) throw err;
                    });
                }
            }
        classAnswer.on('collect', m => classInterpreter(m.content));
    }
});

I tried replacing id with message.author.id , but to no avail.我尝试用message.author.id替换id ,但无济于事。 Please help!请帮忙!

You can solve it by passing the player array to the classInterpreter function, and use that to set player[id].class value.您可以通过将播放器数组传递给classInterpreter function 来解决它,并使用它来设置player[id].class值。

classAnswer.on('collect', m => classInterpreter(m.content, player));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Node.js、Mongoose 和 Discord.js 的未定义错误 [无法读取未定义的属性] - Undefined errors using Node.js, Mongoose, and Discord.js [Cannot read property of undefined] node.js discord.js 无法读取未定义的属性“发送” - node.js discord.js cannot read property 'send' of undefined JavaScript,Discord.js,Node.js TypeError:无法读取未定义的属性“执行” - JavaScript,Discord.js, Node.js TypeError: Cannot read property 'execute' of undefined Node.js Discord.js UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“calculatedPosition” - Node.js Discord.js UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'calculatedPosition' of undefined 类型错误:无法读取未定义 Discord.js node.js v12.16.3 的属性“第一个” - TypeError: Cannot read property 'first' of undefined Discord.js node.js v12.16.3 node.js/discord.js:类型错误:无法读取 null 的属性“setPresence” - node.js/discord.js: TypeError: Cannot read property 'setPresence' of null discord.js | 无法读取未定义的属性“设置” - discord.js | Cannot read property 'set' of undefined 类型错误:无法读取未定义 Discord.js javascript 的属性“添加” - TypeError: Cannot read property 'add' of undefined Discord.js javascript 无法读取未定义的 discord.js 警告命令的属性“发送” - Cannot read property 'send' of undefined discord.js warn command discord.js 无法读取未定义的属性“第一个” - discord.js Cannot read property 'first' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM