Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda