Advertisement
RokettoJanpu

Dice Indented

May 27th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. a = decodeURIComponent(`$(querystring)`).toLowerCase(); // a is the query in the user input, turned lowercase
  2. r = `$(urlfetch https://pastebin.com/raw/GYvHmWMs)`; // fetch !dice help string and store in r
  3. if(a == `help`){
  4.     r; // output r if a is "help"
  5. }else{
  6.     b = a.match(/\d+\s?d\s?\d+/gi); // b is an array containing all phrases in the query matching the following pattern: [any number of digits][0 or 1 spaces]d[0 or 1 spaces][any number of digits] <- global, case-insensitive search... though the i flag is redundant due to toLowerCase() on query
  7.     if(b){ // if b is not null, i.e. it contains at least one matching phrase
  8.         c = b.map(z => z.split(`d`).map(y => parseInt(y.trim()))); // run map() on b and store in c, transforming each element into an array of 2 integers, e.g. if an element in b is string `2d300` then the corresponding element in c is array [2,300]
  9.         d=[]; // declare empty array d
  10.         for(i = 0; i < b.length; i++){ // run for the length of b
  11.             d[i] = []; // declare each element in d as an array to contain the numbers for each dice roll, each element corresponds to an array of dice roll outputs
  12.             for(j = 0; j < c[i][0]; j++){ // run for the length of each first number in c, e.g. if c=[[2,300], [5,45], [10,1000]] then the nested for loops will run 2 times, 5 times, and 10 times
  13.                 d[i].push(Math.ceil(Math.random()*c[i][1])); // push random number with each second number as the random() upper bound to its corresponding array in d
  14.             }
  15.         }
  16.         b.map((z, i) => `🎲 ${z}: ${d[i].join(` `)} Total: ${d[i].reduce((f, g)=> f+g)}/${c[i][0]*c[i][1]}`).join(` `).slice(0, 400); // gather Nightbot's response, run map() on b, each element in b (each matching phrase) gets transformed into
  17.         // `🎲 [matching phrase]: [corresponding array of dice roll outputs] Total: [sum of dice roll outputs]/[theoretical maximum sum of dice roll outputs]`
  18.     }else{
  19.         `🎲 ${Math.ceil(Math.random()*100)}`; // if there is no valid dice roll request in a, output a single random number between 1 and 100
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement