首页 > 解决方案 > 将用户 ID 保存在 JSON 文件中

问题描述

今天我正在尝试制作一个 json 数据以将不和谐帐户链接到另一个帐户(英雄联盟、Dota 等......)

为了制作我的数据库,我正在使用以下代码:

message.reply(`Your account is link to **${args[1]}**`).then(msg => {
  msg.delete({
    timeout: 5000
  });
});

userID = message.author;

let link = require('../link.json')

link.userID = {
  name: `${message.author.username}`,
  lol: `${args[1]}`,
}

var string = JSON.stringify(link, null, '\t');

fs.writeFile('./link.json', string, function(err) {
  if (err) return console.error(err);
})

如果 bob 写/link lol boby02而我(StarKleey帅哥)写/link lol StarKleeyjson 将是:

{
    "userID": {                     //here I would like "@<565465478767545>": {
        "name": "bob",
        "lol": "boby02"
    },
    "userID": {                     //and here          "@<456465574385735>": {
        "name": "StarKleey 帅哥",
        "lol": "StarKleey"
    }
}

我怎样才能做到这一点?
这是我目前正在使用的:

link.message.author = {
  name: `${message.author.username}`,
  lol: `${args[1]}`,
}

重新加载:

{
    "@<565465478767545>": {
        "name": "bob",
        "lol": "boby02"
    },
    "@<456465574385735>": {
        "name": "StarKleey 帅哥",
        "lol": "StarKleey"
    }
}

标签: node.jsjsondiscord.js

解决方案


您当前正在存储用户名,而您似乎想要存储用户 ID:您应该尝试使用User.id

link.message.author = {
  name: `${message.author.id}`,
  lol: `${args[1]}`,
}

推荐阅读