
So yesterday, I was happily working on an API and found that while text chat messages for dice rolling were showing up fine and there were no errors in my console, the 3 d dice have stopped rolling from an API. They work as expected when I enter something like /roll 3 d6 in the text chat, but not when my api brings that up.
I've checked my Automatically roll 3D dice and it is checked. I've turned it off and on. Still no go.
I deleted all my scripts including those from the script library leaving just the dice rolling API I had created with the help of some folks here. Still no 3d dice. The sendChat command still has {use3d: true} as its 4th argument.
I'm pretty sure I've done nothing to change the script.
Any ideas?
Code is (with help from https://app.roll20.net/forum/post/8951266/request-javascript-criique/?pageforid=8980029#post-8980029on('ready',%20()%20=%3E%20{
on('ready', () => {
function checkDice (content) {
//First, trim off any spaces.
//content=content.trim();
//Now trim out
let args = content.split(/\s+/);
let numDice = parseInt(args[0]);
let numTarget = parseInt(args[1]);
let dice = {};//Defines dice as a prpoertytthat can have various values
// error checking goes here, and if an error is found, a message string is saved to dice.error
//Verify that numDice is a number.
if (isNaN(numDice)) {
dice.error = '===============<br>**Crossfire Roll Error**<br>' +
'You entered: !cf ' + content +'.<br>The correct ' +
'format is:<br><br>!cf [no of dice] [OPTIONAL to hit] <br><br>' +
'**Examples**:<br>!cf 3 will roll 3 dice, with 5,6 to hit.' +
'<br>!cf 2 6 will roll 2 dice, just a 6 hits (note just a ' +
'space, no comma).';
}
// if an error exists, end the function early, so the script can be ended back in the main body.
if(dice.hasOwnProperty('error')) {
//Do nothing, dice.error has been written.
} else {
// save the calculated and validated dice values.
dice.number = numDice,
dice.target = numTarget || 5;
}
return dice;
}
function reportHitrange(target) {
let range = '';
switch(target) {
case 6:
range = 'only 6 hits (-1 pip).';
break;
case 5:
range = '5 or 6 hits (normal).';
break;
default:
let pips = 5 - target;
if (pips == 1) {
pips = ' (+1 pip)';
} else {
pips = ' (+' + pips + ' pips)';
}
range = target + ' to 6 hits' + pips;
}
return range;
}
function AreDiceOk(DiceToRoll, DiceToHit) {
let DiceMessage = 'OK';
//log('---> DiceToRoll: ' + DiceToRoll + '; DiceToHit: ' + DiceToHit);
//Dice range must be from 1 to 9
if (DiceToRoll <= 0) {
DiceMessage = 'It makes no sense to specify ' + DiceToRoll +
' dice.';
} else if (DiceToRoll >= 9) {
DiceMessage = "You have specified " + DiceToRoll +
" dice to be rolled. There is no requirment in Crossfire or " +
"Tim's Armour Rules for 9 or more dice to be rolled.";
}
//log('Checked DiceToRoll: ' + DiceMessage);
//log('DiceToHit: ' + DiceToHit);
//Now make sure there are 2 to 6 specified in DiceToHit
if (DiceToHit <= 1 || DiceToHit > 6) {
log('Checked DiceToHit and there is an issure. Before consideration: ' + DiceToHit + ' - ' + DiceMessage);
if (DiceMessage !== 'OK') {
DiceMessage = DiceMessage + '<br><br> Also, you ';
} else {
DiceMessage = 'You ';
}
log('Checked for preceding DiceMessage Contents: ' + DiceMessage);
DiceMessage = DiceMessage +
'have specified ' + DiceToHit + ' or higher to hit.' +
' You can only enter 2 to 6 for the second argument.';
}
log('Result: ' + DiceMessage);
return DiceMessage;
}
on('chat:message', function(msg) {
//Convert what is typed to lowercase and trim.
let chatcommand = msg.content.trim().toLowerCase();
if(msg.type == 'api' && chatcommand.startsWith('!cf')) {
// since 'player|' + msg.playerid is used multiple times, store it a variable.
let sender = 'player|' + msg.playerid;
// move error checking off into its own function, so it can be handled neatly
//trim off !cf. This is required in case the command does not have
//a space, for example, !cf4
let dice = chatcommand.replace('!cf',"").trim();//necessary to trim in case of leading space
//Pass dice to function checkDice
dice = checkDice(dice);
// if this returns an error, we have to end the function and report the error.
if(dice.hasOwnProperty('error')) {
sendChat(sender, dice.error);
return;//break out of function. I'm not comfortable with this
} else {
let DiceOK = AreDiceOk(dice.number, dice.target);
if (DiceOK !== 'OK') {
sendChat(sender, '===============<br>**Crossfire Roll** (' +
msg.content.trim() + ')<br><br>' + DiceOK);
return;
}
}
// if we reach this point numDice and numTarget are valid, and are accessed with dice.number and dice.target
// saving the output in its own variable makes the sendChat line neater and easier to read.
//With all those commas, the null and the {use3d} part, it would be easy to make a syntax error. This helps avoid that.
let hitRange = reportHitrange(dice.target);
let message = `===============
**Crossfire Roll** (${msg.content.trim()})
${dice.number} dice; ${hitRange}
/roll ${dice.number}d6>${dice.target}`;
//message = `&{template:default} {{name=Crossfire Roll (${msg.content})}} {{Roll=${dice.number}d6; ${hitRange}}} {{Result=[[${dice.number}d6>${dice.target}]]}}`;
sendChat(sender, message, null, {use3d: true});
}
});
});