-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
630 lines (577 loc) · 19.3 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
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
const Discord = require('discord.js'); // https://discord.js.org/#/
const client = new Discord.Client();
const settings = require('./settings.json');
const sql = require('sqlite');
const createCollage = require("photo-collage");
const sizeOf = require('image-size');
var url = require('url');
var http = require('https');
const miniGalleryChannelId = "236049686820159488";
const generalChannelId = "236042005929656320";
const botChannelId = "344320952991219712";
const pointsRequestChannelId = "344320952991219712";
var scoredb = 0;
var accountsdb = 0;
function set_points(message, user, new_points, current_level) {
let member = message.guild.member(user);
let role1 = message.guild.roles.find("name", "Dip 'N Forget");
let role2 = message.guild.roles.find("name", "Ebay Propainted");
let role3 = message.guild.roles.find("name", "C+C Plz");
let role4 = message.guild.roles.find("name", "JALMM");
let role5 = message.guild.roles.find("name", "Bub For The Bub Glub");
let new_level = current_level;
let old_role = role1;
let new_role = role1;
if (new_points >= 70) {
old_role = role4;
new_role = role5;
new_level = 5;
} else if (new_points >= 40) {
old_role = role3;
new_role = role4;
new_level = 4;
} else if (new_points >= 20) {
old_role = role2;
new_role = role3;
new_level = 3;
} else if (new_points >= 10) {
old_role = role1;
new_role = role2;
new_level = 2;
} else if (new_points >= 5) {
old_role = null;
new_role = role1;
new_level =1;
}
if (current_level != new_level) {
client.channels.get(generalChannelId)
.sendMessage(user +
` :confetti_ball: Congratulations you reached **${new_role.name}** rank! :confetti_ball:`
);
if (old_role !== null) member.removeRole(old_role).catch(console.error);
member.addRole(new_role).catch(console.error);
let tmp = `UPDATE scores SET points = ${new_points}, level = ${new_level} WHERE userId = ${user.id}`
console.log('update scores ',tmp);
scoredb.run(
tmp
);
} else {
let tmp2 = `UPDATE scores SET points = ${new_points} WHERE userId = ${user.id}`;
console.log('update scores ',tmp2)
scoredb.run(
tmp2
);
}
}
// Open the local SQLite database to store account and score information.
Promise.all([
sql.open('./score.sqlite', {
Promise
}),
sql.open('./accounts.sqlite', {
Promise
})
]).then(function ([scoreDB, accountsDB]) {
scoredb = scoreDB;
accountsdb = accountsDB;
});
client.on('ready', () => {
console.log('I\'m Online\nl\'m Online');
});
var commandPrefix = "!"
var waiting_users = [];
client.on('message', message => {
if (message.author.bot)
return;
if (message.channel.type !== 'text')
return;
if (message.channel.id === miniGalleryChannelId) {
let is_link = false;
if (message.attachments.size == 0) {
let message_text = message.content;
if (message_text.startsWith('https://') ||
message_text.startsWith('http://') ||
message_text.startsWith('www')) {
is_link = true;
} else {
message.delete();
return;
}
}
if (is_link) {
client.channels.get(generalChannelId)
.sendMessage(message.author + " : " + message.content);
client.channels.get(pointsRequestChannelId)
.sendMessage(message.author + " : " + message.content);
return;
}
if (waiting_users.includes(message.author.id))
return;
waiting_users.push(message.author.id);
message.channel
.awaitMessages(m => m.attachments.size > 0 &&
m.author.id == message.author.id, {
time: 10e3
})
.then(collected => {
let idx = waiting_users.indexOf(message.author.id);
waiting_users.splice(idx, 1);
let images = [];
for (let attachment of message.attachments.values()) {
images.push(attachment.url);
if (images.length >= 3)
break;
}
for (let msg of collected.values()) {
for (let attachment of msg.attachments.values()) {
images.push(attachment.url);
if (images.length >= 3)
break;
}
if (images.length >= 3)
break;
}
// message.reply('www.collected' + images);
let w = images.length;
if (w > 3) {
w = 3;
}
let h = images.length / w;
var options = url.parse(images[0]);
http.get(options, function (response) {
var chunks = [];
response.on('data', function (chunk) {
chunks.push(chunk);
}).on('end', function () {
var buffer = Buffer.concat(chunks);
const options = {
sources: images,
width: w, // number of images per row
height: 1, // number of images per column
imageWidth: sizeOf(buffer).width / w,
imageHeight: sizeOf(buffer).height / w,
backgroundColor: "#cccccc", // optional, defaults to black.
spacing: 2, // optional: pixels between each image
};
createCollage(options).then((canvas) => {
let buf = canvas.toBuffer();
let genChan = client.channels.get(generalChannelId);
let pointChan = client.channels.get(pointsRequestChannelId);
if (genChan != null) {
genChan.sendFile(buf, 'minigalleryimage.png',
message.author);
}
if (pointChan != null) {
pointChan.sendFile(buf, 'minigalleryimage.png',
message.author);
}
});
});
});
});
return;
}
if (!message.content.startsWith(commandPrefix))
return;
const addPointsCmd = 1;
const resetPointsCmd = 2;
const pointsCmd = 3;
const makersCmd = 4;
const tutorialsCmd = 5;
const leaderboardCmd = 6;
const helpCmd = 7;
const bioEndioCmd = 8;
const redpianoCmd = 9;
const sdubCmd = 10;
const setRAccountCmd = 11;
const redditCmd = 12;
const setAlbumCmd = 13;
const albumCmd = 14;
const resetCmd = 15;
let bot_command = 0;
if (message.content.startsWith(commandPrefix + 'addpoints')) {
bot_command = addPointsCmd;
} else if (message.content.startsWith(commandPrefix + 'resetpoints')) {
bot_command = resetPointsCmd;
} else if (message.content.startsWith(commandPrefix + 'points')) {
bot_command = pointsCmd;
} else if (message.content.startsWith(commandPrefix + 'makers')) {
bot_command = makersCmd;
} else if (message.content.startsWith(commandPrefix + 'tutorials')) {
bot_command = tutorialsCmd;
} else if (message.content.startsWith(commandPrefix + 'leaderboard')) {
bot_command = leaderboardCmd;
} else if (message.content.startsWith(commandPrefix + 'help')) {
bot_command = helpCmd;
} else if (message.content.startsWith(commandPrefix + 'bio_endio')) {
bot_command = bioEndioCmd;
} else if (message.content.startsWith(commandPrefix + 'redpiano')) {
bot_command = redpianoCmd;
} else if (message.content.startsWith(commandPrefix + 'sdub')) {
bot_command = sdubCmd;
} else if (message.content.startsWith(commandPrefix + 'setredditaccount')) {
bot_command = setRAccountCmd;
} else if (message.content.startsWith(commandPrefix + 'reddit')) {
bot_command = redditCmd;
} else if (message.content.startsWith(commandPrefix + 'setalbum')) {
bot_command = setAlbumCmd;
} else if (message.content.startsWith(commandPrefix + 'album')) {
bot_command = albumCmd;
} else if (message.content.startsWith(commandPrefix + 'reset')) {
bot_command = resetCmd;
}
let pointschannel = (message.channel.id === pointsRequestChannelId);
let botchannel = (message.channel.id === botChannelId);
let generalchannel = (message.channel.id === generalChannelId);
let ignoreMessage = true;
if (generalchannel && (bot_command == bioEndioCmd || bot_command == redpianoCmd || bot_command == albumCmd)) {
ignoreMessage = false;
}
if (botchannel) {
ignoreMessage = false;
}
if (pointschannel
&& (bot_command == addPointsCmd || bot_command == resetPointsCmd || bot_command == pointsCmd)) {
ignoreMessage = false;
}
if (ignoreMessage)
return;
if (bot_command == addPointsCmd) {
console.log('addPoints:' + message.mentions.users);
var user = message.mentions.users.first();
var number = 0;
var index = message.content.lastIndexOf(" ");
if (index !== -1) {
number = Number(message.content.substring(index + 1));
}
let adminRole = message.guild.roles.find("name", "Admin");
let modRole = message.guild.roles.find("name", "Moderators");
// Only allow Admin and Moderators to add points.
if (!message.member.roles.has(adminRole.id) &&
!message.member.roles.has(modRole.id) &&
message.author.id != '134744140318638080') {
message.reply(
`:japanese_goblin: Haha! Being sneaky are we? :japanese_goblin: `
);
return;
}
let new_points = number;
let current_level = 0;
let new_level = 0;
scoredb.get(`SELECT * FROM scores WHERE userId ='${user.id}'`)
.then(row => {
if (!row) {
scoredb.run(
'INSERT INTO scores (userId, points, level) VALUES (?, ?, ?)', [
user.id, 0, 0
]);
} else {
current_level = row.level;
new_points += row.points;
new_level = current_level;
}
console.log('addpoints: ' + user + ' ' + user.id + ' ' + new_points + ' ' + current_level);
set_points(message, user, new_points, current_level);
scoredb.get(`SELECT * FROM scores WHERE userId ='${user.id}'`).then(
row => {
message.reply(user + ` has ${row.points} points`);
});
})
.catch(() => {
console.error;
scoredb
.run(
'CREATE TABLE IF NOT EXISTS scores (userId TEXT, points INTEGER, level INTEGER)'
)
.then(() => {
scoredb.run(
'INSERT INTO scores (userId, points, level) VALUES (?, ?, ?)', [
user.id, 0, 0
]);
set_points(message, user, new_points, current_level);
scoredb.get(
`SELECT * FROM scores WHERE userId ='${user.id}'`).then(
row => {
message.reply(user + ` has ${row.points} points`);
});
});
});
return;
} else if (bot_command == resetPointsCmd) {
var user = message.mentions.users.first();
var number = 0;
let myRole1 = message.guild.roles.find("name", "Admin");
if (!message.member.roles.has(myRole1.id)) {
message.reply(
`:japanese_goblin: Haha! Being sneaky are we? :japanese_goblin: `
);
return;
}
let member = message.guild.member(user);
let myRole2 = message.guild.roles.find("name", "Dip 'N Forget");
let myRole3 = message.guild.roles.find("name", "Ebay Propainted");
let myRole4 = message.guild.roles.find("name", "C+C Plz");
let myRole5 = message.guild.roles.find("name", "JALMM");
let myRole6 = message.guild.roles.find("name", "Bub For The Bub Glub");
if (member.roles.has(myRole2.id)) {
member.removeRole(myRole2).catch(console.error);
}
if (member.roles.has(myRole3.id)) {
member.removeRole(myRole3).catch(console.error);
}
if (member.roles.has(myRole4.id)) {
member.removeRole(myRole4).catch(console.error);
}
if (member.roles.has(myRole5.id)) {
member.removeRole(myRole5).catch(console.error);
}
if (member.roles.has(myRole6.id)) {
member.removeRole(myRole6).catch(console.error);
}
scoredb.get(`SELECT * FROM scores WHERE userId ='${user.id}'`).then(row => {
if (!row) {
scoredb.run(
'INSERT INTO scores (userId, points, level) VALUES (?, ?, ?)', [
user.id, number, 0
]);
} else {
scoredb.run(
`UPDATE scores SET points = ${number}, level = ${number} WHERE userId = ${user.id}`
);
}
console.log("points-reset!");
message.reply(user + ` reset to 0 points`);
return;
});
return;
} else if (bot_command == pointsCmd) {
var user = message.author;
if (message.mentions.users.size > 0) {
user = message.mentions.users.first();
}
scoredb.get(`SELECT * FROM scores WHERE userId ='${user.id}'`).then(row => {
if (!row) {
message.reply(user + ` has 0 points`);
return;
}
message.reply(user + ` has ${row.points} points`);
return;
});
return;
} else if (bot_command == makersCmd) {
message.reply(
`Here's a list of all manufacturers: https://www.reddit.com/r/minipainting/wiki/manufacturers`
);
return;
} else if (bot_command == tutorialsCmd) {
message.reply(
`Here's a compilation of useful guides: https://www.reddit.com/r/minipainting/wiki/tutorials`
);
return;
} else if (bot_command == leaderboardCmd) {
scoredb.all(`SELECT * FROM scores ORDER BY points DESC LIMIT 10`).then(results => {
if (results) {
let userPromises = [];
for (let i = 0; i < results.length; i++) {
userPromises.push(client.fetchUser(results[i].userId));
}
Promise.all(userPromises).then(users => {
let msg = '```';
for( let i = 0; i < results.length; i++) {
msg += `${results[i].points} - ${users[i].username} \n`;
}
msg += '```';
message.reply(msg);
});
} else {
message.reply("Oops, something went wrong!");
}
});
return;
} else if (bot_command == helpCmd) {
message.reply(
`**COMMAND LIST**\n
"!points [user]": Check a user's points!
"!leaderboard": Check the points leaderboard!
"!makers": You can find all mini related manufactores!
"!tutorials": You can find useful guide lists!
"!setredditaccount [username]": You can link your reddit account!
"!reddit [user]": Get a user's linked reddit account!
"!setalbum [link]": You can link an album of your paintings!
"!album [user]": Get a user's linked album!`
);
return;
} else if (bot_command == bioEndioCmd) {
message.reply(
`
C O N T R A S T
O
N
T
R
A
S
T`
);
return;
} else if (bot_command == redpianoCmd) {
message.reply(`O I L`);
return;
} else if (bot_command == sdubCmd) {
message.reply(`https://www.youtube.com/user/SDubist`);
return;
} else if (bot_command == setRAccountCmd) {
let author = message.author;
let index = message.content.lastIndexOf(" ");
let redditAccount = "";
if (index == -1) {
message.reply('Usage: !setredditaccount [username]').then(msg => {
msg.delete(7000);
});
message.delete();
return;
}
redditAccount = message.content.substring(index + 1);
if (redditAccount.startsWith('https://')) {
redditAccount = redditAccount.replace('https://', '');
}
if (redditAccount.startsWith('http://')) {
redditAccount = redditAccount.replace('http://', '');
}
if (redditAccount.startsWith('www.reddit.com')) {
redditAccount = redditAccount.replace('www.reddit.com', '');
}
if (redditAccount.startsWith('/user/')) {
redditAccount = redditAccount.replace('/user/', '');
}
accountsdb.get(`SELECT * FROM accounts WHERE userId ='${author.id}'`)
.then(row => {
if (!row) {
accountsdb.run(
'INSERT INTO accounts (userId, account, album) VALUES (?, ?, ?)', [
author.id, redditAccount, ""
]);
} else {
accountsdb.run(
`UPDATE accounts SET account = "${
redditAccount
}" WHERE userId = ${
author.id
}`
);
}
console.log("set reddit account!");
})
.catch(() => {
console.error;
accountsdb
.run(
'CREATE TABLE IF NOT EXISTS accounts (userId TEXT, account TEXT, album TEXT)'
)
.then(() => {
accountsdb.run(
'INSERT INTO accounts (userId, account, album) VALUES (?, ?, ?)', [
author.id, redditAccount, ""
]);
});
});
message.reply(`Successfully linked to https://www.reddit.com/u/` +
redditAccount);
return;
} else if (bot_command == redditCmd) {
try {
var user = message.author;
if (message.mentions.users.size > 0) {
user = message.mentions.users.first();
}
accountsdb.get(`SELECT * FROM accounts WHERE userId ='${user.id}'`)
.then(row => {
if (!row || row.account == "") {
message.reply(user +
` does not have a linked Reddit account.`);
return;
} else {
message.reply(`Reddit Account for ` + user +
`: https://www.reddit.com/user/` + row.account);
return;
}
});
} catch (e) {
message.reply('Invalid user');
return;
}
} else if (bot_command == setAlbumCmd) {
let author = message.author;
let index = message.content.lastIndexOf(" ");
let album = "";
if (index == -1) {
message.reply('Usage: !setalbum [link]');
return;
}
album = message.content.substring(index + 1);
if (!album.startsWith('http://') && !album.startsWith('https://')) {
album = 'http://' + album;
}
accountsdb.get(`SELECT * FROM accounts WHERE userId ='${author.id}'`)
.then(row => {
if (!row) {
accountsdb.run(
'INSERT INTO accounts (userId, account, album) VALUES (?, ?, ?)', [
author.id, "", album
]);
} else {
accountsdb.run(
`UPDATE accounts SET album = "${
album
}" WHERE userId = ${author.id}`
);
}
console.log("set album!");
})
.catch(() => {
console.error;
accountsdb
.run(
'CREATE TABLE IF NOT EXISTS accounts (userId TEXT, account TEXT, album TEXT)'
)
.then(() => {
accountsdb.run(
'INSERT INTO accounts (userId, account, album) VALUES (?, ?, ?)', [
author.id, "", album
]);
});
});
message.reply(`Successfully set album to ` + album);
} else if (bot_command == albumCmd) {
try {
var user = message.author;
if (message.mentions.users.size > 0) {
user = message.mentions.users.first();
}
accountsdb.get(`SELECT * FROM accounts WHERE userId ='${user.id}'`)
.then(row => {
if (!row || row.album == "") {
message.reply(user + ` does not have a linked album.`);
return;
} else {
message.reply(`Album for ` + user + `: ` + row.album);
return;
}
});
} catch (e) {
message.reply('Invalid user');
return;
}
} else if (message.content.startsWith(commandPrefix + "reset")) {
let myRole1 = message.guild.roles.find("name", "Admin");
if (!message.member.roles.has(myRole1.id) &&
message.author.id != '134744140318638080') {
return;
}
message.reply(`Coming back!`);
setTimeout(() => {
process.exit();
}, 1000);
}
});
client.login(settings.token);