Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JessicaCGlenn/21fd6fa075a177dd1bd4642dd515c1b5 to your computer and use it in GitHub Desktop.
Save JessicaCGlenn/21fd6fa075a177dd1bd4642dd515c1b5 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.3.1
// =======================
// 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] + ",");
}
@JessicaCGlenn
Copy link
Author

Updated the roll function so that it removes the modifier in the response if there is no modifier.

Also updated to return the list of dice results as long as there is more than one die, and less than 30 dice, to ensure that it doesn't clog up chat logs.

Then minified the function so that it will fit in the nightbot script input. Tested on nightbot on twitch.tv/dianacprince.

To test either follow the instructions in this script, or go to twitch.tv/dianacprince and type !r $inp
where $inp will be equal to your roll requirements, i.e. 3d8, 4d12+4.

Have fun!

@garajeux
Copy link

garajeux commented May 13, 2021

Hi and thx for this code. I am new from all of that.
I have installed Nightbot but i can t writte all the scrcipt in the command Night Bot, message is too long, Where i have done a mistake ?
For by advance.
I tried to put that :

$(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);
)

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