
I don't write a lot of JavaScript, and I have not written anything for the Roll20 API before. I wanted to patch my 5e adventure products to clear the name from the 5e NPCs to avoid those situations where the NPC name in a template can accidentally give away information about who or what the PCs are fighting. I figured that the easiest (?) way to do this was a custom script. I heavily plagiarized parts from various examples, read a bit of the documentation, and put the following together which seems to work. The "npc_name_flag" on the 5e by Roll20 sheets are set to "0", which avoids emitting the NPC's name when a roll template is triggered from the NPC sheet. Is there anything I am missing? Any deficiencies? Problems that could crop up? Unintended side effects? // Register a function for the 'ready' event. When it occurs, which happens only once
// this function will get called. This avoids getting events resulting from the
// loading of all the game state into the API
on('ready',()=>{
// Declare a function called myFunc. This is using the "Fat Arrow" function syntax
// and the const declaration from Javascript ES6
const myFunc = ()=>{
sendChat('myFunc','myFunc was called!');
var characters = findObjs({_type: 'character'});
characters.forEach(function(chr) {
var attr = findObjs({name: "npc_name_flag",_type: 'attribute',_characterid: chr.id}, {caseInsensitive: true})[0];
if (!attr) {
attr = createObj('attribute', {
name: "npc_name_flag",
current: "0",
max: "",
characterid: chr.id
});
} else {
attr.set('max', "");
attr.set('current', "0");
}
sendChat(chr.get('name'), attr.get('current'));
});
};
// Register a function for the 'chat:message' event. This event occurs for all
// chat messages, so it's important to filter down to just the ones you care about
on('chat:message',msg=>{
// First check the type is an API message. API messages are not show shown in chat
// and begin with a ! in the first character of the message.
//
// Next, make sure this is our API message. The regular expression checks that the
// command starts with "!call-my-func" and either ends or has a space, all case
// insensitive.
if('api'===msg.type && /^!call-my-func(\b\s|$)/i.test(msg.content)){
myFunc();
}
});
});