new to JavaSript i'm currently working on a discord bot using an API.
(我是JavaSript的新手,我目前正在使用API??开发不和谐的机器人。)
And I would like to know how I could store the request answers in some variables so I can use them later to write on a .json file.(而且我想知道如何将请求答案存储在一些变量中,以便以后可以使用它们在.json文件中进行写操作。)
API used : https://wiki.guildwars2.com/wiki/API:2/account (I can show the answer with a console.log without problems but I cant store them :/ )
(使用的API: https ://wiki.guildwars2.com/wiki/API:2/帐户(我可以使用console.log显示答案,但不会出现任何问题,但我不能存储它们:/))
bot.on('message', message => {
if (message.content.startsWith(config.prefix+"add")){
const fs = require('fs') //importing file save
var fPath = './data.json'
var fRead = fs.readFileSync(fPath);
var fFile = JSON.parse(fRead); //ready for use
var userId = message.author.id //user id here
var n; var g;
if (!fFile[userId]) { //this checks if data for the user has already been created
cut=message.content.replace("?add ", "");
api.authenticate(cut).account().blob().then(request=> n=(request.account.name))
api.authenticate(cut).account().blob().then(request=> g=(request.account.guilds));
fFile[userId] = {key: cut, uidgw2:n, guild: g, role:""} //if not, create it
fs.writeFileSync(fPath, JSON.stringify(fFile, null, 2));
} else {
message.reply("Déjà dans la base.");
console.log(message.author.id+" déjà dans data.json");
}
}
})
My problem is that n & g are apparently undefined and so i get that in my .json file :
(我的问题是n&g显然是未定义的,所以我在.json文件中得到了它:)
{
"231452514608360960": {
"key": "B1257308-125X-8040-A55B-0AD1CF03480DF08F4AC2-9326-44DC-83A6-75A950C5ADFA",
"role": ""
}
}
Even if I declare them outside bot.on or inside, still undefined and I dont have much knowledge in js & api requests...
(即使我在bot.on或内部声明它们,仍然是未定义的,并且我对js和api请求没有太多了解...)
ask by Trijoikolip translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…