Skip to content

Instantly share code, notes, and snippets.

@zkent
Last active August 29, 2015 14:15
Show Gist options
  • Save zkent/9ee94d2dbe01966d1702 to your computer and use it in GitHub Desktop.
Save zkent/9ee94d2dbe01966d1702 to your computer and use it in GitHub Desktop.
A TT.FM bot
This was a simple bot I made for Turntable.fm using Node.js. It uses a Turntable.FM API found at https://github.com/alaingilbert/Turntable-API to listen to the HTTP traffic in the TT.FM room. I spent a lot of time at TT.FM and these bots (there were others) made the room fun and interesting. There were other bots that played word games, moderated the room, etc. At one time, I had a few of these bots running and the would interact with the rooms users and each other in different ways. I kept getting myself booted due to TT.FM's TOS regarding bots but it was still fun. There was another bot we used that would track song plays and votes in the room but I can't find that code anymore.
var Bot = require('./node_modules/ttapi/index');
var AUTH = 'auth+live+6638dc1ee98f4db24420737b6313e1db6b5e25fb';
var USERID = '5019fc9eeb35c1383e000082';
// require("./global.js");
// Set the room for the bot to enter
// var ROOMID = '4fbd96cd2e38176ea601fdc7'; // Code Ninjas
var ROOMID = '4fbc678edf5bcf502e046081'; // Evil Inc
// var ROOMID = '4ded3b7e99968e1d29000047'; // Coding Soundtrack
var OWNER = 'Doofenshmirtz'; // That's me.
var g_AutoVote = false;
var bot = new Bot(AUTH, USERID, ROOMID);
var theUsersList = { };
// voting stats
var TotalVotes = 0;
var TotalUpvotes = 0;
// when entering the room, get a list of all user's currently there
bot.on('roomChanged', function (data) {
// Reset the users list
theUsersList = { };
var users = data.users;
for (var i=0; i<users.length; i++) {
var user = users[i];
theUsersList[user.userid] = user;
}
// console.log(theUsersList);
});
// when a new user enter's the room
bot.on('registered', function (data) {
var user = data.user[0];
theUsersList[user.userid] = user;
if (user.name.match(OWNER)) {
bot.speak('/me bows before Doofenshmirtz.');
}
});
// when a user leaves the room
bot.on('deregistered', function (data) {
var user = data.user[0];
delete theUsersList[user.userid];
});
// If a user sends a chat into the room. Handle interactions here.
bot.on('speak', function (data) {
// Get the data
var name = data.name;
var text = data.text;
// If I say something with the word "praise" in it, chat this back.
if (text.match(/^\/praise$/)) {
if(name.match(OWNER))
{
bot.speak('All Hail @' + name + '!');
}
}
if (text.match(/:coffee:/) && name.match(OWNER)) {
//randsleep(2,3);
bot.speak('Beer me some coffee, friend');
}
if (text.match(/^\/bop$/)) {
if(name.match(OWNER))
{
bot.bop();
}
}
});
// If a user PMs the bot
bot.on('pmmed', function (data) {
// Get the data
var name = data.name;
var text = data.text;
console.log("someone pm'd me.", text);
console.log(data);
//var num = randomFromTo(1, 10);
//if (text.match(/^\/random$/)) {
// console.log(num);
//}
});
// When a new song is played, wait a bit and then upvote, maybe.
bot.on('newsong', function (data) {
var randvote = randomFromTo(1,10);
console.log('randvote: '+ randvote);
TotalVotes++;
if (g_AutoVote && randvote <= 4)
{
var waittime = randomFromTo(1000, 5000);
sleep(waittime);
bot.bop();
console.log('I waited: '+ waittime);
TotalUpvotes++;
}
else {
//var waittime = randomFromTo(5000, 10000);
//sleep(waittime);
console.log('Meh');
}
console.log('Upvote Average: ' + Math.round((TotalUpvotes/TotalVotes)*100) + "%");
});
function randomFromTo(from, to){
return Math.floor(Math.random() * (to - from + 1) + from);
}
function sleep(ms) {
var dt = new Date();
dt.setTime(dt.getTime() + ms);
while (new Date().getTime() < dt.getTime());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment