Creating different random alphanumeric codes using single array in Discord bot Node.js

I’m having trouble with my Discord bot and need some guidance. I want to create a command that produces unique random alphanumeric sequences each time someone uses it. Right now when users type !generate, my bot works but I think there might be an issue with how I’m handling the random generation.

// Required modules
const { EmbedBuilder, Client } = require('discord.js');
const settings = require('../settings.json');

module.exports = {
    name: 'generate',
    description: 'Creates random alphanumeric sequences',

    async run(interaction) {
        
        if (interaction.channel.id !== settings.allowedChannel) {
            return interaction.reply({
                embeds: [new EmbedBuilder()
                    .setColor(settings.colors.error)
                    .setTitle('Invalid Channel')
                    .setDescription(`Please use <#${settings.allowedChannel}> for code generation`)
                    .setAuthor({ name: interaction.user.username, iconURL: interaction.user.avatarURL() })
                    .setTimestamp()]
            });
        }
        
        const randomCode = [`${Math.random().toString(36).substring(2)}`+'\n'];
        
        if (interaction.channel.id === settings.allowedChannel) {
            interaction.reply({
                embeds: [new EmbedBuilder()
                    .setColor(settings.colors.success)
                    .setTitle('Code Created')
                    .setDescription(`${randomCode}${randomCode}`)
                    .setAuthor({ name: interaction.user.username, iconURL: interaction.user.avatarURL() })
                    .setTimestamp()]
            });
        }
    }
};

The command runs fine but I’m getting the same output repeated twice in the embed message. How can I modify this to generate multiple different random strings instead of duplicating the same one?

Your problem is in the setDescription line - you’re using ${randomCode}${randomCode} which just shows the same string twice. You need to generate each code separately.

Here’s the fix:

const code1 = Math.random().toString(36).substring(2);
const code2 = Math.random().toString(36).substring(2);

// Then in your embed:
.setDescription(`${code1}\n${code2}`)

Now each variable gets a different random string. If you’re planning to generate more codes later, consider making a helper function.

you’re using the same randomCode for the embed. make two separate codes: const code1 = Math.random().toString(36).substring(2) and const code2 = Math.random().toString(36).substring(2) then use both in the reply. also, drop those array brackets.

Your problem lies with the way you’re handling the randomCode variable. By enclosing it in an array, you’re effectively creating an array with one element, which leads to duplication in your embed. Instead, generate two separate strings: const firstCode = Math.random().toString(36).substring(2); and const secondCode = Math.random().toString(36).substring(2);. This approach ensures that each Math.random() call yields a different result, thereby resolving the duplication issue. The channel validation section seems correct; just focus on the code generation aspect.