${o.join(' ')}
`, items: (o) => `${o.join(' ')}`,
attr: {
bare: (o)=>`${ch('@')}${ch('{')}${o}${ch('}')}`,
selected: (o)=>`${ch('@')}${ch('{')}selected${ch('|')}${o}${ch('}')}`,
target: (o)=>`${ch('@')}${ch('{')}target${ch('|')}${o}${ch('}')}`,
char: (o,c)=>`${ch('@')}${ch('{')}${c||'CHARACTER NAME'}${ch('|')}${o}${ch('}')}`
},
bold: (...o) => `${o.join(' ')}`,
italic: (...o) => `${o.join(' ')}`,
font: {
command: (...o)=>`${o.join(' ')}`
}
};
const getLastByPlayer = () => _h.ul(...findObjs({type: 'player'})
.map((p)=>`${_h.bold(p.get('displayname'))}: ${
_h.ul(
`${_h.bold('Shown:')} ${getRelativeDate(state.MotD.playerShownLast[p.id])}`,
`${_h.bold('Seen:')} ${getRelativeDate(state.MotD.playerTimes[p.id])}`
)}`)
);
const showHelp = (playerid) => {
let who=(getObj('player',playerid)||{get:()=>'API'}).get('_displayname');
sendChat('', '/w "'+who+'" '+
_h.outer(
_h.title('MotD',version),
_h.header(
_h.paragraph( `MotD is a simple script that creates a handout named ${_h.code("MotD Note")}. When a player logs in, it whispers the contents of that note to the player. Any formatting that works in the Note is passed on to the player. You can use ${_h.code("%%NAME%%")} as a placeholder for the name of the player.`)
),
_h.subhead('Commands'),
_h.inset(
_h.font.command(
`!motd `,
_h.optional(
`--help`,
`--all`,
`--PLAYER NAME ...`
)
),
_h.ul(
`${_h.bold('--help')} -- Shows the Help screen`,
`${_h.bold('--all')} -- Show the Message of the Day to all logged in players (including GM).`,
`${_h.bold('--PLAYER NAME')} -- Player who should be shown the Message of the Day. Matches are case-insensitive and will match partial names. You can have multiple of this argument to show to multiple players.`
)
),
_h.paragraph(`In the absense of any arguments, will show the Message of the Day to the current player.`),
_h.paragraph(`Show the Message of the day to the player named ${ch('"')}Bob the Slayer${ch('"')}:`),
_h.inset(
_h.preformatted(
'!motd --Bob the Slayer'
)
),
_h.paragraph(`Show the Message of the day to all the players with ${ch('"')}ka${ch('"')} in their name`),
_h.inset(
_h.preformatted(
'!motd --ka'
)
),
_h.paragraph(`Show the Message of the day to all the logged in players.`),
_h.inset(
_h.preformatted(
'!motd --all'
)
),
_h.minorhead('Player Interactions'),
getLastByPlayer()
)
);
};
const handleInput = (msg) => {
let args;
if (msg.type !== "api") {
return;
}
args = msg.content.split(/\s+--/);
switch(args[0]) {
case '!motd':
if(args.includes('help')){
showHelp(msg.playerid);
}
if(1 === args.length){
let player = getObj('player',msg.playerid);
showToPlayer(player);
} else if (playerIsGM(msg.playerid)){
// find players on list
let names = args.slice(1);
let players = findObjs({ type: 'player' });
if(names.includes('all')){
players.forEach(p=>{
if(true == p.get('online')){
showToPlayer(p);
}
});
} else {
args.slice(1).forEach(n=>{
let key = n.toLowerCase().replace(/\s+/,'');
let match = players.find((p)=>{
return -1 !== p.get('displayname').toLowerCase().replace(/\s+/,'').indexOf(key);
});
if(match){
showToPlayer(match);
}
});
}
// send them each the listing.
}
break;
}
};
const handlePlayerLogin = (obj) => {
if( true === obj.get('online') && (
state.MotD.playerTimes[obj.id] === undefined ||
state.MotD.playerTimes[obj.id] < (_.now() - playerOnlineCooldown)
)){
setTimeout(function(){
showToPlayer(obj);
},loginSendDelay);
}
state.MotD.playerTimes[obj.id] = _.now();
};
const handleNoteChange = (obj) => {
if(obj.id === motdNoteId) {
log('MotD Note changed.');
setTimeout(()=>{
obj.get('notes',(notes) => {
obj.get('gmnotes',(gmNotes)=>{
loadMotDNote(notes, gmNotes, obj.get('avatar'));
});
});
},loginSendDelay);
}
};
const handleNoteDestroy = (obj) => {
if(obj.id === motdNoteId) {
createMotDNote();
}
};
const registerEventHandlers = () => {
on('chat:message', handleInput);
on('change:player:_online', handlePlayerLogin);
on('change:handout', handleNoteChange);
on('destroy:handout', handleNoteDestroy);
};
on('ready',function() {
checkInstall(function(){
registerEventHandlers();
});
});
})();