I'll echo what Pat and Martijn said. There is not a definitive way to know who creates a graphic, it isn't part of the event. The best you can do is narrow it down to who could have created it.
Here's a script that will remove Potentially Player Created Tokens. It will remove the token if the following are true:
- The created token can be controlled by a player.
- A player that could create the token is on the page where the token was created (either by ribbon or by party split)
- Any player that could have created the token is logged into the game.
The token is then removed. A message will be issued to the chat to the GM to let them know it happened, in case you intended to create the token.
Big Caveat: Many scripts assume that tokens that were added to the page will still be there for them to process. If they perform any sort of asynchronous operations and then try to adjust the token, they will crash. TokenNameNumber, for example will definitely be upset when it goes to change the number on a token (which must be done asynchronously for various reasons) and find the token doesn't exist anymore. I'd like to say I'll fix that, but be aware that it may happen and the message may be strange. YMMV.
The code:
on('ready',()=>{
const playerCanControl = (obj, playerid='any') => {
const playerInControlledByList = (list, playerid) => list.includes('all') || list.includes(playerid) || ('any'===playerid && list.length);
let players = obj.get('controlledby')
.split(/,/)
.filter(s=>s.length);
if(playerInControlledByList(players,playerid)){
return true;
}
if('' !== obj.get('represents') ) {
players = (getObj('character',obj.get('represents')) || {get: function(){return '';} } )
.get('controlledby').split(/,/)
.filter(s=>s.length);
return playerInControlledByList(players,playerid);
}
return false;
};
const getPlayersOnPage = (pageid) => {
let pages = {};
let ribbonPage = Campaign().get('playerpageid');
let psp = Campaign().get('playerspecificpages');
findObjs({type:'player'})
.forEach(p=>{
if(playerIsGM(p.id)){
const lp = p.get('lastpage') || Campaign().get('playerpageid');
pages[lp]=pages[lp]||[];
pages[lp].push(p.id);
} else if(psp.hasOwnProperty(p.id)){
pages[psp[p.id]]=pages[psp[p.id]]||[];
pages[psp[p.id]].push(p.id);
} else {
pages[ribbonPage]=pages[ribbonPage]||[];
pages[ribbonPage].push(p.id);
}
});
if(pageid){
return pages[pageid]||[];
}
return pages;
};
const playerIsOnline = (playerid) => (getObj('player',playerid)||{get:()=>{}}).get('online');
const RemoveIfPlayerCreated = (token) => {
let players = getPlayersOnPage(token.get('pageid')).filter(playerIsOnline);
if(players.length){
let res = players.some(p=>playerCanControl(token,p));
if(res){
sendChat('',`/w gm Removed Player Token for: <code>${token.get('name')}</code>`);
token.remove();
}
}
};
let tokenIds = [];
on('add:graphic',(obj)=>{
tokenIds.push(obj.id);
let id = obj.id;
let c = 10;
const checkToken = ()=>{
let token = getObj('graphic',id);
if(token) {
RemoveIfPlayerCreated(token);
tokenIds=tokenIds.filter(e=>e!==id);
} else if(--c>0){
setTimeout(checkToken,100);
} else {
tokenIds=tokenIds.filter(e=>e!==id);
}
};
setTimeout(checkToken,100);
});
});