How to make multiple random alphanumeric strings with one const array in a node.js discord bot

Viewed 18

I am really stuck, and I need help answering how to generate new random strings from my bot, so far this is the code when a user type %random

// Dependencies
const { MessageEmbed, Message } = require('discord.js');
const config = require('../config.json');

module.exports = {
    name: 'random', // Command name
    description: 'Generate a random alphanumeric string.', // Command description

    /**
     * Command exetute
     * @param {Message} message The message sent by user
     */
    execute(message) {
    
    if (message.channel.id !== config.stringChannel) {
        message.channel.send(
            new MessageEmbed()
            .setColor(config.color.default)
            .setTitle('Wrong Channel')
            .setDescription('Go to '+`<#${config.stringChannel}>`+' to generate nitro') // Mapping the commands
            .setFooter(message.author.tag, message.author.displayAvatarURL({ dynamic: true, size: 64 }))
            .setTimestamp()
        );
    };
    const gn = [`${Math.random().toString(36).slice(2)}`+'\n'];
    if (message.channel.id === config.stringChannel) {
              message.channel.send(
            new MessageEmbed()
            .setColor(config.color.default)
            .setTitle(`String Generated`)
            .setDescription(`${gn}${gn}`) // Mapping the commands
            .setFooter(message.author.tag, message.author.displayAvatarURL({ dynamic: true, size: 64 }))
            .setTimestamp()
        );
    }
}
};

The following picture is the resulting embedded message I receive when I send %random received embed

2 Answers

You can achieve this by using function and forlooping

function generateAlpaNum(number) { //the number is your given number.
    var emptyString = ""; //To fill up the empty string when generating.
    var AlphaNumeric = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; //your AN
    var ANLength = AlphaNumeric.length; //Getting the length
    for(var i = 0; i < number; i++) { //loop when given number doesnt matched with I
        emptyString += AlphaNumeric.charAt(Math.floor(Math.random() * ANLength));  //Filling the emptyString
    }

    return emptyString;
}

console.log(generateAlpaNum(5));

You can check the snippet to see the result:

function generateAlpaNum(number) { //the number is your given number.
    var emptyString = ""; //To fill up the empty string when generating.
    var AlphaNumeric = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; //your AN
    var ANLength = AlphaNumeric.length; //Getting the length
    for(var i = 0; i < number; i++) { //loop when given number doesnt matched with I
        emptyString += AlphaNumeric.charAt(Math.floor(Math.random() * ANLength));  //Filling the emptyString
    }

    return emptyString;
}

console.log(generateAlpaNum(5));

You can also check the jsfiddle to see the live code.

This is the proof for working function with node.js: The source Code of mine

Result:

Result for the source code

I found working code for what I wanted to make, here it is

const gn = [""];
let cap = 100
for (let i = 0; i < cap; i++) {
gn.push("");
};


let text = "";
let fLen = gn.length;
for (let i = 0; i < fLen; i++) {
  text += gn[i] +`${Math.random().toString(36).slice(2)}`+'\n';
}
    if (message.channel.id === config.stringChannel) {
              message.channel.send(
            new MessageEmbed()
            .setColor(config.color.default)
            .setTitle(`String Generated`)
            .setDescription(`${text}`) // Mapping the commands
            .setFooter(message.author.tag, message.author.displayAvatarURL({ dynamic: true, size: 64 }))
            .setTimestamp()
        );
    }
Related