It is a bit complex, but you probably are only interested in the function getDiceCounts() on line 23: var getDiceCounts = function(msg,idx) {
var rolls = {};
if( msg.inlinerolls
&& msg.inlinerolls[idx]
&& msg.inlinerolls[idx].results
&& msg.inlinerolls[idx].results.rolls[0]
) {
_.each(msg.inlinerolls[idx].results.rolls,function(res){
rolls=_.reduce(_.map(res.results,function(r){
return r.v;
}).sort() || [], function(m,r){
m[r]=(m[r]||0)+1;
return m;
},rolls);
});
}
return rolls;
};
Given a message object and the index of a roll, it will return an object that maps the result to the number of times it occurs, so assuming a message from this command: !something [[7d6]] with rolls of: 5,2,3,5,1,3,5 Then calling: var counts = getDiceCounts(msg,0); will give you this object: {
'1': 1,
'2': 1,
'3': 2,
'5': 3
} You can then turn that into the counts numbers you want with a map: var values=_.map(counts,function(num,face){
return parseInt(face,10)*num;
}); yielding: [1,2,6,15] and take the max value: var maxval = values.max(); You could also use a modified version of the original function and cut out the intermediate _.map() call if you have no need of the actual counts: var getDiceCounts = function(msg,idx) {
var rolls = {};
if( msg.inlinerolls
&& msg.inlinerolls[idx]
&& msg.inlinerolls[idx].results
&& msg.inlinerolls[idx].results.rolls[0]
) {
_.each(msg.inlinerolls[idx].results.rolls,function(res){
rolls=_.reduce(_.map(res.results,function(r){
return r.v;
}).sort() || [], function(m,r){
m[r]=(m[r]||0)+r;
return m;
},rolls);
});
}
return rolls;
}; Hope that helps!