Skip to content

Instantly share code, notes, and snippets.

@oatycreates
Last active February 7, 2024 03:39
Show Gist options
  • Save oatycreates/8ce58b002a8f6260c27ecfce905bf479 to your computer and use it in GitHub Desktop.
Save oatycreates/8ce58b002a8f6260c27ecfce905bf479 to your computer and use it in GitHub Desktop.
Tabletop !roll dice roller command for the Nightbot Twitch chatbot. Made for the Oaty Twitch channel: https://www.twitch.tv/Oaty
// The !roll or !r command
// Version: 0.4.0
// Contributions by:
// @patferguson, @JessicaCGlenn
// ==============================
// The first and last line are just for the Nightbot command, they aren't JavaScript
// vvv Copy from BELOW this line vvv
$(user) rolled: $(eval
var inp = "$(1)";
function roll(r){for(var a=r.split(/[d+-]/),t=Math.min(a[0].length>0?Number.parseInt(a[0],10):1,1e3),e=Number.parseInt(a[1],10),n=a.length>2?Number.parseInt(a[2],10):0,o=n=-1!==r.indexOf("-")?-n:n,h=[],l=0;l<t;++l){var f=Math.floor(Math.random()*Math.floor(e)+1);o+=f,h.push(f)}var i=`${o}`;return t<30&&t>1&&(i+=` [${h}]`),0!=n&&(i+=` (modified: ${o-n}${n<0?"-":"+"}${Math.abs(n)})`),i}
roll(inp);
)
// ^^^ To ABOVE this line, paste this into the Command 'Message' field in Nightbot ^^^
// For checking the distribution of dice rolls
// ===========================================
// NOTE - This is just for development, copy the Nightbot command above if you
// just want the !roll dice roller command
// An easy way to test this out is to run the code in a web browser's dev console
// Run this first:
function roll(inp) {
var spl = inp.split(/[d+-]/);
var dCount = Math.min(spl[0].length > 0 ? Number.parseInt(spl[0], 10) : 1, 1000);
var dSize = Number.parseInt(spl[1], 10);
// modify the result from the roll
var mod = spl.length > 2 ? Number.parseInt(spl[2], 10) : 0;
mod = inp.indexOf('-') !== -1 ? -mod : mod;
var res = mod;
var ress = [];
for (var i=0; i < dCount; ++i) {
var roll = Math.floor((Math.random() * Math.floor(dSize)) + 1);
res += roll;
ress.push(roll);
}
var rString = `${res}`;
if (dCount < 30 && dCount > 1) rString += ` [${ress}]`;
if (mod != 0) rString += ` (modified: ${res - mod}${mod < 0 ? '-' : '+'}${Math.abs(mod)})`;
return rString;
}
// Then run this:
var diceSize = 20;
var iterCount = 1000000;
var rollResults = []
for (var fillIter = 1; fillIter <= diceSize; ++fillIter) {
rollResults[fillIter] = 0;
}
for (var i = 0; i < iterCount; ++i) {
var rollResult = roll("1d"+diceSize);
rollResults[Number.parseInt(rollResult)] += 1;
}
for (var logIter = 1; logIter <= diceSize; ++logIter) {
console.log(logIter + " " + rollResults[logIter] + ",");
}
@oatycreates
Copy link
Author

oatycreates commented Mar 6, 2019

Error checking is minimal in this command, though it will do its best to work with the input it is given.

Argument structure is: <number of dice to roll>d<size of dice>[+-]<optional modifier>
E.g. 5d100-3

Supported command formats (assuming !r has been created as an alias of !roll):

!r d20
!r 1d20
!r 1d10+3
!r 5d100-2

Currently the maximum number of dice that can be rolled in one go is 1000 though this limit can be increased if desired by increasing the 1000 that's on the dCount line. Keep in mind that Nightbot has a 1 second timeout for commands.

@oatycreates
Copy link
Author

oatycreates commented Mar 7, 2019

Changelog:

  • 0.1.0: Initial command and docs.
  • 0.2.0: Added 1000 dice rolling cap to limit processing time. Also standardise Number.parseInt to use base-10 radix.
  • 0.3.0: Upgrade output to show the total rolled result, and the breakdown of the dice total and the modifier.
  • 0.3.1: Change default modifier output to use a plus sign instead of negative for a zero modifier.
  • 0.4.0: Changes by the wonderful @JessicaCGlenn: Remove roll modifier in response if there isn't one in the input. Return the list of rolled dice if more than one is rolled, cap to 30 dice. Minified the function output for the Nightbot character limit.

@JessicaCGlenn
Copy link

Hey Pat!

Made some small changes in here in case they're helpful. It appears that for the moment you can't send a pull request through on a gist, so have left them on their own fork for you to peruse and added the working script into my channel so that you can test the results without risk:

https://gist.github.com/JessicaCGlenn/21fd6fa075a177dd1bd4642dd515c1b5

Hope this helps :-D

JG

@oatycreates
Copy link
Author

oatycreates commented Apr 23, 2019

I finally got around to incorporating your dice roller changes @JessicaCGlenn, huge thanks for your work! I've updated the !roll and !r Nightbot commands over at https://www.twitch.tv/oaty as well, looks like it's all working great :D

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