【问题标题】:How can I get an ID from json file using fs如何使用 fs 从 json 文件中获取 ID
【发布时间】:2020-09-02 06:41:51
【问题描述】:
const rptch = require('./reportch.json')

let reportchannel = message.guild.channels.find('id', `${rptch}`);

我使用 fs.writeFile 将频道 ID 写入 json 文件,但我无法通过 json 文件 ID 获取该频道

【问题讨论】:

  • 您无法使用fs.readFile 写入文件。你的意思是fs.writeFile
  • 是的!我的意思是 fs.writeFile
  • 你的回答很好。 +1

标签: javascript node.js discord.js


【解决方案1】:

使用 fs 可以这样使用:

fs = require('fs')
data = fs.readfilesync(path)
info = JSON.parse(data)
info['propertyname'] //Will give you the id.

【讨论】:

    【解决方案2】:

    我认为在 JSON 文件中可能是:

    {
      "id": "ID"
    }
    

    在您的主文件中:

    const { id } = require('./reportch.json');
    let reportChannel = message.guild.channels.cache.get(id);
    

    那么,这是什么意思?

    1. 您写下的第一行可能是错误的。因为 JSON 文件没有 id 变量。

    2. 自从新的 Discord.js v12 更新以来,message.guild.channels.get() 中的 reportChannel 变量值已更改为 message.guild.channels.cache.get()

    它完全适合我!

    注意:我正常使用。不需要fs 模块

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-22
      • 2017-07-05
      • 2017-08-07
      • 2016-11-27
      • 2022-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多