Reputation: 1
I'm trying to make a Discord bot and will apply commands from JSON file (for ex. if command is "abc", find "abc" in commands' array (list) and reply with abc's data). Here an element from my JSON file (file is long, because of this, haven't put full file):
[
{"howareyou": {
"Reply": "**Uh, oh... A few minutes ago lost my diamond banana from Middle Ages. But expect this, I'm OK.",
"NoArguments": true
}}
]
But the problem is: I get error "Cannot read property "Reply" of undefined" at this JS code:
const CMDFile = JSON.parse (FS.readFileSync ('./Commands.json', 'utf8', Error_ => { if (Error_) throw Error_ }))
if (Message.content.startsWith ('\\')) { // My prefix is '\'
if (Used_CMD == '' /* Used CMD is msg content without prefix */ || (Message.mentions.users.first () && Message.mentions.users.first ().id == '123456789012345678')) {
Cevapla ('Yes, I'm! For more, just use command `\\help`.')
} else {
const CMD = CMDFile [Used_CMD],
Reply = CMD.Reply, NoArg = CMD.NoArguments
My code runs correctly when I use '\' command btw, but when I type '\howareyou', it errors TypeError: Cannot read property 'Reply' of undefined
. How can I fix this? Thanks.
Upvotes: 0
Views: 103
Reputation: 2433
The problem is either the format of your input file, or the code to get the proper command from the JSON.
{
"howareyou": {
"Reply": "**Uh, oh... A few minutes ago lost my diamond banana from Middle Ages. But expect this, I'm OK.",
"NoArguments": true
},
"other_cmd": {
"Reply": "xx",
"NoArguments": true
}
}
var CMD = null
CMDFile.forEach((cmd) => { if(cmd.hasOwnProperty(Used_CMD)) CMD = cmd; });
or (as per comment)
const CMD = CMDFile.find(cmd => cmd.hasOwnProperty(Used_CMD));
Upvotes: 2