Skip to content

Instantly share code, notes, and snippets.

@PakL
Last active January 15, 2024 18:48
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`);
@baristassdot
Copy link

Thank you very much! I'm having a hard time figuring out how to add this command in nightbot, but i'll manage it somehow.

@baristassdot
Copy link

ok, i give up...where do i c/p this code? Thank you!

@PakL
Copy link
Author

PakL commented Jan 11, 2023

When you're logged in on the Nightbot website you should have Commands on the left side. Click that and then on Custom. Add a new command with the blue button on the right.
Set your command (in the examples this was !roll) and paste the code in the message field. Submit, and you're done. Make sure you don't copy the source but the minimized version above the examples.

@baristassdot
Copy link

I've done that... but nightbot just pops that message in the chat like it's not code but regular message!

nightbot
chatbot

am i doing something wrong? I even tried simpler version of "urandom" and nothing. Thank you for fast responses!

@PakL
Copy link
Author

PakL commented Jan 11, 2023

Oh. It looks like Youtube has a stricter limit on message length than Twitch, and you're just not able to paste the whole command.
That is unfortunate, but I can't do anything about this. Compressing this command into less than 200 characters will not be possible.

@baristassdot
Copy link

oh! thank you anyway! :)

@baristassdot
Copy link

Oh. It looks like Youtube has a stricter limit on message length than Twitch, and you're just not able to paste the whole command. That is unfortunate, but I can't do anything about this. Compressing this command into less than 200 characters will not be possible.

Actually i think it's possbile to get it shorter, but i don't know how to code. But maybe i can give you and idea for d20 random roll:

"$(eval Math.floor((Math.random() * 20) + 1))"

the problem with only this code is that it's not related to user who rolled, so if you know how to relate this first row of code to user, it would be 300 characters? :)

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