forked from overdrivenpotato/EdmpPlugBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
158 lines (143 loc) · 3.52 KB
/
bot.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
/**
* User: Marko
* Date: 3/8/14
* Time: 9:20 PM
*/
var lastMsg = "";
var skipFixEnabled = false;
window.setInterval(function(){
var message = $(".message:last");
if(message.attr("class") != lastMsg)
{
lastMsg = message.attr("class");
dispatch(message.children(":last").html(), message.children(".from").html().trim());
}
if(skipFixEnabled)
{
skipFix();
}
}, 10);
var lastSkipTime = 0;
function skipFix()
{
if(lastSkipTime < 1)
{
lastSkipTime = Date.now();
return;
}
var times = $("#now-playing-time").children(":last").html().split(":");
if(times[0] < 1 && times[1] < 1)
{
var timeN = Date.now();
if(timeN - lastSkipTime > 5000)
{
log("Skipping due to lag", 1);
commandDispatch("!skip", API.getUser().username);
}
}
}
function dispatch(message, author)
{
message = message.replace(/ /g, '');
if(message.match(/(^!)(!?)/))
{
try
{
var end = message.indexOf(' ');
commandDispatch(message.substr(1, end == -1 ? message.length : end), author);
}
catch(exp)
{
console.log("Error: " + exp);
}
}
}
function isPlaying(username)
{
if(typeof API.getDJ() !== "undefined" && API.getDJ().username == username.trim())
{
return true;
}
return false;
}
function moveToFirst(username) {
API.moderateMoveDJ(getId(username), 1);
}
function commandDispatch(command, author)
{
console.log(author + " has dispatched: \'" + command + "\'");
switch(command.toLowerCase().trim())
{
case "goosesux":
log("Yes he does.", 2);
break;
case "skip":
if(isPlaying(author))
{
skipDj();
break;
}
if(getPermLevel(author) > 1 && typeof API.getDJ() !== "undefined")
{
log(author + " has skipped " + API.getDJ().username, 2);
skipDj();
}
break;
case "skipfix":
skipFixEnabled = !skipFixEnabled;
break;
case "privateskip":
if(isPlaying(author) || getPermLevel(author) >= 2)
{
var current = API.getDJ().username;
log("Skipping " + current + " and repositioning due to private track.", 2);
skipDj();
var processor = setInterval(function(){
if(current != API.getDJ().username)
{
clearInterval(processor);
moveToFirst(current);
}
}, 10);
}
break;
default:
console.log(author + " has entered an invalid command.");
break;
}
}
function skipDj()
{
API.moderateForceSkip();
}
function log(log, level)
{
level = (typeof level === "undefined") ? 3 : level;
if(level < 3)
{
console.log("Chatting: ");
chat(log);
}
console.log(log);
}
function chat(text)
{
$("#chat-input-field").val("/me " + text);
var e = $.Event('keydown');
e.which = 13;
$('#chat-input-field').trigger(e);
}
function getPermLevel(username)
{
return API.getUser(getId(username)).permission;
}
function getId(username) {
var users = API.getUsers();
for(var i = 0; i < users.length; i++)
{
if(users[i].username == username.trim())
{
return users[i].id;
}
}
}