Search code examples
javascriptnode.jsdiscord.jscommando

Random quote send from a bot - Unexpected Identifyer


So I'm getting a weird error message which looks like this:

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

So it worked before but I have all dependencies installed. The Javascript code looks like this:

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

The Config.json is just a simple json where all the random Quotes are stored.

The issue should be on Line 20 where I define the avatar URL but if I delete this line, it says something else on a different line with exactly the same error.

Thanks in advance :)


Solution

  • When you create a command with Commando, you need to put the code you want to execute inside the .run method of the class.
    In your case, the code should look like this:

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

    If you added arguments to your command, it would have looked like this:

    aysnc run(message, {arg1, arg2, arg3, ...args}) {...}