master
1// npm install ttapi prompt
2//
3
4
5// TODO
6//
7// * reverse userid to username lookup - async callback hell
8// * reverse roomid to roomname lookup - async callback hell
9// * add bop when master bops
10
11var Bot = require('ttapi')
12 , repl = require('repl');
13var prompt = require('prompt');
14
15// prompt configuration
16prompt.message = "";
17prompt.delimiter = "";
18prompt.start();
19;
20// nice pile-o-globals :(
21var Config = require('./config.js');
22var config = new Config();
23var AUTH = config.AUTH;
24var USERID = config.USERID;
25var ROOMID = config.ROOMID;
26var chat_on = config.chat_on;
27var master_id = config.master_id;
28
29var master_uid;
30var master_loc;
31var self_uid = USERID;
32var self_loc = ROOMID;
33var self_id;
34
35var tmp_username;
36var tmp_roomname;
37
38// create a bot and connect with provided credentials
39var bot = new Bot(AUTH, USERID);
40if (!chat_on){ repl.start('> ').context.bot = bot; }
41
42function sleep(milliSeconds) {
43 var startTime = new Date().getTime();
44 while (new Date().getTime() < startTime + milliSeconds);
45}
46
47// make this non-blocking with callbacks?
48function followMaster(master_loc,self_loc,moved_yet){
49 if (master_loc==undefined){ followMaster(self_loc,self_loc,false); }
50 if ((master_loc==self_loc)&&(!moved_yet)) {
51 bot.stalk(master_uid,function(data){
52 console.log("Looking for -", master_id);
53 if ((data.roomId==undefined)||(master_loc==data.roomId)){
54 sleep(10000);
55 followMaster(master_loc,self_loc,moved_yet);
56 } else {
57 console.log("Found -", data.roomId);
58 master_loc = data.roomId;
59 followMaster(master_loc,self_loc,moved_yet);
60 }
61 });
62 }
63 if ((master_loc!=self_loc) && (!moved_yet)) {
64 moved_yet = true;
65 console.log("Moving to -", master_loc);
66 bot.roomRegister(master_loc); // has a break?
67 followMaster(master_loc,self_loc,moved_yet); // never get here
68 }
69 if ((master_loc==self_loc) && (moved_yet)) { // never get here
70 console.log("finished?");
71 }
72}
73
74function chat(){
75 prompt.get(['chat'], function (err, result) {
76 if (result.chat.match(/^!bop$/)){
77 bot.bop();
78 console.log("Bopping for - <cli>");
79 } else {
80 bot.speak(result.chat);
81 }
82 chat();
83 })
84}
85
86// these are not working at all :/
87function getUsername(userid){
88 bot.getProfile(userid, function(data){
89 console.log("inner:",data.name);
90 return data.name;
91 });
92}
93
94// these are not working at all :/
95function getRoomName(roomid){
96 var returnid;
97 bot.directoryRooms(function(data) {
98 console.log(data);
99 });
100}
101
102bot.on('ready', function (data) {
103 bot.roomRegister(ROOMID);
104 if (chat_on) {
105 console.log("Chat - on");
106 chat(); // initial chat call
107 } else {
108 console.log("Chat - off");
109 }
110});
111
112bot.on('speak', function (data) {
113 if (data.text.match(/^!master$/)) {
114 if (data.userid==master_uid){
115 bot.speak('You are my master @m_a');
116 } else {
117 bot.speak('@m_a is my master');
118 }
119 }
120 if (data.text.match(/bop/)) {
121 speaker = data.userid;
122 bot.roomInfo( function (data) {
123 if ( (speaker==master_uid) &&
124 (speaker==data.room.metadata.current_dj)) {
125 bot.speak("Sorry, I don't do self-bops... Especially not for you, Master.");
126 } else if (speaker==data.room.metadata.current_dj) {
127 bot.speak("Sorry, I don't do self-bops.");
128 } else {
129 //bot.speak("Bopping for -", getUsername(speaker));
130 bot.bop();
131 }
132 });
133 }
134 if (data.text.match(/^!userid/)){
135 console.log("split: ",data.text.split(8));
136 }
137 if (data.text.match(/^!roomid/)){
138 console.log("split: ",data.text.split(8));
139 getRoomName();
140 }
141 console.log('<', data.name, '> ', data.text);
142});
143
144bot.on('update_votes', function (data) {
145 console.log('\t\t(',
146 "+", data.room.metadata.upvotes," ",
147 "-", data.room.metadata.downvotes," ",
148 "[", data.room.metadata.listeners,"])");
149});
150
151bot.on('registered', function (data) {
152 if (master_uid == undefined){
153 bot.getUserId(master_id, function (data) {
154 master_uid = data.userid;
155 console.log("Master set -", master_id);
156 followMaster(self_loc,self_loc, false);
157 })
158 }
159
160 for (var i=0; i<data.user.length; i++){
161 console.log("[",data.user[i].name,"] entered");
162 }
163});
164
165bot.on('deregistered', function (data) {
166 if (data.user[0].userid == master_uid) {
167 console.log("Master left -", self_loc);
168 followMaster(self_loc,self_loc,false);
169 } else {
170 console.log("[", getUsername(data.user[0].userid), "] exited");
171 }
172});
173
174bot.on('roomChanged', function (data) {
175 console.log('Now in room -',data.room.name, "(",data.room.roomid,")");
176 self_loc = data.room.roomid;
177 self_id = data.room.name;
178});
179