-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
575 lines (477 loc) · 18.3 KB
/
app.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
var version = '12.9.2'
const { KlasaClient, Schema } = require('klasa')
const Discord = require('discord.js')
const MessageEmbed = require('discord.js')
const DiscordAliases = require('discord.js-aliases')
const fs = require('fs')
const config = require('./config.js')
const client = new KlasaClient({
prefix: config.prefix,
providers: {
default: 'rethinkdb'
},
ownerID: '93015586698727424',
fetchAllMembers: false,
commandEditing: true,
typing: true,
readyMessage: (client) => `Successfully initialized. Ready to serve ${client.guilds.size} guilds.`
})
client.config = require('./config.js')
const Idiot = require('idiotic-api')
const delay = require('delay')
const chalk = require('chalk')
const fetch = require('node-fetch')
// const Carina = require('carina').Carina
// const ws = require('ws')
const WebSocket = require('ws');
client.idiotAPI = new Idiot.Client(client.config.idiotKey, {
dev: true
})
const streamerFolder = './streamers'
const streamerFolderMixer = './streamers/mixer'
const streamerFolderTwitch = './streamers/twitch'
const halfHour = 1800000 // 30 min in ms
function configureKlasa() {
// Configure default guild schema for Klasa.
// KlasaClient.defaultGuildSchema.add('mixerLiveChannel', 'TextChannel')
// KlasaClient.defaultGuildSchema.add('twitchLiveChannel', 'TextChannel')
KlasaClient.defaultGuildSchema.add('modLog', 'TextChannel')
KlasaClient.defaultGuildSchema.add('welcomeChannel', 'TextChannel')
// KlasaClient.defaultGuildSchema.add('livePing', 'Boolean', {
// default: true
// })
KlasaClient.defaultGuildSchema.add('defaultRole', 'role')
KlasaClient.defaultGuildSchema.add('muted', 'role')
KlasaClient.defaultGuildSchema.add('mod', 'role')
KlasaClient.defaultClientSchema.add('points', 'float', {
default: 10
})
// Configure default permissions levels for Klasa.
KlasaClient.defaultPermissionLevels
// Mods are lvl5
.add(5, ({
guild,
member
}) => guild && guild.settings.mod != null && member.roles.has(guild.settings.mod))
// Support are lvl8
.add(8, ({
client,
author
}) => client.config.botSupportTeam.includes(author.id), {
fetch: true
})
}
function configureClient() {
// Set the clients version.
client.version = version
// Set bot's activity in Discord.
client.on('ready', () => {
client.user.setActivity(`v${version} | m8bot.js.org`)
})
}
function checkStatus(res) {
if (res.ok) {
return res
} else {
console.log('Status not ok: ' + res);
// nothing
}
}
console.log('Configuring Klasa.')
configureKlasa();
console.log('Klasa configured.')
console.log('Logging in.')
client.login(client.config.token)
console.log('Logged in.')
console.log('Configuring client.')
configureClient();
console.log('Client configured.')
// function loadStreamers() {
// fs.readdir(streamerFolderMixer, (err, files) => {
// if (files === undefined) {
// return;
// }
// var fileCount = files.length
// var allMixer = ''
// for (var i = 0; i < fileCount; i++) {
// var name = files[i].replace('.json', ', ')
// allMixer = allMixer + name
// }
// fs.writeFileSync(streamerFolder + '/mixerStreamers.txt', allMixer.replace('.DS_Store', ''))
// if (err) {
// console.log(err)
// }
// })
// fs.readdir(streamerFolderTwitch, (err, files) => {
// if (files === undefined) {
// return;
// }
// var fileCount = files.length
// var allTwitch = ''
// for (var i = 0; i < fileCount; i++) {
// var name = files[i].replace('.json', ', ')
// allTwitch = allTwitch + name
// }
// fs.writeFileSync(streamerFolder + '/twitchStreamers.txt', allTwitch.replace('.DS_Store', ''))
// if (err) {
// console.log(err)
// }
// })
// }
// function twitchCheck() {
// console.log('Initiating Twitch check.')
// var streamersTwitch = fs.readFileSync(streamerFolder + '/twitchStreamers.txt', 'utf-8').split(', ')
// streamersTwitch.pop()
// var count = 0;
// var streamerBatches = [];
// var streamerBatch = [];
// var announced = [];
// for (var tc = 0; tc < streamersTwitch.length; tc++) {
// var rawdata = fs.readFileSync(streamerFolderTwitch + '/' + streamersTwitch[tc] + '.json')
// var liveTime = (new Date()).getTime();
// var streamerData = JSON.parse(rawdata);
// var lastLiveTime = streamerData.liveTime;
// var timeDiff = liveTime - lastLiveTime;
// if (timeDiff >= halfHour) {
// streamerBatch.push(streamersTwitch[tc]);
// count++;
// }
// if (count == 75) {
// streamerBatches.push(streamerBatch.join(','));
// count = 0;
// streamerBatch = [];
// }
// }
// streamerBatches.push(streamerBatch.join(','));
// var streamerIdBatches = [];
// for (var batch = 0; batch < streamerBatches.length; batch++) {
// var url = `https://api.twitch.tv/kraken/users?login=${streamerBatches[batch]}`;
// var headers = {
// 'Accept': 'application/vnd.twitchtv.v5+json',
// 'Client-Id': client.config.twitch_id,
// }
// fetch(url, { method: 'GET', headers: headers })
// .then(checkStatus)
// .then(res => res.json())
// .then(twitchInfo => {
// var idList = [];
// for (var user = 0; user < twitchInfo.users.length; user++) {
// idList.push(twitchInfo.users[user]._id);
// }
// streamerIdBatches.push(idList.join(','));
// for (var batch = 0; batch < streamerIdBatches.length; batch++) {
// var url = `https://api.twitch.tv/kraken/streams?channel=${streamerIdBatches[batch]}`;
// var headers = {
// 'Accept': 'application/vnd.twitchtv.v5+json',
// 'Client-Id': client.config.twitch_id,
// }
// fetch(url, { method: 'GET', headers: headers })
// .then(checkStatus)
// .then(res => res.json())
// .then(twitchInfo => {
// for (var stream = 0; stream < twitchInfo.streams.length; stream++) {
// var liveTime = (new Date()).getTime()
// var streamStartTime = new Date(twitchInfo.streams[stream].created_at)
// var streamStartMS = streamStartTime.getTime()
// if (liveTime - streamStartMS < 1800000) {
// if (twitchInfo.streams[stream].game === '') {
// var gameName = '[API ERROR]'
// var channelName = twitchInfo.streams[stream].channel.name;
// var args = [
// twitchInfo.streams[stream].channel.name,
// gameName,
// twitchInfo.streams[stream].channel.status,
// twitchInfo.streams[stream].channel.logo,
// twitchInfo.streams[stream].channel.followers,
// twitchInfo.streams[stream].channel.views]
// delay(5000).then(() => {
// if (!announced.includes(channelName)) {
// console.log(`Announcing ${channelName}`);
// client.shard.broadcastEval(`(${liveTwitch}).apply(this, ${JSON.stringify(args)})`)
// announced.push(channelName);
// var streamerFile = fs.readFileSync(streamerFolderTwitch + '/' + channelName + '.json');
// var goLiveTime = (new Date()).getTime();
// var streamerJson = JSON.parse(streamerFile);
// streamerJson.liveTime = goLiveTime;
// fs.writeFileSync(streamerFolderTwitch + '/' + channelName + '.json', JSON.stringify(streamerJson));
// }
// });
// }
// else {
// var channelName = twitchInfo.streams[stream].channel.name;
// var args = [twitchInfo.streams[stream].channel.name,
// twitchInfo.streams[stream].game,
// twitchInfo.streams[stream].channel.status,
// twitchInfo.streams[stream].channel.logo,
// twitchInfo.streams[stream].channel.followers,
// twitchInfo.streams[stream].channel.views]
// delay(5000).then(() => {
// if (!announced.includes(channelName)) {
// console.log(`Announcing ${channelName}`);
// client.shard.broadcastEval(`(${liveTwitch}).apply(this, ${JSON.stringify(args)})`)
// announced.push(channelName);
// var streamerFile = fs.readFileSync(streamerFolderTwitch + '/' + channelName + '.json');
// var goLiveTime = (new Date()).getTime();
// var streamerJson = JSON.parse(streamerFile);
// streamerJson.liveTime = goLiveTime;
// fs.writeFileSync(streamerFolderTwitch + '/' + channelName + '.json', JSON.stringify(streamerJson));
// }
// });
// }
// }
// }
// });
// }
// });
// }
// console.log('Twitch check completed.')
// }
// function liveTwitch(name, game, status, logo, followers, views) {
// const streamerFolderTwitch = './streamers/twitch'
// const Discord = require('discord.js')
// const {
// MessageEmbed
// } = require('discord.js')
// require('discord.js-aliases')
// const fs = require('fs')
// const liveEmbed = new Discord.MessageEmbed() // start the embed message template
// .setTitle(name + "'s Stream")
// .setAuthor(status)
// .setColor(0x9900FF)
// .setDescription('Hey guys, ' + name + ' is live on Twitch right now! Click above to watch!')
// .setFooter('Sent via M8 Bot', 'http://m8bot.js.org/img/profile.png')
// .setThumbnail(logo)
// .setTimestamp()
// .setURL('http://twitch.tv/' + name)
// .addField('Streaming', game)
// .addField('Followers', followers, true)
// .addField('Total Views', views, true) // end the embed message template
// var serversAllowedRaw = fs.readFileSync(streamerFolderTwitch + '/' + name + '.json')
// var streamerData = JSON.parse(serversAllowedRaw)
// var serversAllowed = streamerData.guilds.toString().split(',')
// var i
// for (i = 0; i < serversAllowed.length; i++) { // run for the total number of servers they are allowed on
// if (this.guilds.map(c => c.id).includes(serversAllowed[i])) {
// var guildId = serversAllowed[i]
// if (this.guilds.get(guildId) !== undefined) {
// var gSettings = this.guilds.get(guildId).settings
// if (gSettings.twitchLiveChannel !== undefined) {
// var channelID = gSettings.twitchLiveChannel
// if (channelID == null) {
// channelID = this.guilds.get(guildId).channels.find(channel => channel.name === 'general').id
// if (channelID === undefined) {
// return
// }
// var liveMessage = ''
// if (gSettings.livePing) {
// liveMessage = liveMessage + '@here, '
// }
// liveMessage = liveMessage + name + ' is now live on Twitch!'
// try {
// this.channels.get(channelID).sendEmbed(liveEmbed, liveMessage)
// }
// catch (err) {
// console.log(`Error sending message via twitch: ${err}`)
// }
// } else {
// var liveMessage = ''
// if (gSettings.livePing) {
// liveMessage = liveMessage + '@here, '
// }
// liveMessage = liveMessage + name + ' is now live on Twitch!'
// try {
// this.channels.get(channelID).sendEmbed(liveEmbed, liveMessage)
// }
// catch (err) {
// console.log(`Error sending message via twitch: ${err}`)
// }
// }
// }
// }
// }
// }
// }
// function mixerCheck() {
// for (var i = 0; i < mixerStreamerCount; i++) {
// var halfHour = 1800000 // time in milis that is 30min
// var bootTime = (new Date()).getTime() // get the time the bot booted up
// var halfHourAgo = bootTime - 1800000 // get the time 30min before the boot
// if (streamersMixer[i] == '') {
// continue;
// }
// var mixerID = streamersMixer[i];
// //console.log(`Subscribing to ${mixerID}`);
// // var subscribe = "{\"type\": \"method\", \"method\": \"livesubscribe\", \"params\": {\"events\": [\"channel:" + beamId + ":update\"]}, \"id\": " + beamId + "}";
// var subscribe = {
// "type": "method",
// "method": "livesubscribe",
// "params": {
// "events": [`channel:${mixerID}:update`]
// },
// "id:": mixerID
// };
// ws.send(JSON.stringify(subscribe));
// }
// console.log(chalk.cyan(`Now stalking ${mixerStreamerCount} streamers on Mixer`)) // logs that the bot is watching for the streamer to go live
// }
// function liveMixer(name, game, status, logo, followers, views, level, id) {
// var mixerDir = './streamers/mixer'
// const Discord = require('discord.js')
// const {
// MessageEmbed
// } = require('discord.js')
// require('discord.js-aliases')
// const fs = require('fs')
// const liveEmbed = new Discord.MessageEmbed() // start the embed message template
// .setTitle(name + "'s Stream")
// .setAuthor(status)
// .setColor(0x9900FF)
// .setDescription('Hey guys, ' + name + ' is live on Mixer right now! Click above to watch!')
// .setFooter('Sent via M8 Bot', 'http://m8bot.js.org/img/profile.png')
// .setThumbnail(logo)
// .setTimestamp()
// .setURL('http://mixer.com/' + name)
// .addField('Streaming', game)
// .addField('Followers', followers, true)
// .addField('Mixer Level', level, true)
// .addField('Total Views', views, true) // end the embed message template
// var serversAllowedRaw = fs.readFileSync(mixerDir + '/' + id + '.json')
// var streamerData = JSON.parse(serversAllowedRaw)
// var serversAllowed = streamerData.guilds.toString().split(',')
// var mi
// for (mi = 0; mi < serversAllowed.length; mi++) { // run for the total number of servers they are allowed on
// if (this.guilds.map(c => c.id).includes(serversAllowed[mi])) {
// var guildId = serversAllowed[mi]
// if (this.guilds.get(guildId) !== undefined) {
// var gSettings = this.guilds.get(guildId).settings
// if (gSettings.mixerLiveChannel !== undefined) {
// var channelID = gSettings.mixerLiveChannel
// if (channelID == null) {
// channelID = this.guilds.get(guildId).channels.find(channel => channel.name === 'general').id
// var liveMessage = ''
// if (gSettings.livePing) {
// liveMessage = liveMessage + '@here, '
// }
// liveMessage = liveMessage + name + ' is now live on Mixer!'
// var channel = this.channels.get(channelID);
// if (channel !== null) {
// channel.sendEmbed(liveEmbed, liveMessage) // send the live message to servers
// }
// } else {
// var liveMessage = ''
// if (gSettings.livePing) {
// liveMessage = liveMessage + '@here, '
// }
// liveMessage = liveMessage + name + ' is now live on Mixer!'
// var channel = this.channels.get(channelID);
// if (channel !== null) {
// channel.sendEmbed(liveEmbed, liveMessage) // send the live message to servers
// }
// }
// }
// }
// }
// }
// }
// console.log('Loading streamers into txt from JSON.')
// // Load streamers
// loadStreamers();
// console.log('Streamers loaded into txt files.')
// console.log('Loading Mixer streamers into memory.')
// // Load Mixer streamers.
// var streamersMixer = fs.readFileSync(streamerFolder + '/mixerStreamers.txt', 'utf-8').split(', ')
// streamersMixer.pop()
// var mixerStreamerCount = streamersMixer.length
// console.log('Loaded Mixer streamers into memory.')
// console.log('Loading Twitch streamers into memory.')
// var streamersTwitch = fs.readFileSync(streamerFolder + '/twitchStreamers.txt', 'utf-8').split(', ')
// streamersTwitch.pop()
// var streamerCountTwitch = streamersTwitch.length
// console.log(chalk.magenta(`Now stalking ${streamerCountTwitch} streamers on Twitch!`))
// console.log('Loaded Twitch streamers into memory.')
// console.log('Connecting to Mixer Constellation and other stuffs.')
// const ws = new WebSocket('wss://constellation.mixer.com', {
// headers: {
// "x-is-bot": "true"
// }
// });
// function configureConstellation() {
// ws.on('message', function incoming(data) {
// var messageData = JSON.parse(data);
// if (messageData !== null) {
// if (messageData.event === 'live') {
// if (messageData.data !== null) {
// if (messageData.data.payload.updatedAt !== null) {
// if (messageData.data.payload.online) {
// var channelString = messageData.data.channel.split(':');
// var channelId = channelString[1];
// var url = `https://mixer.com/api/v1/channels/${channelId}`;
// fetch(url)
// .then(checkStatus)
// .then(res => res.json())
// .then(mixerInfo => {
// if (mixerInfo == null) {
// console.log(`Null response from: ${url}`);
// console.log(``);
// return;
// }
// var game = "";
// if (mixerInfo.type == null) {
// game = "A game";
// }
// else {
// game = mixerInfo.type.name;
// }
// var liveTime = (new Date()).getTime() // time the bot sees they went live
// var mixerD = new MixerJSON(channelId)
// var lastLiveTime = mixerD.streamerData.liveTime
// var timeDiff = liveTime - lastLiveTime // gets the diff of current and last live times
// if (timeDiff >= halfHour) { // if its been 30min or more
// var args = [mixerInfo.token, game, mixerInfo.name, mixerInfo.user.avatarUrl, mixerInfo.numFollowers, mixerInfo.viewersTotal, mixerInfo.user.level, mixerInfo.id]
// var v = JSON.stringify(args)
// client.shard.broadcastEval(`(${liveMixer}).apply(this, ${JSON.stringify(args)})`)
// if (mixerInfo.token === mixerD.streamerData.name) {
// mixerD.streamerData.liveTime = liveTime
// fs.writeFileSync(streamerFolderMixer + '/' + channelId + '.json', JSON.stringify(mixerD.streamerData))
// } else {
// mixerD.streamerData.name = mixerInfo.token
// mixerD.streamerData.liveTime = liveTime
// fs.writeFileSync(streamerFolderMixer + '/' + channelId + '.json', JSON.stringify(mixerD.streamerData))
// }
// }
// })
// }
// }
// }
// }
// }
// });
// }
// class MixerJSON {
// constructor(id) {
// var rawdata = fs.readFileSync(streamerFolderMixer + '/' + id + '.json')
// this.streamerData = JSON.parse(rawdata)
// }
// }
// console.log('Connected to Mixer Constellation and did other stuffs.')
// function pingMixer() {
// var ping = {
// "id": 1,
// "type": "method",
// "method": "ping",
// "params": null
// }
// ws.send(JSON.stringify(ping));
// }
// if (client.shard.id === 0) { // only the main/first shard
// configureConstellation();
// setInterval(pingMixer, 10000);
// delay(30000).then(() => {
// mixerCheck()
// })
// // console.log('Waiting 60 seconds, then doing our initial Twitch check.')
// delay(60000).then(() => {
// twitchCheck()
// setInterval(twitchCheck, 120000) // run the check every 2min
// })
// }