How can I get an ID from json file using fs

Viewed 317
const rptch = require('./reportch.json')

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

I used fs.writeFile that writes a channel ID into a json file, but I can't get that channel by the json file id

2 Answers

I think in the JSON file might be:

{
  "id": "ID"
}

In Your main file:

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

So, what did it mean?

  1. The first line you wrote down may be wrong. Because there's no id variable for the JSON file.

  2. The reportChannel variable value from message.guild.channels.get() has been changed to message.guild.channels.cache.get() since new Discord.js v12 update.

It completly works for me!

NOTE: I used normally. No fs module needed

With fs use something like this:

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