-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
259 lines (210 loc) · 7.08 KB
/
index.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
const ttapi = require('ttapi');
const Storage = require('node-storage');
const store = new Storage('data');
const cors = require('cors');
const express = require('express')
const app = express();
const pug = require('pug');
require('dotenv').config({ path: './src/config/.env' });
app.use(`/styles`, express.static('./src/styles'));
app.use(`/scripts`, express.static('./src/scripts'));
app.use(`/modules`, express.static('./node_modules'));
app.use(express.json());
app.use(cors({
origin: 'http://localhost:3000'
}));
const bot = {
API: new ttapi(process.env.BOT_AUTH, process.env.BOT_USERID),
Chat: require('./src/chat.js'),
ContentFilters: require('./src/contentfilters.js'),
DJQueue: require('./src/djqueue.js'),
PrivateMessage: require('./src/privatemessage.js'),
Room: require('./src/room.js'),
Song: require('./src/song.js'),
User: require('./src/user.js'),
Vote: require('./src/vote.js'),
}
let roomName = '';
const login = () => {
try {
bot.API.roomRegister(process.env.BOT_ROOMID, async () => {
bot.API.setAsBot();
const roomInfo = await bot.Room.getInfo(bot);
roomName = roomInfo.room.name;
});
}
catch(err) {
console.log(err);
login();
}
}
login();
var checkIfConnected = function ()
{
if (!bot.API._isAuthenticated) {
login();
}
};
setInterval(checkIfConnected, 10000);
bot.API.on('disconnected', async (data) => {
});
bot.API.on('pmmed', async (data) => {
bot.PrivateMessage.onNewPM(bot, data);
});
bot.API.on('add_dj', async (data) => {
let nextDJ = await bot.DJQueue.getNextDJ();
// Check the new DJ is the next in the Queue
if (nextDJ) {
if (nextDJ.userid !== data.user[0].userid) {
bot.Chat.say(bot, `@${data.user[0].name} you are not next in queue.` );
bot.API.remDj(data.user[0].userid);
return;
}
}
// Remove new DJ from the queue
let removeMessage = await bot.DJQueue.remove(data.user[0]);
if (removeMessage) {
bot.Chat.say(bot, removeMessage);
}
});
bot.API.on('rem_dj', async (data) => {
// Check is there is anyone in the queue
let nextDJ = await bot.DJQueue.getNextDJ();
if (nextDJ) {
Chat.say(bot, await bot.DJQueue.next() );
}
});
// Actions when a user posts in chat
bot.API.on('speak', async (data) => {
if (data.text.match(/^UNBAN/)) {
bot.API.getUserId(data.name, function (userdata) {
if (!userdata.userid) {
return;
}
let bannedList = store.get('bannedlist') ? store.get('bannedlist') : [];
bot.API.boot(userdata.userid, `You've been banned from this room.`);
if (bannedList.includes(userdata.userid)) {
return;
}
bannedList.push(userdata.userid);
store.put('bannedlist', bannedList);
});
}
if (data.text.match(/𝕘𝕣𝟠𝟜𝕦/)) {
bot.API.getUserId(data.name, function (userdata) {
if (!userdata.userid) {
return;
}
let bannedList = store.get('bannedlist') ? store.get('bannedlist') : [];
bot.API.boot(userdata.userid, `You've been banned from this room.`);
if (bannedList.includes(userdata.userid)) {
return;
}
bannedList.push(userdata.userid);
store.put('bannedlist', bannedList);
});
}
if (data.text.match(/gr84u/)) {
bot.API.getUserId(data.name, function (userdata) {
if (!userdata.userid) {
return;
}
let bannedList = store.get('bannedlist') ? store.get('bannedlist') : [];
bot.API.boot(userdata.userid, `You've been banned from this room.`);
if (bannedList.includes(userdata.userid)) {
return;
}
bannedList.push(userdata.userid);
store.put('bannedlist', bannedList);
});
}
bot.Chat.onNewMessage(bot, data);
let isMod = await bot.User.isMod(bot, data.userid);
// Someone is calling for a ban
if (data.text.match(/^\/ban (.+)/)) {
if (!isMod) {
bot.API.speak(`Only mods can ban users.`);
return;
}
let usertoban = data.text.match(/^\/ban (.+)/)[1];
bot.API.getUserId(usertoban, function (userdata) {
if (!userdata.userid) {
return;
}
let bannedList = store.get('bannedlist') ? store.get('bannedlist') : [];
bot.API.boot(userdata.userid, `You've been banned from this room.`);
if (bannedList.includes(userdata.userid)) {
return;
}
bannedList.push(userdata.userid);
store.put('bannedlist', bannedList);
});
}
});
// Actions when a new song plays
bot.API.on('newsong', (data) => {
bot.Song.onNewSong(bot, data);
});
// Actions when new users enter room
bot.API.on('registered', (registereddata) => {
// Check if user is on banned list
// If so, boot 'em
bot.API.getUserId(registereddata.user[0].name, function (userdata) {
//Lookup failed
if (!userdata.userid) {
return;
}
// Ignore yourself
if (userdata.userid === process.env.BOT_USERID) {
return;
}
let bannedList = store.get('bannedlist') ? store.get('bannedlist') : [];
if (bannedList.includes(userdata.userid)) {
bot.API.boot(userdata.userid, `You've been banned from this room.`);
}
if (registereddata.user[0].name.substring(0,3) === 'III') {
bot.API.boot(userdata.userid, `You've been banned from this room.`);
}
});
// Greet incoming users
/*
sleep(1000).then(() => {
bot.API.speak(`${registereddata.user[0].name}, Welcome to ${roomName}.`);
});
*/
});
app.get('/', function (req, res) {
bot.API.playlistAll( (playlistData) => {
let html = pug.renderFile('./src/templates/index.pug', {playlistData: playlistData.list});
res.send(html);
});
});
app.post('/songstatus', async function (req, res) {
const Youtube = require('./src/lib/youtube');
let videoStatus = await Youtube.checkVideoStatus(req.body.videoIDs)
res.send(videoStatus);
});
app.get('/queue.json', function (req, res) {
bot.API.playlistAll( (playlistData) => {
res.json(playlistData);
});
});
app.post('/movesong', (req, res) => {
bot.API.playlistReorder(Number.parseInt(req.body.indexFrom), Number.parseInt(req.body.indexTo));
res.json(`refresh`);
});
app.get('/findsong', (req, res) => {
bot.API.searchSong(req.query.term, (data) => {
let html = pug.renderFile('./src/templates/search.pug', {playlistData: data.docs});
res.send(html);
});
});
app.get('/addsong', (req, res) => {
bot.API.playlistAdd(req.query.songid);
res.json(`refresh`);
});
app.get('/deletesong', (req, res) => {
bot.API.playlistRemove(Number.parseInt(req.query.songindex));
res.json(`refresh`);
});
app.listen(8585)