【问题标题】:Random quote send from a bot - Unexpected Identifyer从机器人发送的随机报价 - 意外标识符
【发布时间】:2019-04-19 15:36:12
【问题描述】:

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

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 行,但如果我删除这一行,它会在不同的行显示其他内容,并出现完全相同的错误。

提前致谢:)

【问题讨论】:

    标签: javascript node.js discord.js commando


    【解决方案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}) {...}
    

    【讨论】:

      猜你喜欢
      • 2021-07-28
      • 2019-06-13
      • 1970-01-01
      • 2020-08-25
      • 2021-08-19
      • 2019-04-01
      • 2017-03-19
      • 2017-06-16
      • 1970-01-01
      相关资源
      最近更新 更多