This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
331 lines (302 loc) · 9.44 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
main.js
IRC Bot in node.js
*/
const tmi = require('tmi.js');
const crypto = require('crypto');
const Datastore = require('nedb');
const metadata = require('./package.json');
const conf = require('./config.json') || require('./config.example.json'); // eslint-disable-line
const lib = require('./lib.js');
const db = new Datastore({ filename: 'commands', autoload: true });
const timezoneJS = require('timezone-js');
const tzdata = require('tzdata');
const _tz = timezoneJS.timezone;
_tz.loadingScheme = _tz.loadingSchemes.MANUAL_LOAD;
_tz.loadZoneDataFromObject(tzdata);
let identity;
if (conf.username && conf.password) {
identity = {
username: conf.username,
password: conf.password
};
}
const joinThem = process.argv.slice();
joinThem.splice(0, 2);
const options = {
options: {
debug: true
},
connection: {
reconnect: true
},
identity,
channels: joinThem
};
const client = tmi.client(options);
function _start() {
client.connect().catch(err => {
console.error(`ERROR: ${err.message}`);
setTimeout(() => {
_start();
}, 1000 * 8);
});
}
_start();
const startTime = Date.now();
const { admins } = conf;
// Map a command to a function
const commands = {
'*say': say,
'*ping': ping,
'!pingall': ping,
'*list': list,
'*node': node,
'*gtfo': gtfo,
'*sha512': sha512,
'*math': math,
'*hug': hug,
'*eval': myEval,
'*version': version,
'*cmd': cmd,
'*agdq': agdq,
'*sgdq': agdq,
'*gdq': agdq
};
db.find({}, (err, docs) => {
if (err) {
console.error(err);
}
for (let i = 0; i < docs.length; i++) {
commands[docs[i].trigger] = function (channel) {
try {
sendMessage(channel, eval(`\`${docs[i].command}\``)); // eslint-disable-line
} catch (err2) {
sendMessage(channel, `${err2.message} WutFace`);
}
};
}
});
// Message handler
let messageQ = [];
let curr = 0;
let lastMessage = new Date().getTime();
function _sendMessage(channel, message) {
let padding = '';
if (curr === 1) {
curr = 0;
padding = ' \u206D';
} else {
curr = 1;
}
return client.say(channel, message + padding).catch(err => {
console.error(`ERROR: ${err.message}`);
});
}
setInterval(() => {
if (messageQ.length > 0) {
const x = messageQ.shift();
_sendMessage(x.channel, x.message);
}
if (messageQ.length > 9) {
messageQ = [messageQ[0]];
}
}, ((30 * 999) / 20));
function sendMessage(channel, message, skipQ = false) {
if (new Date().getTime() - lastMessage >= 1000 * 2 || skipQ) {
_sendMessage(channel, message);
} else {
messageQ.push({ channel, message });
messageQ = lib.uniqueObjArray(messageQ);
}
lastMessage = new Date().getTime();
}
client.on('chat', (channel, user, message, self) => {
setTimeout(() => {
if (self) return;
const match = message.match(/(.[a-zA-Z0-9_-]+)(?:\s+(.*))?/);
if (match !== null) {
const command = match[1].toLowerCase();
const args = match[2] ? match[2].trim().split(/\s+/) : [];
if (commands[command]) {
channel = channel.substring(1);
if (admins.includes(user.username)) {
user.admin = true;
} else {
user.admin = false;
}
message = message.replace(/\u{206d}/ug, '');
const args2 = [];
for (let i = 0; i < args.length; i++) {
args2.push(args[i].replace(/\u{206d}/ug, ''));
}
commands[command](channel, user, message, args2);
}
}
}, 10);
});
// Join / Leave
client.on('join', (channel, username, self) => {
if (self && conf.messages.join) {
sendMessage(channel, conf.messages.join);
}
});
function gtfo(channel, user) {
if (user.admin) {
if (conf.messages.leave) {
sendMessage(channel, conf.messages.leave);
}
process.exit(0);
}
}
// Commands as function
function say(channel, user, message, args) {
if (user.admin) {
sendMessage(channel, args.join(' '));
}
}
function ping(channel) {
return sendMessage(channel, `running for ${lib.msToTimeString(Date.now() - startTime)}`);
}
function list(channel, user, message, args) {
if (args[0] && args[0] !== '') {
[channel] = args;
}
client.whisper(user.username, `https://tmi.twitch.tv/group/user/${channel}/chatters`);
}
function node(channel) {
return sendMessage(channel, 'i run on node js FeelsGoodMan');
}
function sha512(channel, user, message, args) {
return sendMessage(channel, crypto.createHash('sha512').update(args.join(' ')).digest('hex'));
}
function math(channel, user, message) {
return sendMessage(channel, `${user.username}, ${lib.mathEval(message)}`);
}
function hug(channel, user, message, args) {
if (args[0]) {
if (/^[a-zA-Z0-9_]{1,25}$/.test(args[0])) {
sendMessage(channel, `${user.username} hugs ${args[0]} <3`);
return;
}
sendMessage(channel, `${user.username}, that is not a valid username ariW`);
}
}
function myEval(channel, user, message, args) {
if (user.admin) {
try {
sendMessage(channel, `${eval(args.join(' '))}`, true); // eslint-disable-line
} catch (e) {
sendMessage(channel, e, true);
}
}
}
function version(channel) {
return sendMessage(channel, `${metadata.name} running on v${metadata.version} ` +
`for ${lib.msToTimeString(Date.now() - startTime)}`);
}
// 30 sec timeout for agdq()
let agdqT = new Date().getTime() - (1000 * 30);
function agdq(channel, user) {
if (agdqT > (new Date().getTime() - (1000 * 30))) {
return;
}
agdqT = new Date().getTime();
lib.getUser.id('gamesdonequick').then(id => {
lib.getUser.data(id).then(data => {
if (data.stream.stream_type === 'live') {
sendMessage(channel, `${user.username}, GDQ is live ${data.stream.game ? `with "${data.stream.game}"` : ''} PagChomp Here is the schedule: https://gamesdonequick.com/schedule`);
return;
}
sendMessage(channel, `${user.username}, GDQ is offline FeelsBadMan`);
}).catch(err => err);
}).catch(err => err);
}
function cmd(channel, user, message, args) {
if (user.admin) {
if (args.length > 1) {
switch (args[0]) {
case 'rm':
case 'remove':
db.remove({ trigger: args[1] }, {}, (err, numRem) => {
if (err) {
sendMessage(channel, `${user.username}, ${err.message} WutFace`, true);
} else if (numRem === 0) {
sendMessage(channel, `${user.username}, no command with trigger "${args[1]}" found`, true);
} else {
delete commands[args[1]];
sendMessage(channel, `${user.username}, successfully removed command "${args[1]}"`, true);
}
});
break;
case 'add':
db.findOne({ trigger: args[1] }, (err, doc) => {
if (err) {
sendMessage(channel, `${user.username}, ${err.message} WutFace`, true);
} else if (doc) {
sendMessage(channel, `${user.username}, command with the trigger "${args[1]}" already exists`, true);
} else {
const tmp1 = args.slice();
tmp1.splice(0, 2);
db.insert({ trigger: args[1], command: tmp1.join(' ') }, err2 => {
if (err2) {
sendMessage(channel, `${user.username}, ${err2.message} WutFace`, true);
} else {
commands[args[1]] = function () {
try {
sendMessage(channel, eval(`\`${tmp1.join(' ')}\``), true); // eslint-disable-line
} catch (err3) {
sendMessage(channel, `${err3.message} WutFace`, true);
}
};
sendMessage(channel, `${user.username}, successfully added command "${args[1]}"`, true);
}
});
}
});
break;
case 'edit':
case 'update':
db.findOne({ trigger: args[1] }, (err, doc) => {
if (err) {
sendMessage(channel, `${user.username}, ${err.message} WutFace`, true);
} else if (!doc) {
sendMessage(channel, `${user.username}, no command with trigger "${args[1]}" found`, true);
} else {
const tmp1 = args.slice();
tmp1.splice(0, 2);
db.update({ trigger: args[1] }, { $set: { command: tmp1.join(' ') } }, {}, err2 => {
if (err2) {
sendMessage(channel, `${user.username}, ${err2.message} WutFace`, true);
} else {
commands[args[1]] = function () {
try {
sendMessage(channel, eval(`\`${tmp1.join(' ')}\``), true); // eslint-disable-line
} catch (err3) {
sendMessage(channel, `${err3.message} WutFace`, true);
}
};
sendMessage(channel, `${user.username}, successfully updated command "${args[1]}"`, true);
}
});
}
});
break;
case 'info':
case 'show':
db.findOne({ trigger: args[1] }, (err, doc) => {
if (err) {
sendMessage(channel, `${user.username}, ${err.message} WutFace`, true);
} else if (!doc) {
sendMessage(channel, `${user.username}, no command with trigger "${args[1]}" found`, true);
} else {
sendMessage(channel, `${user.username}, "${args[1]}": ${doc.command}`, true);
}
});
break;
default:
break;
}
}
}
}