Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
D&D 2024 has arrived! Pre-Order the new core rulebooks now.
Create a free account

[Script] Get Character from Player chat

1411572620

Edited 1411580099
DXWarlock
Sheet Author
API Scripter
I got tired of players forgetting to 'talk as' characters for my scripts that needed the character name to do anything such as adding XP, adjusting stats, reading attributes, etc. So I made this so regardless of who they are talking as in the dropdown it converts to the characters name, and finds its sheet, maybe someone else can use it. Only limitation, it assumes everyone only has control of one character sheet, it will pull the first one that it finds they have edit rights over. cWho = the character object itself to use wherever you need to pull info from it. It also: Checks to see if a GM is doing commands as a (GM), and if so ignore looking up a character. It changes msg.who to the characters name if your using it in your script. Example as such: on('chat:message', function (msg_orig) { var msg = _.clone(msg_orig); if (msg.type != "api") return; var command = msg.content.split(" ", 1); //--check if character or player var cWho = findObjs({_type: 'character',name: msg.who})[0]; if (cWho == undefined && msg.who.indexOf("(GM)") == -1) { cWho = RollRight(msg.playerid); msg.who = cWho.get("name"); } //-- if (command == "!something") { //do stuff here } if (command == "!somethingelse") { //do stuff here } }); and this is the function that it uses //--convert to character name function RollRight (whoPC) { var character = findObjs({type: 'character',controlledby: whoPC})[0]; return character; } Just toss that somewhere and call it in any scripts using RollRight(msg.playerid); you have that require the person to talk as their character.
1411579516
The Aaron
Roll20 Production Team
API Scripter
If you have my isGM script installed, you can change that 6th line to: if (cWho == undefined && !isGM(msg.playerid) { and have it work regardless of selected voice. (You could also just write !cWho ) Also, I recently got bitten by the fact that the msg object that is passed into on('chat:message',...) is the SAME one passed into every on('chat:message',...) call. So if you change this in one script, you will change it for all scripts that run after it. In the case where it mattered, I changed my code to do this: on('chat:message', function(msg_orig) { var msg = _.clone(msg_orig); // [...]
1411580074

Edited 1411580294
DXWarlock
Sheet Author
API Scripter
Ah true, didn't think of that. Thanks! I was making it in a closed boxed assumption (for my game anyway) since we use voice, everything sent by anyone is as character to chat for rolls and such. So didn't mind it changing it on every message. I updated it above to add the clone incase someone needs that function. the isGM is a good idea, but not fancy enough on my javascript do check if the IsGM script is in place and use it if it is, use current way if not. So dont want to put it in there hardcoded :)
1411580511
The Aaron
Roll20 Production Team
API Scripter
No worries, I was just pointing it out as I designed that isGM() script to be useful by everyone, though I don't know that many are. I think it's the best possible solution to the problem at this time, as it cannot be fooled and handles detection automatically (you just need to speak openly as you once to be detected.). At some point, was considering adding additional features to it, such as named permissions so that you could grant added capabilities to certain of your players (the mage is allowed to summon spell templates, the druid is allowed to summon animal companions, the cleric is allowed to heal others, etc..). Anyway.. off topic for this thread. =D