Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
Discord.js Randomiser Command Ignoring Minimum and Maximum Values

I have this Discord bot here that contains a randomiser command - /random (built with new SlashCommandBuilder) - and consists of two required string options - min and max values.

This code...

run: async (bot, interaction, args) => {
        const min = interaction.options.getString('min').replace(/[^\d.-]/g, '')
        const max = interaction.options.getString('max').replace(/[^\d.-]/g, '')

        parseFloat(min, parseFloat(max))

        random = Math.floor(Math.random() * (max - min) + min)

        console.log(min, max)

        interaction.followUp({ content: `${random}` })
        
    }

...extracts the strings from the user's interaction and parses them into floats. The expected result consists of a random number between the min value and the max value.

However, the randomiser does not use the min or max value and instead prints a random number regardless of what the user inputs.

I predict that is quite an inefficient method of a randomiser command. I kindly suggest assistance, and (if) any will be greatly appreciated!

Note: An error does not occur upon command exection.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should use interaction.options.getNumber() instead in your command handler; that way it's already a number:

let min = interaction.options.getNumber('min');
let max = interaction.options.getNumber('max');

function randomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min);
}

await interaction.reply(`Your random number is: ${randomInt(min, max)}`);

Make sure, you add the options with addNumberOption() in your deploy-commands.js file (or whatever you named it:

new SlashCommandBuilder()
  .setName('random')
  .setDescription('Generates a random number!')
  .addNumberOption((option) =>
    option
      .setName('min')
      .setDescription('Enter the minimum number')
      .setRequired(true),
  )
  .addNumberOption((option) =>
    option
      .setName('max')
      .setDescription('Enter the maximum number')
      .setRequired(true),
  )

enter image description here enter image description here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda