【发布时间】:2020-12-07 16:08:02
【问题描述】:
我尝试找到解决问题“json.find”以显示商品和价格
例如
输入.shop basic kibble
机器人回复Name: Basic Kibble cost: k50
或输入.shop food
机器人回复Name:Food cost k10
在 bot.js 中
bot.on("message", ({ author, channel, content }) => {
if (author.bot || !content.startsWith(prefix)) return;
const args = content.slice(prefix.length).split(" ");
const command = args.shift();
if (command === "shop") {
if (!args.length) return message.channel.send("You must specify an item!");
const input = args.join(" ");
const items = json.find((object) => object.name === `item`);
if (!items) return channel.send(`${input} isn't a valid item!`);
channel.send(`Name: ${items.name} Cost: k${items.cost}`);
}
});
【问题讨论】:
-
在 shop.json 文件中``` {"item": [ { "name": "Basic Kibble", "cost": 50 }, { "name": "food", "cost ": 10 } ] }```
-
您是否需要或导入过 json 文件?
-
你在哪里定义“json”
-
const json = require("./data/shop.json");在 bot.js 的顶部
-
试试
json.JSON().find()
标签: arrays json find discord.js