0

所以我收到一条奇怪的错误消息,如下所示:

SyntaxError: Unexpected identifier
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at /home/remix867/bot_commando/node_modules/require-all/index.js:52:46

所以它以前工作过,但我已经安装了所有依赖项。Javascript 代码如下所示:

const { Command } = require('discord.js-commando');
const { oneLine } = require('common-tags');
const { RichEmbed } = require('discord.js');
const config = require('../../config.json');
var quotes = config.quotes;


module.exports = class EchoCommand extends Command {
    constructor(client) {
        super(client, {
            name: 'quote',
            group: 'quote',
            memberName: 'quote',
            description: 'Echoes a random Quote.',
            details: oneLine`,
            I'll say out a quote`,
            examples: ['quote']
        });
    }

    const avatarURL = message.author.avatar ? message.author.avatarURL: 'https://discordapp.com/assets/0e291f67c9274a1abdddeb3fd919cbaa.png';
    const embed = new Discord.RichEmbed()
      .setAuthor(`${message.author.tag}`, `${avatarURL}`);
      .setColor(0x0000FF);
      .setDescription(quotes[Math.floor(Math.random() * quotes.length)]);
      .setTimestamp();
    await message.channel.send({
      embed
    });
};

Config.json 只是一个简单的 json,其中存储了所有随机报价。

问题应该在我定义头像 URL 的第 20 行,但如果我删除这一行,它会在另一行显示其他内容,并出现完全相同的错误。

提前致谢 :)

4

1 回答 1

1

使用 Commando 创建命令时,需要将要执行的代码放入.run类的方法中。
在您的情况下,代码应如下所示:

module.exports = class EchoCommand extends Command {
  constructor(client) {
    super(client, {
      name: 'quote',
      group: 'quote',
      memberName: 'quote',
      description: 'Echoes a random Quote.',
      details: oneLine `,
            I'll say out a quote`,
      examples: ['quote']
    });
  }

  async run(message) {
    const avatarURL = message.author.avatar ? message.author.avatarURL : 'https://discordapp.com/assets/0e291f67c9274a1abdddeb3fd919cbaa.png';
    const embed = new Discord.RichEmbed()
      .setAuthor(`${message.author.tag}`, `${avatarURL}`);
    .setColor(0x0000FF);
    .setDescription(quotes[Math.floor(Math.random() * quotes.length)]);
    .setTimestamp();
    await message.channel.send({
      embed
    });
  }
};

如果您在命令中添加参数,它将如下所示:

aysnc run(message, {arg1, arg2, arg3, ...args}) {...}
于 2018-11-16T19:41:19.967 回答