Javascript :makekey command

Hey! I’ve been trying to make a discord bot, along with the bot I have premium, to make a premium key an owner has to say :makekey but I can’t get the result to end in format like this FJ@D-$FAJ-(JOF-IWA&, I am new to javascript and I have the code generator, but no way to get the code in a format like that, I’ve looked everywhere else that I’ve seen.

function makeid(length) {

   var result           = '';

   var result1          = '';

   var result2          = '';

   var result3          = '';

   var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()=:;"?<>.,';

   var charactersLength = characters.length;

   for ( var i = 0; i < length; i++ ) {

      result += characters.charAt(Math.floor(Math.random() * charactersLength));

      result1 += characters.charAt(Math.floor(Math.random() * charactersLength));

      result2 += characters.charAt(Math.floor(Math.random() * charactersLength));

      result3 += characters.charAt(Math.floor(Math.random() * charactersLength));

   }

   return result;

}

console.log(makeid(4));

Hi @currencytype,

function makeid(length) {
   var result           = '';
   var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
   var charactersLength = characters.length;
   for ( var i = 0; i < length; i++ ) {
      result += characters.charAt(Math.floor(Math.random() * charactersLength));
      if((i+1) % 4 === 0 && i != (length-1)){
      	result += '-';
      }
   }
   return result;
}

console.log(makeid(16));

Try this one.
This one produce output like --> 05VD-bAtf-jbUn-MD3r

OR you can also do like this to get the output in variable size.
make sure the the value of pairSize should be in that way so that if you divide length by pairSize its remainder need to be zero.

function makeid(length, pairSize) {
   let result           = '';
   const characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
   const charactersLength = characters.length;
   for ( let i = 0; i < length; i++ ) {
      result += characters.charAt(Math.floor(Math.random() * charactersLength));
      if((i+1) % pairSize === 0 && i != (length-1)){
      	result += '-';
      }
   }
   return result;
}

console.log(makeid(25,5));      // try (16,4)  or (9,3) or (30,6) ...etc..

this will output like this --> “lSNRs-3myjD-6Zpoj-5LfbE-0Af0h”

Hope this helps,
Regards,
Abhishek

1 Like