Skip to content

AFK Police: A How To

alaingilbert edited this page Feb 28, 2012 · 5 revisions

This tutorial does not tell you how to make a bot, it only discusses how to implement a very basic AFK check.

Step One: Defining objects

You'll need to set up some objects. So somewhere near the top of your page, add something along these lines:

var lastSeen = {}
  , djs      = [];

Step Two: Updating Idle

This is what keeps our djs list up-to-date.

bot.on('roomChanged', function (data) {
   djs = data.room.metadata.djs;
});
bot.on('add_dj', function (data) {
   djs.push(data.user[0].userid);
});
bot.on('rem_dj', function (data) {
   djs.splice(djs.indexOf(data.user[0].userid), 1);
});

This is what keeps track of the users idle time. It's just a simple function

justSaw = function (uid) {
   return lastSeen[uid] = Date.now();
};

Put that, somewhere. Now, we need to update the users. Find the identifier for each respective action (speak, upvote, whatnot - it's commonly data.userid or data[0].userid, depending on how you've set up your bot, or how the bot you're using has been set up.

So, whenever you want to update your user add that line. For example:

bot.on('speak', function (data) {
   //your other code
   justSaw(data.userid);
});

That updates their time. Now to police it.

Step Three: Checking the AFK.

First, you'll need a function like this, to turn date.now into minutes ago, and check against your limit:

isAfk = function (userId, num) {
   var last = lastSeen[userId];
   var age_ms = Date.now() - last;
   var age_m = Math.floor(age_ms / 1000 / 60);
   if (age_m >= num) {
      return true;
   };
   return false;
};

Step Four: Doing the work.

This is a function that you'll need to add that will check the times against all the DJs. And then you're done:

afkCheck = function () {
   var afkLimit = 10; //An Afk Limit of 10 minutes.
   for (i = 0; i < djs.length; i++) {
      dj = djs[i]; //Pick a DJ
      if (isAfk(dj, afkLimit)) { //if Dj is afk then
         bot.remDj(dj); //remove them
      }; 
   };
};
setInterval(afkCheck, 5000) //This repeats the check every five seconds.

Now, once you know this, you can got all kinds of crazy, and do warnings and whatnot. But I'll leave that up to you.

Clone this wiki locally