!tocm lenght convert miles, inches, yards and foot

Hi, so first thing first I am trying to add a command called !tocm, It basically turns ‘inches’ ‘miles’ ‘yards’ ‘foot’ into cm and i will be giving the codes in following sections.

It works like this: !tocm and it responds in santimeters. The thing is i don’t know how to add the command in chat, how do i do?
!addcom !tocm $(urlfetch json https://pastebin.com/raw/MmzRtwSA) doesnt work. How do i just collect the codes in one section and make it work?
Appreciate any help, thanks.

Codes:

$(eval
const input = $(query);
const validUnits = [“inch”, “inches”, “foot”, “feet”, “yard”, “yards”, “mile”, “miles”];

const unit = input.match(/[a-zA-Z]+/);
const value = parseFloat(input);

if (isNaN(value) || !unit || !validUnits.includes(unit[0].toLowerCase())) {
“Lütfen geçerli bir uzunluk girişi yapın. Örneğin: !tocm 5 inches”;
} else {
const cmValue = (() => {
switch (unit[0].toLowerCase()) {
case “inch”:
case “inches”:
return value * 2.54;
case “foot”:
case “feet”:
return value * 30.48;
case “yard”:
case “yards”:
return value * 91.44;
case “mile”:
case “miles”:
return value * 160934.4;
}
})();

const kmValue = cmValue / 100000;

`${value} ${unit[0]} = ${cmValue} cm = ${kmValue} km`;

}
)

Hey @oldbutdolphin!

Pastebin is good to expand the length of code for a command, but Nightbot can’t parse its variable in there, meaning that if you use any Nightbot variable, you need to define them directly in the command, and then send them to the Pastebin. Here’s how to fix your issue:

$(eval const input = '$(query)'; $(urlfetch json https://pastebin.com/raw/XXXXXXXX))

And the code in your paste becomes:

const validUnits = ["inch", "inches", "foot", "feet", "yard", "yards", "mile", "miles"];
  
const unit = input.match(/[a-zA-Z]+/);
const value = parseFloat(input);
  
if (isNaN(value) || !unit || !validUnits.includes(unit[0].toLowerCase())) {
  "Lütfen geçerli bir uzunluk girişi yapın. Örneğin: !tocm 5 inches";
} else {
  const cmValue = (() => {
    switch (unit[0].toLowerCase()) {
      case "inch":
      case "inches":
        return value * 2.54;
      case "foot":
      case "feet":
        return value * 30.48;
      case "yard":
      case "yards"
        return value * 91.44;
      case "mile":
      case "miles":
        return value * 160934.4;
    }
  })();
    
  const kmValue = cmValue / 100000;
    
  `${value} ${unit[0]} = ${cmValue} cm = ${kmValue} km`;
}

Please note that I haven’t double-checked your paste’s code, so I can’t guarantee that it works properly, but at least now the Nightbot side of things is working.
You also forgot to put $(query) in quotes: you intend to use it as a string and not a variable name, therefore you should put it in quotes.

I added the command but i get an error.
Error:
missing ) after argument list [:1:29]

How can i fix that.
Appreciate, any help.

Edit: I asked chatgpt about the error and nightbot doesn’t respond rightnow. I typed !tocm 5 inches and the bot didn’t even respond. How can i fix that?

ChatGPT is a great tool, but for writing code it’s not as simple, it needs to know the context of the project, and you need to be able to fix the buggy code it writes, because it’s often broken, especially if you don’t know how to prompt it properly in regards to what you’re trying to have it do, and LLMs often hallucinate still as well, you need to be cautious with everything they output, gotta be very aware of this!


Let’s have a look at your code now, and fix/improve it, the thing that prevented the code to work was that you didn’t sent variables to the function giving you cmValue, I took the opportunity to add units abbreviations, and to clean up the code a bit:

const validUnits = ['in', 'inch', 'inches', 'ft', 'foot', 'feet', 'yd', 'yard', 'yards', 'mi', 'mile', 'miles'];

const unit = input.match(/[a-zA-Z]+/)?.[0].toLowerCase() ?? {error: 'NO_UNIT'};
const value = parseFloat(input) || {error: 'NO_VALUE'};

if (unit.error || value.error || !validUnits.includes(unit)) {
  'Lütfen geçerli bir uzunluk girişi yapın. Örneğin: !tocm 5 inches'
} else {
  const cmValue = ((value, unit) => {
    switch(unit) {
      case 'in':
      case 'inch':
      case 'inches':
        return value * 2.54;
      case 'ft':
      case 'foot':
      case 'feet':
        return value * 30.48;
      case 'yd':
      case 'yard':
      case 'yards':
        return value * 91.44;
      case 'mi':
      case 'mile':
      case 'miles':
        return value * 160934.4;
    }
  })(value, unit);

  const kmValue = cmValue / 100000;

  `${value} ${unit} = ${cmValue} cm = ${kmValue} km`
}
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.