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:
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.
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(' ');