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?