【发布时间】:2018-02-21 00:00:44
【问题描述】:
我想做 Discord Bot。
我使用 Node.js 和 Discord api。
我的错误:
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
我的绳子:
//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.
我的文件夹
我认为这个错误是这一行
让 userData = JSON.parse(fs.readFileSync('Storage/userData.json', 'utf8'))
还有……
fs.writeFile('Storage/userData.json', JSON.stringify(userData), (err) => { //这会将我们刚刚对JSON文件所做的更改写入。 if (err) console.error(err); })
这一行。但我不知道如何修理这条线。
我该怎么办?
【问题讨论】:
-
和.. 我可以链接 youtubeURL 吗?网址是...我参考视频制作这条线
标签: node.js