简体   繁体   English

需要帮助让 JSON.parse() 工作(Discord Bot)

[英]Need help getting JSON.parse() to work (Discord Bot)

I have a seperate JSON file which is linked as我有一个单独的 JSON 文件,链接为

const Players = require('./Database/Players.json');

and a praser that goes through the code和一个通过代码的praser

client.on('message', message => {


    if (message.content.toLowerCase() ==='smack activate') {
        
        let PlayerData = [message.author.username];

        Activate [message.author.username] = {
        AccountActive: 1,
        Health: 100,
        Level: 1,
        Lust: 0,
        Items: ""
        };

        var parsedata = JSON.parse(Players)   // <-----------
        if (parsedata.PlayerData.accountactive === 1) {
            message.channel.send ("Account Already Activated");
            return;
        }

        fs.writeFile("./Database/Players.json", JSON.stringify (Activate, null, 4), err => {
            if (err) throw err;
            message.channel.send ("Account Activated")
        });
    };

But nothing seems to work.. what am I doing wrong?但似乎没有任何效果..我做错了什么? I'm getting this error message我收到此错误消息

undefined:1
[object Object]
 ^

Take a look at how require works.看看require是如何工作的。 As described in the documentation: https://nodejs.org/api/modules.html#modules_all_together如文档中所述: https://nodejs.org/api/modules.html#modules_all_together

LOAD_AS_FILE(X) LOAD_AS_FILE(X)

  1. If X is a file, load X as its file extension format.如果 X 是文件,则加载 X 作为其文件扩展名格式。 STOP停止
  2. If X.js is a file, load X.js as JavaScript text.如果 X.js 是文件,则将 X.js 加载为 JavaScript 文本。 STOP停止
  3. If X.json is a file, parse X.json to a JavaScript Object.如果 X.json 是文件,则将 X.json 解析为 JavaScript Z497031794414A5524BZAC.901 STOP停止
  4. If X.node is a file, load X.node as binary addon.如果 X.node 是一个文件,则将 X.node 作为二进制插件加载。 STOP停止

So you should not call JSON.parse , because require automatically parses json modules into JavaScript objects.所以你不应该调用JSON.parse ,因为require会自动将 json 模块解析为 JavaScript 对象。

As far as I understand you are trying to do the following:据我了解,您正在尝试执行以下操作:

  1. Check if the message author is a known player with an activated account.检查消息作者是否是具有激活帐户的已知玩家。
  2. If yes - send the message that the account is already activated and return.如果是 - 发送帐户已激活的消息并返回。
  3. Otherwise - add the new player data to the known players.否则 - 将新玩家数据添加到已知玩家。

You probably can do this in this following way:您可能可以通过以下方式执行此操作:

const Players = require('./Database/Players.json');

client.on('message', message => {
  if (message.content.toLowerCase() === 'smack activate') {

    const PlayerData = Players[message.author.username];

    if (PlayerData && (PlayerData.AccountActive === 1)) {
      message.channel.send("Account Already Activated");
      return;
    }

    Players[message.author.username] = {
      AccountActive: 1,
      Health: 100,
      Level: 1,
      Lust: 0,
      Items: ""
    };

    fs.writeFile("./Database/Players.json", JSON.stringify(Players, null, 4), err => {
      if (err) throw err;
      message.channel.send("Account Activated")
    });
  };

})

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM