可以通过"or"命令调用 JSON 数组吗?



我有一个关于JSON对象的问题。

我有一个.json文件,看起来像这个

{
"Plate Boots": {
"T4": [],
"T5": []
},
"Plate Armor": {
"T4": [],
"T5": []
},
"Plate Helmet": {
"T4": [],
"T5": []
}
}

我希望能够通过在discord服务器中键入命令,将userID放入对象的[]中。我只是想知道,制作最简单的方法是什么

if (message.content.startsWith(prefix + " craft")) {
}

命令将message.author.id放入对象中。让我举一个例子:

User1 can craft T4 Plate Boots, so he wants to be in the "Plate Boots - T4" category in the JSON file.
So User1 types in the discord server "!v craft T4 Plate Boots" (The !v is my prefix).
And the bot pushes his message.author.id into the ""T4": [*in here*]"

我该如何制作

if (message.content.startsWith(prefix + " craft")) {
}

对于这种类型的事情?

是的,这是可能的。如果格式总是像类别一样有2个世界,并键入1个世界,您可以按照以下方式进行操作。

const fs = require('fs')
const crafts = require('./FILEPATCH');
if (message.content.startsWith(prefix + " craft")) {
let args = message.content.split(' ')
if(args.length < 4) return message.reply('Incorrect format, use `!v craft T4 Plate Boots`')
let category = args.splice(args.length - 2, 2).join(' ')
let type = args[args.length -1]
if (!crafts.hasOwnProperty(category)) return message.reply('Incorrect format, use `!v craft T4 Plate Boots`')
if (!crafts[category].hasOwnProperty(type)) return message.reply('Incorrect format, use `!v craft T4 Plate Boots`')
crafts[category][type].push(message.author.id)
fs.writeFileSync('filepatch/craft.json', JSON.stringify(crafts));
}

最新更新