Discord机器人将令牌显示为SyntaxError:输入意外结束

时间:2020-04-10 01:07:52

标签: javascript node.js discord.js

每当我运行机器人代码时,都会出现SyntaxError:输入意外结束。

我没有看到方括号,所以我想知道你们中是否有人可以看到这个问题。

我的代码如下:

const request = require('request');
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
  if (!msg.content.startsWith(prefix) || msg.author.bot) return;

  const args = msg.content.slice(prefix.length).split(' ');
  const command = args.shift().toLowerCase();
  if (msg.author.bot) return;
  if (msg.content === `${prefix}ping`) {
    msg.reply('Pong!');
  }

  if (msg.content === `${prefix}rule`) {
    msg.reply('rules are not yet implemented, check out https://5thsrd.org/ for rules in the meantime');
  }

  if (msg.content.startswith(`${prefix}spell`)) {
    request(`http://www.dnd5eapi.co/api/spells/${args}`, { json: true }, (err, res, body) => {
      if (err) { return console.log(err); }
      else if (command === 'spell')
        msg.channel.send(`Name: ${body.name}`);
      msg.channel.send(`${body.desc}`);
      msg.channel.send(`At Higher Levels: ${body.higher_level}`)

      if (msg.content.startsWith(`${prefix}class`)) {
        request(`http://www.dnd5eapi.co/api/classes/${args}`, { json: true }, (err, res, body) => {
          if (err) { return console.log(err); }
          else if (command === 'class')
            msg.channel.send(`Name: ${body.name}`);
          msg.channel.send(`Hit Dice:${body.hit_die}`);
          msg.channel.send(`Proficiency Options: ${body.proficiency_choices}`);
          msg.channel.send(`Starting Equipment: ${body.starting_equipment}`);
          msg.channel.send(`Proficiencies: ${body.proficiencies}`);
          msg.channel.send(`Subclasses: ${body.subclasses}`);

        });

      }
    })
  }
});
client.login(token);

1 个答案:

答案 0 :(得分:1)

如果您使用任何online JavaScript "beautifier"linter或任何重量合适的IDE,您很快就会意识到您实际上并没有正确关闭函数。

您缺少2件事:

  1. 您的}语句的大括号(if (msg.content.startswith(`${prefix}spell`)))。
  2. 使用大括号(}),大括号())和分号(;)来封闭client.on('message', msg => {块。

总而言之,您固定的代码应类似于:

const request = require('request');
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
    if (!msg.content.startsWith(prefix) || msg.author.bot) return;

    const args = msg.content.slice(prefix.length).split(' ');
    const command = args.shift().toLowerCase();
    if (msg.author.bot) return;
    if (msg.content === `${prefix}ping`) {
        msg.reply('Pong!');
    }

    if (msg.content === `${prefix}rule`) {
        msg.reply('rules are not yet implemented, check out https://5thsrd.org/ for rules in the meantime');
    }

    if (msg.content.startswith(`${prefix}spell`)) {
        request(`http://www.dnd5eapi.co/api/spells/${args}`, {
            json: true
        }, (err, res, body) => {
            if (err) {
                return console.log(err);
            } else if (command === 'spell')
                msg.channel.send(`Name: ${body.name}`);
            msg.channel.send(`${body.desc}`);
            msg.channel.send(`At Higher Levels: ${body.higher_level}`)

            if (msg.content.startsWith(`${prefix}class`)) {
                request(`http://www.dnd5eapi.co/api/classes/${args}`, {
                    json: true
                }, (err, res, body) => {
                    if (err) {
                        return console.log(err);
                    } else if (command === 'class')
                        msg.channel.send(`Name: ${body.name}`);
                    msg.channel.send(`Hit Dice:${body.hit_die}`);
                    msg.channel.send(`Proficiency Options: ${body.proficiency_choices}`);
                    msg.channel.send(`Starting Equipment: ${body.starting_equipment}`);
                    msg.channel.send(`Proficiencies: ${body.proficiencies}`);
                    msg.channel.send(`Subclasses: ${body.subclasses}`);

                });

            }
        });
    }
});

client.login(token);