Skip to content

Instantly share code, notes, and snippets.

@shdwjk
Created December 30, 2014 06:30
Show Gist options
  • Save shdwjk/28d4970fe59fa15d3d67 to your computer and use it in GitHub Desktop.
Save shdwjk/28d4970fe59fa15d3d67 to your computer and use it in GitHub Desktop.
Roll20 API: Message of the Day script -- greets players that log in with the contents of a particular note.
// GIST: https://gist.github.com/shdwjk/28d4970fe59fa15d3d67
var MotD = MotD || (function() {
'use strict';
var version = 0.1,
motdNoteId,
motdNoteName = 'MotD Note',
motdText,
fixedCreateObj = (function () {
return function () {
var obj = createObj.apply(this, arguments);
if (obj && !obj.fbpath) {
obj.fbpath = obj.changed._fbpath.replace(/([^\/]*\/){4}/, "/");
}
return obj;
};
}()),
loadMotDNote = function (text) {
motdText=text;
},
createMotDNote = function() {
var motdNote = fixedCreateObj('handout',{
name: motdNoteName
});
motdText='Welcome to the Game!';
motdNote.set('notes',motdText);
motdNoteId = motdNote.id;
},
checkInstall = function(callback) {
var motdNote = filterObjs(function(o){
return ( 'handout' === o.get('type') && motdNoteName === o.get('name') && false === o.get('archived'));
})[0];
if(motdNote) {
motdNoteId = motdNote.id;
motdNote.get('notes',function(n) {
loadMotDNote(n);
callback();
});
} else {
createMotDNote();
callback();
}
},
handlePlayerLogin = function(obj,prev) {
if( true === obj.get('online') && false === prev._online ) {
setTimeout(function(){
var who=obj.get('displayname').split(/\s/)[0];
sendChat('MotD','/w '+who+' '
+motdText.replace(/%%NAME%%/g,obj.get('displayname'))
);
},10000);
}
},
handleNoteChange = function(obj,prev) {
if(obj.id === motdNoteId) {
obj.get('notes',function(n) {
loadMotDNote(n);
});
}
},
handleNoteDestroy = function(obj) {
if(obj.id === motdNoteId) {
createMotDNote();
}
},
registerEventHandlers = function() {
on('change:player:_online', handlePlayerLogin);
on('change:handout', handleNoteChange);
on('destroy:handout', handleNoteDestroy);
};
return {
CheckInstall: checkInstall,
RegisterEventHandlers: registerEventHandlers
};
}());
on('ready',function() {
'use strict';
MotD.CheckInstall(function(){
MotD.RegisterEventHandlers();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment