K
K
Kirill2020-11-03 09:34:26
JSON
Kirill, 2020-11-03 09:34:26

Why is my bot throwing an error with JSON file?

const Discord = require("discord.js");
const fs = require('fs');
module.exports = {
  name: 'setup',
  description: 'Setup the bot',
  execute(message, args) {
        if (!args[1]) {
            let setupMess = new Discord.MessageEmbed()
            .setTitle('Настройка бота')
            .setDescription(`Введите \`verify\` после \`!setup\`, чтобы настроить верификацию,
            введите \`yournick\` после \`!setup\`, чтобы настроить смену ника.`)
            .addField('Например:', `\`!setup verify 123456789098765432\`, где \`123456789098765432\` - id канала`)
            .setColor('0x808000')
            message.channel.send(setupMess)
        }
        var verify = JSON.parse(fs.readFileSync("./servers.json", "utf8"));
        var yournick = JSON.parse(fs.readFileSync("./servers.json", "utf8"));
        if (!verify[message.guild.id]) verify[message.guild.id] = {
            verify: 0
        }
        fs.writeFile("./servers.json", JSON.parse(verify), (err) => { // Всё сохраняется в .json файл
            if (err) console.log(err)
        });
        if (!yournick[message.guild.id]) yournick[message.guild.id] = {
            yournick: 0
        }
        fs.writeFile("./servers.json", JSON.parse(yournick), (err) => { // Всё сохраняется в .json файл
            if (err) console.log(err)
        });
        if (args[1] == 'verify') {
            if (!args[2]) return
            verify[message.guild.id] = { 
                verify: args[2]
            }
            fs.writeFile("./servers.json", JSON.stringify(verify), (err) => { // Всё сохраняется в .json файл
                if (err) console.log(err)
            });
            message.author.send(verify[message.guild.id])
            message.author.send(args[2])
        }
        if (args[1] == 'yournick') {
            if (!args[2]) return
            yournick[message.guild.id] = {
                verify: `${args[2]}`
            }
            fs.writeFile("./servers.json", JSON.stringify(yournick), (err) => { // Всё сохраняется в .json файл
                if (err) console.log(err)
            });
        }
    },
};

Gives an error message
SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at Object.execute (C:\Users\Кирилл\Documents\discord-bots\verification-bot\commands\setup.js:16:27)
    at Client.<anonymous> (C:\Users\Кирилл\Documents\discord-bots\verification-bot\index.js:140:11)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (C:\Users\Кирилл\Documents\discord-bots\verification-bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Кирилл\Documents\discord-bots\verification-bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\Кирилл\Documents\discord-bots\verification-bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)       
    at WebSocketShard.onPacket (C:\Users\Кирилл\Documents\discord-bots\verification-bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (C:\Users\Кирилл\Documents\discord-bots\verification-bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (C:\Users\Кирилл\Documents\discord-bots\verification-bot\node_modules\ws\lib\event-target.js:125:16)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-11-03
@KIRIK12

before working with a .json file, you must add two curly braces to it {}.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question