搜索.json文件并显示Discord.js

mwngjboj  于 2022-12-24  发布在  其他
关注(0)|答案(1)|浏览(136)

我想说的是:
用户检查(id)
bot将搜索一个json文件

[
    {
        "name": “adam”,
        "age": “16”,
        "id": "9"
    },
    {
        "name": “jack”,
        "age": “18”,
        "id": "10"
    }

]

示例:! check 9
机器人随后将回复

name: adam
age:16
id:9

他怎么能这么做?

qyswt5oh

qyswt5oh1#

可以先使用require导入JSON文件,然后过滤这些文件以找到具有所需特定ID的条目,然后响应交互。

// For demo purposes, I have added the JSON object directly in the code.
// But in your code, you can just do something like this: 
// const entries = require("./filepath/to/json/file")
const entries = [{
    "name": "adam",
    "age": "16",
    "id": 9
  },
  {
    "name": "jack",
    "age": "18",
    "id": 10
  }
]

const objId = 9

const filteredObj = entries.filter((obj) => obj.id === objId)[0]
const name = filteredObj.name
const age = filteredObj.age
const id = filteredObj.id

console.log("Name: ", name)
console.log("Age: ", age)
console.log("Id: ", id)

相关问题