Skip to content

Instantly share code, notes, and snippets.

@PakL
Last active July 3, 2024 16:05
Show Gist options
  • Save PakL/8f4cad54572ecd5f78eef291e48f31d6 to your computer and use it in GitHub Desktop.
Save PakL/8f4cad54572ecd5f78eef291e48f31d6 to your computer and use it in GitHub Desktop.
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

Or something absurd like:
!roll 15d19 - PakL rolled a 118 with 15Γ—:game_die:19
!roll 5d1 - PakL rolled a 5 with 5Γ—:game_die:1

2021-05-25
Any 20 will be replaced by the emote. d20 is now the default instead d6 and I also added the capability to make up to 20 throws at once:
!roll 20 d20 - PakL rolled 13, 6, 4, melziiD20 , 8, 19, 19, 2, 13, 15, 6, 3, 5, 7, 12, 14, 10, 1, 11 and 5 with a melziiD20

2022-07-08

  • Fixed a bug that prevented users to use other dice than d20 when outputting multiple throws and prefexing dice with a "d". (Thanks @MontageTM)
  • Removed Discord emote as bot cannot use them across servers anymore.
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++) 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`);
@PakL
Copy link
Author

PakL commented Jul 3, 2024

If this is still active... Is there a way to remove so this doesn't show up?
image

I'm guessing you mean the D20 emote? Sure, you can easily remove that by setting the e variable to 🎲20:

$(eval var i=parseInt,d=[],e='🎲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`))

Or even better, edit the command so when someone does !roll it shows them rolling 3 dice on a 6 sided dice like this instead of me having to do !roll 3 6?
image 2

You can change the default roll by changing the defaults in the c (number of throws, max. 20), n (number of dice per throw), and s (number of sides on dice) variables. Your example would be:

$(eval var i=parseInt,d=[],e='🎲20',j=0,l,m='$(query)'.match(/^(([0-9]+)\s+)?(([0-9]+)?d([0-9]+)|([0-9]+))$/)??{},c=i(m[2]??3),n=i(m[4]??1),s=i(m[5]??m[6]??6);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`))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment