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`);
@MatsiWolf
Copy link

This works great, thank you!

Would be funny if you could make it timeout for a minute or something if you roll a Nat 1

@MontageTM
Copy link

Hello, I love this command! I just noticed that if you roll multiple dice it will always roll a d20.
For example if I do !roll 5 d10, it will still return something like:
MontageTM rolled 19, 17, 7, 14 and 8 with a melziiD20

It wont roll a d10, is that on purpose or a bug?

Thank you!

@PakL
Copy link
Author

PakL commented Jul 8, 2022

Hello, I love this command! I just noticed that if you roll multiple dice it will always roll a d20. For example if I do !roll 5 d10, it will still return something like: MontageTM rolled 19, 17, 7, 14 and 8 with a melziiD20

It wont roll a d10, is that on purpose or a bug?

Thank you!

Thank you for reporting this. This was not intended behavior. I found the issue and fixed it.

@den3606
Copy link

den3606 commented Jan 8, 2023

This code worked. Thank you <3

@baristassdot
Copy link

How would this work on youtube? Can i just c/p and replace "twitch" with "youtube" in code?

@PakL
Copy link
Author

PakL commented Jan 11, 2023

How would this work on youtube? Can i just c/p and replace "twitch" with "youtube" in code?

This should work as it is on YouTube. There is a provider check, but it's only for a Twitch specific emote that replaces 'd20's. Every other platform will get a '🎲20'.
If you replace twitch with youtube you'd get melziiD20 in the messages for YouTube, which is not what you want. Just c/p and leave as it is.

@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