SyntaxError:意外的标记}

时间:2018-02-22 12:10:17

标签: javascript bots discord discord.js

我正在discord.js编码Discord机器人。我已经通过我的代码得到了一点,并且没有找到修复方法的问题,但最近我尝试添加一个帮助命令。每次我运行cmd时,我都会this

我的代码是

//tips
// '\n' = new line


//Requited
const Discord = require("discord.js");
const YTDL = require("ytdl-core");
const fs = require('fs');
const prefix = "!";
const TOKEN = ("This is where my token is");
const commands = JSON.parse(fs.readFileSync('commands.json', 'utf8'));


var bot = new Discord.Client();
var servers = {};


//Boot Up
bot.on("ready", function() {
  console.log("Logged in and Ready!");
bot.user.setStatus('Online');
bot.user.setActivity('Roblox4Lyfe <3');
});


//Commands
bot.on('message', message => {
    let msg = message.content.toUpperCase();
    let sender = message.author;
    let cont = message.content.slice(prefix.length).split(" ");
    let args = cont.slice(1);

    //Help System
    if (meg.startsWith(prefix + 'HELP')) {
        if (msg === '${prefix}HELP') {
            const embed = new Discord.RichEmbed()
            .setColor(0x1D82B6)
        let commands = 0;
        for (var cmd in commands) {
            if (commands[cmd].group.toUpperCase() === 'USER') {
                commandsFound++;
                embed.addFeild('${commands[cmd].name}', 'Description: ${commands[cmd].desc}\nUsage: ${prefix + commands[cmd].usage}');
            }
            embed.setFooter('Our Commands.');
            embed.setDescription('Command found');
        }
        message.author.send({embed});
        message.channel.send({embed: {
            color: 0x1D82B6,
            description: 'Check your DMs buddy boy ${message.author}'
        }});
        }
    }
    //Purge system
    if (msg.startsWith(prefix + 'PURGE')) {
        async function purge() {
            message.delete();
            if (!message.member.roles.find("name", "Guild Officer")) {
                message.channel.send('You need a higher rank for this command.');
                return;
            }
            if (isNaN(args[0])) {
                message.channel.send('Make sure to use a number');
                return;
            }
            const fetche = await message.channel.fetchMessages({limit: args[0]});
            console.log(fetched.size + ', Found the little cunt(s)');
            message.channel.bulkDelete(fetched)
                .catch(error => message.channel.send('Error: With the purge command, \nPlease report this to MeanTeacher.'));
    }
    purge ();
    }
});


//Token
bot.login(TOKEN);

在另一个文件中我也存储了我的命令的基础

{
    "Help": {
        "name": "Help",
        "group": "User",
        "desc": "Displays commands.",
        "usage": "help",
    },
    "Purge": {
        "name": "Purge",
        "group": "Guild Officer",
        "desc": "Clears Messages",
        "usage": "purge <number>",
    }
}

现在我知道这对你们很多人来说很基本但是我仍然坚持到这里 我很抱歉,如果这很简单,你不想帮忙,但如果可以,我会非常感激,谢谢!

1 个答案:

答案 0 :(得分:0)

您获得的错误是由于您的JSON文件中有2个额外的逗号","。每个命令对象的最后一个属性("Help""Purge")最后都不需要逗号","

因此,在"Help"命令对象中,删除行"usage": "help",末尾的逗号,以便命令对象现在看起来像:

"Help": {
    "name": "Help",
    "group": "User",
    "desc": "Displays commands.",
    "usage": "help"
}

"Purge"命令对象中,删除行"usage": "purge <number>",末尾的逗号,以便命令对象现在看起来像:

"Purge": {
    "name": "Purge",
    "group": "Guild Officer",
    "desc": "Clears Messages",
    "usage": "purge <number>"
}

因此,您的最终json文件应如下所示:

{
    "Help": {
        "name": "Help",
        "group": "User",
        "desc": "Displays commands.",
        "usage": "help"
    },
    "Purge": {
        "name": "Purge",
        "group": "Guild Officer",
        "desc": "Clears Messages",
        "usage": "purge <number>"
    }
}