Home > OS >  How to dynamically read from a JSON file?
How to dynamically read from a JSON file?

Time:09-13

json file that looks like this

{
 "997708726666547240": 456
 "1009030663636267028": 654
}

this is for my discord bot that does a mute command, both ids in the config.json file are different server ids with the mute role as the value. The thing is I don't want to do

if (message.guild.id) { ...code } 

for both of the servers. So is there any way I could do something like

var muteRole = config.message.guild.id

so that it would automatically get the mute role id depending on where the command was run.

I've tried doing

var muteRole = config.(`${message.guild.id}`)

but that didn't work. Any and all help is appreciated. Thanks.

CodePudding user response:

As @axiac mentioned. You could use square brackets syntax like so

const muteRole = config[message.guild.id];

CodePudding user response:

As @axiac Or u can try this

const { id:muteRole } = config.message.guild
  • Related