Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!