-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.js
262 lines (231 loc) · 6.3 KB
/
functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Stopgap variables
var playerDBID = -1;
var songDBID = -1;
// Fires when a new DJ gets on the turntables.
// Does nothing currently.
OnAddDJ = function(data) {
var playerName = data.user[0].name;
var userid = data.user[0].userid;
if(nconf.get('enforceQueue'))
{
if(djQueue.length > 0 && djList.length >= 4)
{
if(isNextDJ(userid))
{
djQueue.pop();
}
else
{
// remove the offender
bot.remDj(userid);
bot.speak('Sorry ' + playerName + '. That spot is still reserved for ' + djQueue[0].name);
return;
}
}
}
djList.push(data.user[0]);
if(nconf.get('autodj'))
{
if(djList.length > 3)
{
bot.remDj();
}
}
//console.log(JSON.stringify(djList));
//var str = '\u0002' + playerName + '\u000F is now on the decks!';
//client.say(nconf.get('ircroom,str'));
};
// Fires when a DJ gets off the turntables.
// Does nothing currently.
OnRemDJ = function(data) {
var playerName = data.user[0].name;
var userid = data.user[0].userid;
var newList = djList;
djList.forEach(function(dj, index) {
if(dj.userid == userid){
newList.splice(index,1);
}
});
djList = newList;
console.log(JSON.stringify(djList));
if(nconf.get('autodj'))
{
if(djList.length <= 2)
{
bot.addDj();
bot.speak("Starting to AutoDJ. If you do not want this, '!dj Auto'");
}
}
//var str = '\u0002' + playerName + '\u000F has given up!';
//client.say(nconf.get('ircroom,str'));
};
// Fires when there are no more DJs on the turntables.
OnNosong = function(data) {
var str = '\u0002Oh noes! The room is dead!\u000F';
if(nconf.get('verbose')) client.say(nconf.get('ircroom'),str);
};
// Fires when a song ends before the next song starts playing.
// We get up/downvote info here. Maybe switch to recording votes as they come in with update_votes and track who is voting?
OnEndsong = function(data) {
var pid = playerDBID;
playerDBID = -1;
var sid = songDBID;
songDBID = -1;
var meta = data.room.metadata;
var upvotes = meta.upvotes;
var downvotes = meta.downvotes;
var listeners = meta.listeners;
var room = data.room.metadata;
var songPlayer = room.current_dj;
var myID = nconf.get('USERID');
if(songPlayer == myID){
RemLastSongFromQueue();
}
if(pid == -1 || sid == -1) return;
var str = "insert into plays (player,song,upvotes,downvotes,audience) values ("+pid+","+sid+","+upvotes+","+downvotes+","+listeners+")";
connection.query(str);
};
// Fires when a new song starts playing.
OnNewsong = function(data) {
var room = data.room.metadata;
var song = room.current_song;
var songID = song._id;
var songTitle = song.metadata.song;
var songArtist = song.metadata.artist;
var songPlayer = room.current_dj;
var myID = nconf.get('USERID');
if(songPlayer == myID){
AddRandomSong();
}
var str = "insert into songs (name,artist,songid) values (" + connection.escape(songTitle) + "," + connection.escape(songArtist)+ "," + connection.escape(songID) + ") on duplicate key update id=LAST_INSERT_ID(id), lastHeard = now()";
//console.log("Query: "+ str);
connection.query(str, function(err,rows) {
songDBID = rows.insertId;
//console.log('Song: ' + songDBID);
});
bot.getProfile(songPlayer,function(profile) {
var playerName = profile.name;
var str = nconf.get('irctopic');
if(nconf.get('verbose'))
{
var str = nconf.get('ttlink') + ' - \u0002' + songTitle + '\u000F (\u0002' + songArtist + '\u000F) - \u0002' + playerName + '\u000F';
//client.say(nconf.get('ircroom'),str);<br>
client.send('TOPIC', nconf.get('ircroom'), str);
}
connection.query("insert into djs (name,userid) values (" + connection.escape(playerName) + "," + connection.escape(songPlayer)+ ") on duplicate key update id=LAST_INSERT_ID(id), lastHeard = now()", function(err,rows) {
playerDBID = rows.insertId;
//console.log('DJ: ' + playerDBID);
});
});
};
OnRoomChanged = function(data) {
data.room.metadata.djs.forEach(function(dj) {
bot.getProfile(dj, function(profile) {
djList.push(profile);
//console.log(JSON.stringify(djList));
});
});
};
OnRegistered = function(data) {};
OnDeregistered = function(data) {};
OnUpdateVotes = function(data) {};
OnBootedUser = function(data) {};
OnUpdateUser = function(data) {};
OnNewModerator = function(data) {};
OnRemModerator = function(data) {};
OnSnagged = function(data) {};
OnPM = function(data) {};
// DJ Queue Functions
AddToQueue = function(person) {
djQueue.push(person);
//console.log(JSON.stringify(djQueue));
};
RemFromQueue = function(person) {
var newQueue = djQueue;
djQueue.forEach(function(dj, index) {
if(dj.userid == person.userid){
newQueue.splice(index,1);
}
});
djQueue = newQueue;
//console.log(JSON.stringify(djQueue));
};
IsNextDJ = function(userid) {
if(djQueue[0].userid == userid) return true;
else return false;
}
OnSpeak = function(data) {
var userid = data.userid;
var nick = data.name;
var text = data.text;
var isValidCommand = text.match(/^!dj (.+)/i);
if(isValidCommand)
{
var tLower = isValidCommand[1].toLowerCase();
if(tLower == "q+") AddToQueue(data);
else if(tLower == "q-") RemFromQueue(data);
else if(tLower == 'auto')
{
if(nconf.get('autodj'))
{
nconf.set('autodj',false);
str = "AutoDJ Turned OFF!";
bot.remDj();
}
else
{
nconf.set('autodj',true);
str = "AutoDJ Turned ON!";
}
bot.speak(str);
}
else if(tLower == 'begin dj')
{
if(nconf.get('autodj'))
{
var response = "AutoDJ is turned on, sorry. '!dj Auto' to turn it off."
bot.pm(response,userid);
}
else {
bot.addDj();
str = "Starting to DJ!";
}
}
else if(tLower == 'stop dj')
{
if(nconf.get('autodj'))
{
var response = "AutoDJ is turned on, sorry. '!dj Auto' to turn it off."
bot.pm(response,userid);
}
else
{
bot.remDj();
str = "Not gonna DJ no mo'!";
}
}
else {
DoChatCommand(userid, isValidCommand[1], function(userid, response) {
bot.pm(response,userid);
});
}
}
};
// Fires when a PM is sent to the bot on IRC.
OnIRCChat = function(nick,text,message) {
console.log(nick);
console.log(text);
if(nconf.get('authUsers').indexOf(nick) >=0)
{
var isValidCommand = text.match(/^!dj (.+)/i);
if(isValidCommand)
{
DoChatCommand(nick, isValidCommand[1], function(nick, response) {
client.say(nick,response);
});
}
}
};
OnIRCNotice = function(nick, to, text, message) {
console.log(nick + ':' + text);
};