Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- a = decodeURIComponent(`$(querystring)`).toLowerCase(); // a is the query in the user input, turned lowercase
- r = `$(urlfetch https://pastebin.com/raw/GYvHmWMs)`; // fetch !dice help string and store in r
- if(a == `help`){
- r; // output r if a is "help"
- }else{
- 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
- if(b){ // if b is not null, i.e. it contains at least one matching phrase
- 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]
- d=[]; // declare empty array d
- for(i = 0; i < b.length; i++){ // run for the length of b
- 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
- 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
- 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
- }
- }
- 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
- // `🎲 [matching phrase]: [corresponding array of dice roll outputs] Total: [sum of dice roll outputs]/[theoretical maximum sum of dice roll outputs]`
- }else{
- `🎲 ${Math.ceil(Math.random()*100)}`; // if there is no valid dice roll request in a, output a single random number between 1 and 100
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement