Last active
March 30, 2021 10:10
-
-
Save stephaned68/239eba2bf66057679c777f3eda7581ee to your computer and use it in GitHub Desktop.
A Roll20 API script to rename character sheets and linked tokens ** Not compatible with COFantasy.js **
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Usage : | |
* !realname xxxxx | |
* Where xxxxx is the real name of the NPC | |
* If the name contains spaces, replace them by a + in the chat command, as in : | |
* !realname red+blade+assassin | |
* | |
* Add a line to the GM notes on the character sheets | |
* realname=xxxxx | |
* e.g. | |
* realname=Red Blade Assassin | |
* | |
* The script ignores case when searching for character sheets with the realname= setting | |
* If a character sheet is linked to multiple tokens, they will be all renamed and numbered | |
* | |
* For ease of use, you could set up a macro with an action command such as : | |
* !realname ?{NPC ?|Red Blade Assassin,red+blade+assassin|Obi Wan Kenobi,obi+wan+kenobi} | |
* | |
* 2021/03/29 - Revision 1.1 | |
* Added support for a --notokens argument to prevent renaming of tokens | |
*/ | |
on('ready', function () { | |
log('>>> API script realname.js Version 1.1 started <<<'); | |
on('chat:message', function (msg) { | |
const [cmd, ...args] = msg.content.split(/\s+/); | |
/** | |
* Update tokens for character | |
* @param {object} charObj Roll20 API character object | |
* @param {string} real Real name | |
* @returns void | |
*/ | |
const updateTokens = function (charObj, real) { | |
const tokens = findObjs({ | |
_type: 'graphic', | |
_subtype: 'token', | |
represents: charObj.id, | |
}); | |
if (tokens.length == 1) { | |
tokens[0].set('name', real); | |
return; | |
} | |
tokens.forEach((token, ix) => { | |
token.set('name', `${real} ${ix + 1}`); | |
}); | |
}; | |
if (msg.type == 'api' && cmd.indexOf('!realname') === 0) { | |
let realName = args[0] || ''; | |
if (realName === '') { | |
sendChat('API', '/w gm Must specify the real character name'); | |
return; | |
} | |
realName = realName.replace(/\+/g, ' '); | |
let renameTokens = true; | |
if (args.length > 1) { | |
for (const argv of args) { | |
if (argv.toLowerCase() === '--notokens') renameTokens = false; | |
} | |
} | |
const characters = findObjs({ | |
_type: 'character', | |
}); | |
characters.forEach((character) => { | |
character.get('gmnotes', function (gmnotes) { | |
gmnotes = gmnotes.replace(/<p>/g, ''); | |
gmnotes = gmnotes.replace(/<\/p>/g, '\n'); | |
for (const gmnote of gmnotes.split('\n')) { | |
if (gmnote.toLowerCase().indexOf('realname=') === 0) { | |
const real = gmnote.split('=')[1].trim(); | |
if (real.toLowerCase() === realName.toLowerCase()) { | |
character.set('name', real); | |
if (renameTokens) updateTokens(character, real); | |
} | |
} | |
} | |
}); | |
}); | |
} | |
}); | |
}); |
AVERTISSEMENT
Ce script n'est pas compatible avec le script compagnon COFantasy.js pour la fiche de personnage Chroniques Oubliées Fantasy
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Revision 1.1 :
Added support for a --notokens argument to prevent renaming of tokens