Skip to content

Instantly share code, notes, and snippets.

@PakL
PakL / Nightbot dice roll.md
Last active January 15, 2024 18:48
Nightbot command to roll dice

Simple Nightbot command to roll any number of any kind of dice

$(eval var i=parseInt,d=[],e='$(provider)'=='twitch'?'melziiD20':'🎲20',j=0,l,m='$(query)'.match(/^(([0-9]+)\s+)?(([0-9]+)?d([0-9]+)|([0-9]+))$/)??{},c=i(m[2]??1),n=i(m[4]??1),s=i(m[5]??m[6]??20);c=c>20?20:(c<1||!c?1:c);n=(n<1?1:n);s=(s<1?1:s);for(;j<c;j+=1){d.push(Math.round(n*(s-1)*Math.random())+n)}r=`$(user) rolled ${d.join(', ')} with ${n<2?'a ':n+'×'}🎲`+s;l=r.lastIndexOf(',');(l<0?r:r.substr(0,l)+' and'+r.substr(l+1)).replace(/( |🎲)20(,| |$)/g,` ${ e } $2`))

Examples:
!roll - PakL rolled a 2 with a 🎲6
!roll 20 - PakL rolled a 14 with a 🎲20
!roll 2d6 - PakL rolled a 11 with 2×:game_die:6