In handleInput() , any where you refer to playerid , it needs to be msg.playerid . In the other functions, I'm passing it in as an argument, and so it arrives with the name playerid . In handleInput() , it is on the msg object, so you have to refer to it there. You will also need to add the who variable in, being sure to reference the playerid off of the msg object there as well. That should clear it up. Here is a version with those changes made: // GIST: <a href="https://gist.github.com/XXXXXXXXXXXXXXXXX" rel="nofollow">https://gist.github.com/XXXXXXXXXXXXXXXXX</a>
var Ammo = Ammo || (function() {
'use strict';
var version = 0.11,
adjustAmmo = function (playerid,attr,amount) {
var val = parseInt(attr.get('current'),10)||0,
max = parseInt(attr.get('max'),10)||10000,
adj = (val+amount),
who = getObj('player',playerid).get('_displayname').split(' ')[0],
chr = getObj('character',attr.get('characterid')),
valid = true;
if(adj < 0 ) {
sendChat('Ammo', (isGM(playerid) ? '/w gm ' : '/w '+who+' ')
+'<div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">'
+'<b>'+chr.get('name') + '</b> does not have enough '+attr.get('name')+'. Needs '+Math.abs(amount)+', but only has '
+'<span style="color: #ff0000;">'+val+'</span>.'
+'<span style="font-weight:normal;color:#708090;>[Attribute: '+attr.get('name')+']</span>'
+'</div>'
);
valid = false;
} else if( adj > max) {
sendChat('Ammo', (isGM(playerid) ? '/w gm ' : '/w '+who+' ')
+'<div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">'
+'<b>'+chr.get('name') + '</b> does not have enough storage space for more '+attr.get('name')+'s. Needs '+adj+', but only has space for '
+'<span style="color: #ff0000;">'+max+'</span>.'
+'<span style="font-weight:normal;color:#708090;>[Attribute: '+attr.get('name')+']</span>'
+'</div>'
);
adj = max;
}
if( isGM(playerid) || valid ) {
attr.set({current: adj});
sendChat('Ammo', (isGM(playerid) ? '/w gm ' : '/w '+who+' ')
+'<div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">'
+'<b>'+chr.get('name') + '</b> '+( (adj<val) ? 'uses' : 'gains' )+' '+Math.abs(amount)+' '+attr.get('name')+' and has '+adj+' remaining.'
+'</div>'
);
if(!valid) {
sendChat('Ammo', '/w gm '
+'<div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">'
+'Ignoring warnings and applying adjustment anyway. Was: '+val+'/'+max+' Now: '+adj+'/'+max
+'</div>'
);
}
}
},
showHelp = function(playerid) {
var who=getObj('player',playerid).get('_displayname').split(' ')[0];
sendChat('',
'/w '+who+' '
+'<div style="border: 1px solid black; background-color: white; padding: 3px 3px;">'
+'<div style="font-weight: bold; border-bottom: 1px solid black;font-size: 130%;">'
+'Ammo v'+version
+'</div>'
+'<div style="padding-left:10px;margin-bottom:3px;">'
+'<p>Ammo provides inventory management for ammunition stored in a character'
+'attribute. If the adjustment would change the attribute to be below 0 or above'
+'it\'s maximum value, a warning will be issued and the attribute will not be'
+'changed.</p>'
+( (isGM(playerid)) ? '<p><b>Note:</b> As the GM, bounds will not be '
+'enforced for you. You will be whispered the warnings, but the operation '
+'will succeed. You will also be told the previous and current state in case '
+'you want to revert the change.' : '')
+'</div>'
+'<b>Commands</b>'
+'<div style="padding-left:10px;">'
+'<b><span style="font-family: serif;">!ammo <id> <attribute> <amount> </span></b>'
+'<div style="padding-left: 10px;padding-right:20px">'
+'This command requires 3 parameters:'
+'<ul>'
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">id</span></b> -- The id of the character which has the attribute. You can pass this as @{selected|token_id} and the character id will be pulled from represents field of the token.'
+'</li> '
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">attribute</span></b> -- The name of the attribute representing ammunition.'
+'</li> '
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">amount</span></b> -- The change to apply to the current quantity of ammo. Use negative numbers to decrease the amount, and possitive numbers to increase it.'
+'</li> '
+'</ul>'
+'</div>'
+'</div>'
+'<div style="padding-left:10px;">'
+'<b><span style="font-family: serif;">!get-represents [token_id]</span></b>'
+'<div style="padding-left: 10px;padding-right:20px">'
+'This command will show the character id for the supplied token id, or will list the character ids for all the selected tokens.'
+'<ul>'
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">token_id</span></b> -- The id of the token to lookup, usually supplied with: @{selected|token_id}'
+'</li> '
+'</ul>'
+'</div>'
+'</div>'
+'</div>'
);
},
HandleInput = function(msg_orig) {
var msg = _.clone(msg_orig),
args,attr,amount,chr,token,text='',
who ;
if (msg.type !== "api") {
return;
}
if(_.has(msg,'inlinerolls')){
msg.content = _.chain(msg.inlinerolls)
.reduce(function(m,v,k){
m['$[['+k+']]']=v.results.total || 0;
return m;
},{})
.reduce(function(m,v,k){
return m.replace(k,v);
},msg.content)
.value();
}
who = getObj('player',msg.playerid).get('_displayname').split(' ')[0];
args = msg.content.split(" ");
switch(args[0]) {
case '!ammo':
if(args.length > 1) {
chr = getObj('character', args[1]);
if( ! chr ) {
token = getObj('graphic', args[1]);
if(token) {
chr = getObj('character', token.get('represents'));
}
}
if(chr) {
if(! isGM(msg.playerid)
&& ! _.contains(chr.get('controlledby').split(','),msg.playerid)
&& ! _.contains(chr.get('controlledby').split(','),'all')
)
{
sendChat('Ammo', '<div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">'
+'You do not control the specified character: '+chr.id
+'</div>'
);
sendChat('Ammo', '/w gm <div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">'
+'<b>'+getObj('player',msg.playerid).get('_displayname')+'</b> attempted to adjust attribute <b>'+args[2]+'</b> on character <b>'+chr.get('name')+'</b>.'
+'</div>'
);
return;
}
attr = findObjs({_type: 'attribute', _characterid: chr.id, name: args[2]})[0];
}
amount=parseInt(args[3],10);
if(attr && amount) {
adjustAmmo(msg.playerid,attr,amount);
} else {
if(attr) {
sendChat('Ammo', (isGM(msg.playerid) ? '/w gm ' : '/w '+who+' ')
+'<div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">'
+'Amount ['+args[3]+'] is not correct. Please specify a positive or negative integer value like -1 or 4.'
+'</div>'
);
} else {
if(chr) {
sendChat('Ammo', (isGM(msg.playerid) ? '/w gm ' : '/w '+who+' ')
+'<div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">'
+' Attribute ['+args[2]+'] was not found. Please verify that you have the right name.'
+'</div>'
);
} else {
sendChat('Ammo', (isGM(msg.playerid) ? '/w gm ' : '/w '+who+' ')
+'<div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">'
+( (undefined !== token) ? ('Token id ['+args[1]+'] does not represent a character. ') : ('Character/Token id ['+args[1]+'] is not valid. ') )
+'Please be sure you are specifying it correctly, either with @{selected|token_id} or copying the character id from: !get-represents @{selected|token_id} '
+'</div>'
);
}
}
}
} else {
showHelp(msg.playerid);
}
break;
case '!get-represents':
if(args.length > 1) {
chr = getObj('character', args[1]);
if( ! chr ) {
token = getObj('graphic', args[1]);
if(token) {
sendChat('Ammo', (isGM(msg.playerid) ? '/w gm ' : '')
+'<div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">'
+'The specified token represents the following character:'
+'<ul><li>'+ ( ('' !== token.get('name')) ? token.get('name') : 'BLANK' )+' -> '+ ( ('' !== token.get('represents')) ? token.get('represents') : 'NOTHING' ) + '</li></ul>'
+'</div>'
);
} else {
sendChat('Ammo', (isGM(msg.playerid) ? '/w gm ' : '')
+'<div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">'
+' Token id ['+args[1]+'] is not valid.'
+'</div>'
);
}
}
} else if (msg.selected && msg.selected.length) {
_.each(msg.selected, function(s) {
token = getObj('graphic', s._id);
if(token) {
text += '<li>'+ ( ('' !== token.get('name')) ? token.get('name') : 'BLANK' )+' -> '+ ( ('' !== token.get('represents')) ? token.get('represents') : 'NOTHING' ) + '</li>';
}
});
sendChat('Ammo', (isGM(msg.playerid) ? '/w gm ' : '')
+'<div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">'
+'The selected tokens represent the following characters:'
+'<ul>' + text + '</ul>'
+'</div>'
);
} else {
showHelp(msg.playerid);
}
break;
}
},
RegisterEventHandlers = function() {
on('chat:message', HandleInput);
};
return {
RegisterEventHandlers: RegisterEventHandlers
};
}());
on("ready",function(){
'use strict';
var Has_IsGM=false;
try {
_.isFunction(isGM);
Has_IsGM=true;
}
catch (err)
{
log('--------------------------------------------------------------');
log('Ammo requires the isGM module to work.');
log('isGM GIST: <a href="https://gist.github.com/shdwjk/8d5bb062abab18463625" rel="nofollow">https://gist.github.com/shdwjk/8d5bb062abab18463625</a> ');
log('--------------------------------------------------------------');
}
if( Has_IsGM )
{
Ammo.RegisterEventHandlers();
}
});
Hope that helps!