Make Discord Bot for node.js JSON.parse() error : undefined:1 -


i want make discord bot.

i use node.js , discord api.

my error :

c:\----\----\desktop\siinabot>node app.js undefined:1 syntaxerror: unexpected end of json input @ json.parse (<anonymous>) @ object.<anonymous> (c:\users\lin\desktop\siinabot\app.js:7:21) @ module._compile (module.js:573:30) @ object.module._extensions..js (module.js:584:10) @ module.load (module.js:507:32) @ trymoduleload (module.js:470:12) @ function.module._load (module.js:462:3) @ function.module.runmain (module.js:609:10) @ startup (bootstrap_node.js:158:16) @ bootstrap_node.js:598:3 

my cord :

//calling pakage const discord = require('discord.js'); const bot = new discord.client(); const fs = require('fs');  // json files let userdata = json.parse(fs.readfilesync('storage/userdata.json', 'utf8')); // calls json file.  //listener event : message received ( wiil run every time message recived) bot.on('message', message => {   //variables   let sender = message.author; // person sent th message   let msg = message.content.touppercase(); // takes message, , makes uppercase   let prefix = '>' // test before commands, can set ever want    //event   if(!userdata[sender.id + message.guild.id]) userdata[sender.id + message.guild.id] = {} // creates json file user + guild, if 1 not made already.   if(!userdata[sender.id + message.guild.id].money) userdata[sender.id + message.guild.id].money = 1000; // creates money object them if start out with, can change whatever want.    fs.writefile('storage/userdata.json', json.stringify(userdata), (err) => {  //this writes changes made json file.     if (err) console.error(err);   })   // commends    //ping   if (msg === prefix + 'ping'){     message.channel.send('pong!') // sends message chanel, contens: "pong!"   }     })  // code runs when bot turns on bot.on('ready', () => {   console.log('economy launched...') })   // login  bot.login('i`ll write code bot token');  //don`t let people see code, people can control bot, including servers bot has admin on. 

my folder

structure of folder here

i think error line

let userdata = json.parse(fs.readfilesync('storage/userdata.json', 'utf8'))

and...

fs.writefile('storage/userdata.json', json.stringify(userdata), (err) => { //this writes changes made json file. if (err) console.error(err); })

this line. don`t know how can fix cord.

how can do?

the error message arising first, not second, attempts parse json data. can identify in call stack of error here:

at object.<anonymous> (c:\users\lin\desktop\siinabot\app.js:7:21) 

it during processing of app.js, line 7, character position 21, corresponds json.parse( ... ) method.

your error, however, not application code : fine far! complaint concerns json data file consuming, hence error message: "unexpected end of json input" (this error message being emitted parse() method).

furthermore, parsing through json line-by-line, , seeking end of json object definition, file ends first! problem in json file? well, missing being reported undefined of error message, , position in call stack, :1 identifies missing content @ character position 1 - is, expecting find character @ cursor positioned @ first character of line, instead encounters marker instead.

your json object definition in storage/userdata.json needs enclosed in properties customised data , therefore, can pretty sure have omitted terminate definition of json object, , missing on last line is:

} 

so, adding last line in userdata.json solve data input file problem. (see, example, this gist , compare content/structure of userdata.json config.json described in setup)


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -