Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
Discord bot unit conversion command returns "undefined"

I was coding a simple unit conversion command for my discord bot that would convert meters to centimetres. I read over my code several times and couldn't find anything possibly wrong with it, however, when I run the command, it simply returns "undefined" for the result value. I also made a point to double check the scope of my code blocks to make sure all the variables are properly accessible.

Code:

if (message.content.startsWith(prefix + 'convert ')) {
        
        let content = message.content.replace(prefix + 'convert ', '');
        content.split(' ');

        let value = content[0];
        let firstUnit = content[1];
        let convertedUnit = content[3];
        let result;
        let description;
        let embedFormula;

        if (firstUnit === 'm' && convertedUnit === 'cm') {
            const formula = (inputValue) => {
                return inputValue * 100;    
            }
            
            result = formula(value);
            description = `${value} m to cm = **${result} cm**`;
            embedFormula = '`Multiply the length value by 100`';    
        }

        const conversionEmbed = new Discord.MessageEmbed()
        .setTitle('Unit Conversion')
        .setDescription(description)
        .setColor('#F942FF')
        .addFields(
            { name: 'Formula', value: embedFormula }
        )

        message.delete();
        message.channel.send(conversionEmbed);
    }

Result:

enter image description here

The command runs when the user types "_convert 1 m to cm" (1 can of course be swapped with any number).

Is there anything missing here?

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

content.split() won't edit existing variables:

let content = 'Hello!';
content.split();

console.log(content); // Still 'Hello!'
console.log(content[0]); // This just gets the first character

So instead of this:

let content = message.content.replace(prefix + 'convert ', '');
content.split(' ');

You should do this:

let content = message.content.replace(prefix + 'convert ', '').split(' ');
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!