Making nightbot roll dice?

I’m trying to create Nightbot command that creates multiple random variables and them adds those variables together, for rolling dice. I’ve successfully figured out how to roll individual dice, and add “lucky” and “unlucky” type messages based on the result. The final is to make a “!rollall” command that rolls a complete dice set. It looks something like this:

/me $(user) rolls some dice! The results are:
D4 = $(eval let a=Math.floor(Math.random()*4)+1;`${a}!`) 
D6 = $(eval let b=Math.floor(Math.random()*6)+1;`${b}!`) 
D8 = $(eval let c=Math.floor(Math.random()*8)+1;`${c}!`) 
D100 = $(eval let d=Math.floor(Math.random()*100)+1;`${d}!`) 
D12 = $(eval let e=Math.floor(Math.random()*12)+1;`${e}!`) 
D20 = $(eval let f=Math.floor(Math.random()*10)+1;`${f}!`) 
Total = $(eval let g=${a}+${b}+${c}+${d}+${e}+${f}; `${g}!`)

The first 7 lines work great, generating unique random numbers within their specified ranges and displaying them properly. However, no matter how I try to format the last line I get some variation of broken. Some options are less broken than others, but I can’t figure out how to get it to simply add the variables I’ve generated before.

In searching before posting, I did find some posts that helped me get the individual dice commands working properly, but couldn’t find anything to help me add them together without it breaking.

As a secondary consideration, as-is the code gets very close to the 500 character limit. I would love to be able to make it shorter so that I could add some flavor text at the end regarding lucky or unlucky rolls. Is there a more concise way to generate a random number or etc.?

This is because eval executions do not share any sort of memory/state. You’d need to perform all of these calculations within one eval, and then output it all at the very end.

1 Like

Ah. That was the bit I was missing, I wasn’t aware how it worked.

After more tinkering following that bit of advice, I got it to work! Posting the code here for posterity and/or to help anyone else who may search a similar topic:

/me $(user) rolls all their dice! $(eval 
let a=Math.floor(Math.random()*4)+1;
let b=Math.floor(Math.random()*6)+1;
let c=Math.floor(Math.random()*8)+1;
let d=Math.floor(Math.random()*100)+1;
let e=Math.floor(Math.random()*12)+1;
let f=Math.floor(Math.random()*20)+1;
let t=a+b+c+d+e+f;
`D4=${a} D6=${b} D8=${c} D100=${d} D12=${e} D20=${f} Total=${t}
${t<26?`! Unlucky`:``}${t==6?`! Their dice are cursed by demons!!`:``}
${t>130?`! Lucky`:``}${t==150?`! Their dice are blessed by the gods!!`:``}!`)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.