1

I'm trying to make a !calculate command in Discord.js v12. I'm using mathjs and this is my current code:

client.on('message', msg => {
    if(!msg.content.startsWith('!')) return;

    const args = msg.content.split(/[\ ]/g);
    const cmd = args[0].slice(1).toLowerCase();

    switch (cmd) {
        case 'calculate':
        case 'calc':
            if(!args[0]) return msg.channel.send('Please input a calculation.');

            let resp;
            try {
                resp = math.evaluate(args.join(''));
            } catch (e) {
                console.log(e);
            }

            const membed = new Discord.MessageEmbed()
                .setColor('#ffaa00')
                .setTitle('Math Calculation')
                 .addField('Input', `\`\`\`css\n${args.slice(1).join(' ')}\`\`\``)
                .addField('Output', `\`\`\`css\n${resp}\`\`\``);

            msg.channel.send(membed);
            break;

However, I get the following console error:

SyntaxError: Value expected (char 1)
    at createSyntaxError (D:\Documents\test\Node.js\Discord.js\calculate\node_modules\mathjs\lib\expression\parse.js:1705:17)
    at parseEnd (D:\Documents\test\Node.js\Discord.js\calculate\node_modules\mathjs\lib\expression\parse.js:1669:13)
    at parseParentheses (D:\Documents\test\Node.js\Discord.js\calculate\node_modules\mathjs\lib\expression\parse.js:1655:12)
    at parseNumber (D:\Documents\test\Node.js\Discord.js\calculate\node_modules\mathjs\lib\expression\parse.js:1626:12)
    at parseObject (D:\Documents\test\Node.js\Discord.js\calculate\node_modules\mathjs\lib\expression\parse.js:1607:12)
    at parseMatrix (D:\Documents\test\Node.js\Discord.js\calculate\node_modules\mathjs\lib\expression\parse.js:1532:12)
    at parseSingleQuotesString (D:\Documents\test\Node.js\Discord.js\calculate\node_modules\mathjs\lib\expression\parse.js:1433:12)
    at parseDoubleQuotesString (D:\Documents\test\Node.js\Discord.js\calculate\node_modules\mathjs\lib\expression\parse.js:1382:12)
    at parseSymbol (D:\Documents\test\Node.js\Discord.js\calculate\node_modules\mathjs\lib\expression\parse.js:1270:12)
    at parseCustomNodes (D:\Documents\test\Node.js\Discord.js\calculate\node_modules\mathjs\lib\expression\parse.js:1239:12) {
  char: 1
}

The embed looks like this:

embed

Daemon Beast
  • 2,794
  • 3
  • 12
  • 29

1 Answers1

1

You are handling the args array incorrectly. There are 2 issues:

  1. args[0] needs to be changed to args[1], as args[0] is referencing the command, not the calculation.

    if(!args[0]) return msg.channel.send('Please input a calculation.');
    // should become
    if(!args[1]) return msg.channel.send('Please input a calculation.');
    
  2. args.join('') needs to be changed to args.slice(1).join(''), so that it only contains the calculation and not the command at the start.

    resp = math.evaluate(args.join(''));
    // should become
    resp = math.evaluate(args.slice(1).join(''));
    
Daemon Beast
  • 2,794
  • 3
  • 12
  • 29