【问题标题】:Discord bot displaying token as SyntaxError: Unexpected end of inputDiscord 机器人将令牌显示为 SyntaxError:输入意外结束
【发布时间】:2020-07-22 17:57:17
【问题描述】:

每当我运行我的机器人代码时,我都会收到 SyntaxError: Unexpected end of input。

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

我的代码如下:

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);

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

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

    你缺少两件事:

    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);
    

    【讨论】:

      猜你喜欢
      • 2020-03-26
      • 2016-08-05
      • 1970-01-01
      • 2021-12-19
      • 2019-12-27
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      相关资源
      最近更新 更多