Ошибка при запуске JS Discord Bot

1

Я пробовал сделать битву в js. У меня была помощь, но я все-таки сделал что-то не так.
При запуске я получаю следующую ошибку в консоли:

undefined:1
SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at Object.<anonymous> (C:\Users\Erkaloth\Dropbox\Novel Notifier Bot\app.js:7:21)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

Полный код без токена:

// Calling the package
var Discord = require("discord.js");
var bot = new Discord.Client();
var fs = require('fs');

//This calls the file named userData.json from the folder Storage.
var userData = JSON.parse(fs.readFileSync('Storage/userData.json', 'utf8'));


// Listener Event: Message Received (This will run everytime a message is received)
bot.on('message', message => {

    // Variables
    var sender = message.author; // The person who sent the Message
    var msg = message.content.toUpperCase();
    var prefix = '~' // The prefix used when writing commands in chat.

    // It checks to make sure the sender is not the bot.
    if (sender.id === '410841116175368193') { // Checks if the ID of the sender is the same ID as the bot.
      return; // Cancels the rest of the Listener Event.
    }

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

    // This will ban specified words.
    if (msg.includes('FUCK')) { // Checks if the word Fuck is included in the message.
        message.delete()
        message.channel.send('Word used not allowed.')
    }

    if (msg === prefix + 'USERSTATS') {
      message.channel.send('You have sent **' + userData[sender.id].messagesSent + '** messages!')
    }

    //Makes sure the username is in userData before writing to the file.
    if (!userData[sender.id]) userData[sender.id] = {
      messagesSent: 0
    }

    userData[sender.id].messagesSent++; //This adds one to 'messagesSent', under the user.

    //This one saves the file that was wrote in.
    fs.writeFile('Storage/userData.json', JSON.stringify(userData), (err) => {
      if (err) console.error(err); //This one sends a messages with the error to the console if there is one.
    });

  });

  // Listener Event: Bot Launched
  bot.on('ready', () => {
    console.log("Loading...");
    setTimeout(function(){
      console.log("Bot has been loaded completely.");
    }, 1000);
    setTimeout(function(){
      console.log("Welcome, Master!");
    }, 2000);

    // Status
    bot.user.setStatus('Online') // You status goes here; It can be 'Online', 'idle', 'invisible', & 'dnd'

    // Game & Streaming
    bot.user.setGame('~help') // You can change the string to whatever you want it to say.

    //To set a stream, add another option like this:
    //bot.user.setGame('~help', 'https://twitch.tv/user');
    //It has to be a twitch stream link.

    // Any code can be placed here. It will automatically run when the bot starts.
  });

// Listener Event: User joining the discord server.
bot.on('guildMemberAdd', member => {
    console.log('User ' + member.user.username + ' has joined the server!') // Sends a message in the console that someone joined the discord server.

    var role = member.guild.roles.find('name', 'Member [Commoner]'); // This looks for the role in the server(guild), it searches by name, meaning you can change 'Member [Commoner]' to the role you want.

    // Secondly, we will add the role.
    member.addRole(role)

    // Sending a message to a channel when a user joins discord.
    member.guild.channels.get('399321101663993856').send('**' + member.user.username + '**, has joined the server!') // The first part gets the channel, the seconds sends a messages in the respective channel.
});

// Listener Event: User leaving the discord server.
bot.on('guildMemberRemove', member => {

    member.guild.channels.get('399321101663993856').send('**' + member.user.username + '**, has left the server!') // The first part gets the channel, the seconds sends a messages in the respective channel.
});

  // Login
  bot.login('TOKEN FOR BOT HERE')

Опять же, извините, если я не должен был публиковать здесь, но я не знаю, где искать больше. Я потратил несколько часов, пытаясь найти решение, и ничего не нашел. Либо это, либо я просто достаточно тупой, чтобы не найти его.

  • 0
    Эта ошибка означает, что JSON, который он пытался прочитать, недопустим. Возможно, у вас пропущена точка с запятой или цитаты или что-то в этом роде.
  • 1
    Кажется, что вызов fs.readFileSync('Storage/userData.json', 'utf8') приводит к неверно отформатированной строке JSON. Можете ли вы поделиться тем, что хранится в файле 'Storage / userData.json'?
Показать ещё 3 комментария
Теги:
discord.js

1 ответ

1
Лучший ответ

Решение. Я добавил некоторый фиктивный текст в userData.json, и он сработал.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню