首页 文章

为node.js制作Discord Bot JSON.parse()错误:undefined:1

提问于
浏览
1

我想制作Discord Bot .

我使用Node.js和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

我认为这个错误是这一行

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

和...

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); })

这条线 . 但我不知道如何修理这根绳子 .

我能怎么做?

2 回答

  • 0

    在尝试解析您的JSON数据时,错误消息来自第一个而不是第二个 . 您可以在错误的调用堆栈中识别此信息:

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

    这是在处理 app.js ,第7行,字符位置21时,它对应于 JSON.parse( ... ) 方法 .

    但是,您的错误不在于此应用程序代码:到目前为止这很好!投诉涉及其消耗的JSON数据文件,因此出现错误消息:“ JSON 输入的意外结束”(这是从 parse() 方法发出的错误消息) .

    此外,它通过您的JSON逐行解析,并寻求JSON对象定义的结束,但文件首先结束!那个JSON文件有什么问题?好吧,缺少的是报告为错误消息的 undefined ,就像调用堆栈中的位置一样, :1 将此标识为字符位置1处的缺失内容 - 也就是说,它期待在其中找到一个字符光标位于该行的第一个字符处,而是遇到标记 .

    您在 Storage/userData.json 中的JSON对象定义需要包含在您的自定义数据的属性中,因此,我可以肯定您已经省略了终止该JSON对象的定义,并且最后一行可能缺少的是:

    }
    

    因此,只需在 userData.json 中添加最后一行就可能解决您的数据输入文件问题 . (例如,参见this gist并将 userData.json 的内容/结构与 Setup 中描述的 config.json 进行比较)

  • 0

    我使用了与您使用完全相同的教程 . 有一种非常简单的方法可以解决这个问题 . 只是进入你的userData.json并输入“{}”我有完全相同的错误消息完全相同的问题,并修复它 .

相关问题