1
votes

I want to make Discord Bot.

I use Node.js and Discord api.

my error :

C:\----\----\Desktop\SiiNaBot>node app.js
undefined:1
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Object.<anonymous> (C:\Users\Lin\Desktop\SiiNaBot\app.js:7:21)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:598:3

my cord :

//Calling the 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')); // This calls the JSON file.

//Listener Event : Message Received ( This wiil run every time a message is recived)
bot.on('message', message => {
  //Variables
  let sender = message.author; // The person who sent th message
  let msg = message.content.toUpperCase(); // Takes the message, and makes it all uppercase
  let prefix = '>' // The test before commands, you can set this to what ever you want

  //Event
  if(!userData[sender.id + message.guild.id]) userData[sender.id + message.guild.id] = {} // This creates a json file for their user + guild, if one is not made already.
  if(!userData[sender.id + message.guild.id].money) userData[sender.id + message.guild.id].money = 1000; // This creates a money object for them if they start out with, you can change this to whatever you want.

  fs.writeFile('Storage/userData.json', JSON.stringify(userData), (err) => { 
//This writes the changes we just made to the JSON file.
    if (err) console.error(err);
  })
  // Commends

  //Ping
  if (msg === prefix + 'PING'){
    message.channel.send('Pong!') // Sends a message to the chanel, with the contens: "Pong!"
  }




})

// This code runs when the bot turns on
bot.on('ready', () => {
  console.log('Economy Launched...')
})


// Login

bot.login('I`ll write code my bot token'); 
//Don`t let people see this code, people can control your bot, including the servers your bot has admin on.

My Folder

Structure of my folder here

I think this error is this line

let userData = JSON.parse(fs.readFileSync('Storage/userData.json', 'utf8'))

and...

fs.writeFile('Storage/userData.json', JSON.stringify(userData), (err) => { //This writes the changes we just made to the JSON file. if (err) console.error(err); })

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

How can i do?

2
and.. can i link youtubeURL? URL is... I Reference Video to make this cord - ko Ha

2 Answers

0
votes

The error message is arising from the first, not the second, as it attempts to parse your JSON data. You can identify this in the call stack of your error here:

at Object.<anonymous> (C:\Users\Lin\Desktop\SiiNaBot\app.js:7:21)

It was during processing of your app.js, line 7, character position 21, which corresponds to the JSON.parse( ... ) method.

Your error, however, is not with this application code : that is fine thus far! The complaint concerns the JSON data file that its consuming, hence the error message: "Unexpected end of JSON input" (this is the error message being emitted from your parse() method).

Furthermore, its parsing through your JSON line-by-line, and its seeking the end of the JSON object definition, but the file ends first! What was the problem in that JSON file? Well, what is missing is being reported as the undefined of the error message, and just like the position in the call stack, the :1 identifies this as missing content at character position 1 - that is, it was expecting to find a character at its cursor positioned at the first character of that line, but instead it encounters the marker instead.

Your JSON object definition in Storage/userData.json needs to enclosed in properties with your customised data and therefore, I can be pretty sure you have omitted to terminate the definition of that JSON object, and what is likely missing on the last line is:

}

So, simply by adding this last line in your userData.json will likely solve your data input file problem. (See, for example, this gist and compare the content/structure of your userData.json with their config.json described in Setup)

0
votes

I used the exact same tutorial as you are using. There is a very simple way to solve this problem. Just go into your userData.json and put in "{}" I had the exact same problem with the exact same error message and this fixed it.