0

So, I'm looking to create a password generator function in discord.js, and I'd like to include [a-z][A-Z][0-9], whilst all being optional (I'd like for the user to be able to determine what to include in the password, for example whether they want lower and uppercase or only one). I've been trying to figure this out for a bit now and can't quite get it right (I'm pretty new to all this).

This is what I have so far:

if (message.content === `${prefix}pwgen`) {
        const random = (length = 8) => {
            
            let chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        
            
            let str = '';
            for (let i = 0; i < length; i++) {
                str += chars.charAt(Math.floor(Math.random() * chars.length));
            }
        
            return str;
        
        };

This is some code I got from https://attacomsian.com/blog/javascript-generate-random-string but obviously, here, he uses console.log to get results from the block of code, but here I'd like to be able to have the user send a number on Discord so the bot generates a random string of that length, and then have the bot return it in Discord. How can I do this? Thanks!

1

2 Answers 2

3

I would like to add an alternative solution that does not use Math.random to get the random number. Using the crypto module should provide a better degree of randomness:

function genPassword (length = 14) {
  var randChars = Array(length).fill().map(getRandomChar)
  var pw = randChars.join('')
  return pw
}

function getRandomChar () {
  var crypt = window.crypto || window.msCrypto
  var target = new Uint8Array(1)
  var randNr = crypt.getRandomValues(target)
  var sevenBitRandNr = randNr >> 1
  if (sevenBitRandNr < 48
     || sevenBitRandNr >= 122
     || (sevenBitRandNr >= 58 && sevenBitRandNr <= 64)
     || (sevenBitRandNr >=91 && sevenBitRandNr <= 96)) {
    return getRandomChar()
  }
  return String.fromCharCode(sevenBitRandNr)
}

console.log(genPassword(20))

This does provide upper- and lowercase as well as numbers.

Sign up to request clarification or add additional context in comments.

2 Comments

Definitely better than my primitive solution :p
I think your approach still needs to be mentioned in this context! Although the crypto module is widely supported nowadays, there are still browser that dont support it.
0

Not the perfect solution, but should work like a charm.

NOTICE: This does not include uppercase characters.

function generate (count = 20) {
  let password = ''
  while (password.length < count) {
    password += Math.random().toString(36).substr(2)
  }
  return password.substr(0, count)
}

console.log(generate(10), generate(20), generate(15))

3 Comments

Hm, alright, thanks! So now, I should be able to do: message.channel.send (`Your generated password is ${password}); Right? Or am I doing something wrong?
Yes, this code works as it is. I changed it to an executable, so you can see
Works perfectly, thanks! How would I be able to modify this to include capital letters and have the user choose whether to include them, though? Or would I need to modify the code block?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.