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

201
Visualizações
Discord.js DND Dice Roll Input Change Number

I've made a "working" dice roll function on my bot. Works fine for what I need, but was wondering how I could make it so depending on what is said in discord it rolls.

So saying !rolld6 would use 6 in var response instead of 20.

if (message.content.toLowerCase().includes("!rolld20")) {
const ayy = client.emojis.cache.find(emoji => emoji.name === "diceroll");
var response = [Math.floor(Math.random() * ((20 - 1) + 1) + 1)];
            message.channel.send(`${ayy}` + `Rolling.`)
                .then(msg => {
                    setTimeout(function() {
                        msg.edit(`${ayy}` + `Rolling..`)
                    }, 1000);
                    setTimeout(function() {
                        msg.edit(`${ayy}` + `Rolling...`)
                    }, 2000)
                    setTimeout(function() {
                        msg.edit(`${ayy}` + `Rolling....`)
                    }, 3000)    
            setTimeout(function() {
                        
            msg.edit(`${ayy}` + " Rolled... " + response + " " + `${ayy}`).then().catch(console.error)  
                    }, 4000)    
            
                })


return;
}

Not even sure what I would search to figure this out so any help is greatly appreciated!

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

0

Use a regular expression instead of .includes, and then extract the digits after the d to randomize.

You don't need to subtract 1, then add 1 right after that - those cancel out, nor do you need to put the result into an array - interpolating just the random number generated into the message will work fine.

const match = message.content.match(/!rolld(\d+)/i);
if (match) {
    const die = match[1];
    const response = 1 + Math.floor(Math.random() * die);    
    const ayy = client.emojis.cache.find(emoji => emoji.name === "diceroll");
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