forked from blargbot/blargbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord.js
1085 lines (998 loc) · 31.6 KB
/
discord.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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const fs = require('fs');
const util = require('util');
const Eris = require('eris');
const moment = require('moment-timezone');
const path = require('path');
const https = require('https');
var bu;
var tags = require('./tags.js');
const reload = require('require-reload')(require);
const request = require('request');
const Promise = require('promise');
var webInterface = require('./interface.js');
const async = require('asyncawait/async');
const await = require('asyncawait/await');
const Cleverbot = require('cleverbot-node');
var cleverbot = new Cleverbot();
var e = module.exports = {}
, avatars
, vars
, config
, emitter
, bot
, VERSION;
e.requireCtx = require;
/**
* Initializes every command found in the dcommands directory
* - hooray for modules!
*/
function initCommands() {
var fileArray = fs.readdirSync(path.join(__dirname, 'dcommands'));
for (var i = 0; i < fileArray.length; i++) {
var commandFile = fileArray[i];
if (/.+\.js$/.test(commandFile)) {
var commandName = commandFile.match(/(.+)\.js$/)[1];
loadCommand(commandName);
bu.logger.init(`${i < 10 ? ' ' : ''}${i}.`, 'Loading command module '
, commandName);
} else {
bu.logger.init(' Skipping non-command ', commandFile);
}
}
}
/**
* Reloads a specific command
* @param commandName - the name of the command to reload (String)
*/
function reloadCommand(commandName) {
if (bu.commands[commandName]) {
bu.logger.init(`${1 < 10 ? ' ' : ''}${1}.`, 'Reloading command module '
, commandName);
if (bu.commands[commandName].shutdown)
bu.commands[commandName].shutdown();
bu.commands[commandName] = reload(`./dcommands/${commandName}.js`);
buildCommand(commandName);
}
}
/**
* Unloads a specific command
* @param commandName - the name of the command to unload (String)
*/
function unloadCommand(commandName) {
if (bu.commands[commandName]) {
bu.logger.init(`${1 < 10 ? ' ' : ''}${1}.`, 'Unloading command module '
, commandName);
if (bu.commands[commandName].sub) {
for (var subCommand in bu.commands[commandName].sub) {
bu.logger.init(` Unloading ${commandName}'s subcommand`
, subCommand);
delete bu.commandList[subCommand];
}
}
delete bu.commandList[commandName];
if (bu.commands[commandName].alias) {
for (var ii = 0; ii < bu.commands[commandName].alias.length; ii++) {
bu.logger.init(` Unloading ${commandName}'s alias`
, bu.commands[commandName].alias[ii]);
delete bu.commandList[bu.commands[commandName].alias[ii]];
}
}
}
}
/**
* Loads a specific command
* @param commandName - the name of the command to load (String)
*/
function loadCommand(commandName) {
bu.commands[commandName] = require(`./dcommands/${commandName}.js`);
if (bu.commands[commandName].isCommand) {
buildCommand(commandName);
} else {
bu.logger.init(' Skipping non-command ', commandName + '.js');
}
}
// Refactored a major part of loadCommand and reloadCommand into this
function buildCommand(commandName) {
bu.commands[commandName].init(bot, bu);
var command = {
name: commandName,
usage: bu.commands[commandName].usage,
info: bu.commands[commandName].info,
hidden: bu.commands[commandName].hidden,
category: bu.commands[commandName].category
};
if (bu.commands[commandName].longinfo) {
bu.r.table('command').insert({
name: commandName,
usage: command.usage.replace(/</g, '<').replace(/>/g, '>'),
info: bu.commands[commandName].longinfo,
type: command.category
}).run();
}
if (bu.commands[commandName].sub) {
for (var subCommand in bu.commands[commandName].sub) {
bu.logger.init(` Loading ${commandName}'s subcommand`, subCommand);
bu.commandList[subCommand] = {
name: commandName,
usage: bu.commands[commandName].sub[subCommand].usage,
info: bu.commands[commandName].sub[subCommand].info,
hidden: bu.commands[commandName].hidden,
category: bu.commands[commandName].category
};
}
}
bu.commandList[commandName] = command;
if (bu.commands[commandName].alias) {
for (var ii = 0; ii < bu.commands[commandName].alias.length; ii++) {
bu.logger.init(` Loading ${commandName}'s alias`
, bu.commands[commandName].alias[ii]);
bu.commandList[bu.commands[commandName].alias[ii]] = command;
}
}
}
var debug = false;
var warn = true;
var error = true;
/**
* Initializes the bot
* @param v - the version number (String)
* @param topConfig - the config file (Object)
* @param em - the event emitter (EventEmitter)
*/
e.init = (blargutil, v, em) => {
bu = blargutil;
VERSION = v;
emitter = em;
config = bu.config;
bu.logger.debug('HELLOOOOO?');
if (fs.existsSync(path.join(__dirname, 'vars.json'))) {
var varsFile = fs.readFileSync(path.join(__dirname, 'vars.json')
, 'utf8');
vars = JSON.parse(varsFile);
} else {
vars = {};
saveVars();
}
e.bot = bot = new Eris.Client(config.discord.token, {
autoReconnect: true,
disableEveryone: true,
disableEvents: {
//PRESENCE_UPDATE: true,
// VOICE_STATE_UPDATE: true,
TYPING_START: true
},
getAllUsers: true
});
bu.init(bot);
bu.bot = bot;
bu.config = config;
bu.emitter = em;
bu.VERSION = v;
bu.startTime = startTime;
bu.vars = vars;
tags.init(bot, bu);
webInterface.init(bot, bu);
/**
* EventEmitter stuff
*/
emitter.on('reloadInterface', () => {
reloadInterface();
});
emitter.on('discordMessage', (message, attachment) => {
if (attachment)
bu.sendMessageToDiscord(config.discord.channel
, message
, attachment);
else
bu.sendMessageToDiscord(config.discord.channel, message);
});
emitter.on('discordTopic', (topic) => {
bot.editChannel(config.discord.channel, {
topic: topic
});
});
emitter.on('eval', (msg, text) => {
eval1(msg, text);
});
emitter.on('eval2', (msg, text) => {
eval2(msg, text);
});
emitter.on('reloadCommand', (commandName) => {
reloadCommand(commandName);
});
emitter.on('loadCommand', (commandName) => {
loadCommand(commandName);
});
emitter.on('unloadCommand', (commandName) => {
unloadCommand(commandName);
});
emitter.on('saveVars', () => {
saveVars();
});
avatars = JSON.parse(fs.readFileSync(path.join(__dirname
, `avatars${config.general.isbeta ? '' : 2}.json`), 'utf8'));
e.bot = bot;
bot.on('debug', function (message, id) {
if (debug)
bu.logger.debug(`[${moment()
.format(`MM/DD HH:mm:ss`)}][DEBUG][${id}] ${message}`);
return 'no';
});
bot.on('warn', function (message, id) {
if (warn)
bu.logger.warn(`[${moment()
.format(`MM/DD HH:mm:ss`)}][WARN][${id}] ${message}`);
});
bot.on('error', function (err, id) {
if (error)
bu.logger.error(`[${moment()
.format(`MM/DD HH:mm:ss`)}][ERROR][${id}] ${err.stack}`);
});
bot.on('ready', async(() => {
bu.logger.init('Ready!');
let guilds = await(bu.r.table('guild').withFields('guildid').run()).map(g => g.guildid);
//console.dir(guilds);
bot.guilds.forEach((g) => {
if (guilds.indexOf(g.id) == -1) {
console.log('Inserting a missing guild');
bu.r.table('guild').insert({
guildid: g.id,
active: true,
name: g.name,
settings: {},
channels: {},
commandperms: {},
ccommands: {},
modlog: []
}).run();
}
});
gameId = bu.getRandomInt(0, 4);
if (config.general.isbeta)
avatarId = 4;
else
avatarId = 0;
switchGame();
switchAvatar();
postStats();
}));
bot.on('guildMemberAdd', async((guild, member) => {
let val = await(bu.guildSettings.get(guild.id, 'greeting'))
if (val) {
var message = await(tags.processTag({
channel: guild.defaultChannel,
author: member.user,
member: member
}, val, ''));
bu.sendMessageToDiscord(guild.defaultChannel.id, message);
}
}));
bot.on('guildMemberRemove', async((guild, member) => {
let val = await(bu.guildSettings.get(guild.id, 'farewell'))
if (val) {
var message = await(tags.processTag({
channel: guild.defaultChannel,
author: member.user,
member: member
}, val, ''));
bu.sendMessageToDiscord(guild.defaultChannel.id, message);
}
}));
bot.on('guildMemberRemove', async((guild, member) => {
if (member.id === bot.user.id) {
postStats();
bu.logger.debug('removed from guild');
bu.sendMessageToDiscord(`205153826162868225`
, `I was removed from the guild \`${guild
.name}\` (\`${guild.id}\`)!`);
bu.r.table('guild').get(guild.id).update({
active: false
}).run();
let channel = await(bot.getDMChannel(guild.ownerID));
bu.sendMessageToDiscord(channel.id, `Hi!
I see I was removed from your guild **${guild.name}**, and I'm sorry I wasn't able to live up to your expectations.
If it's too much trouble, could you please tell me why you decided to remove me, what you didn't like about me, or what you think could be improved? It would be very helpful.
You can do this by typing \`suggest <suggestion>\` right in this DM. Thank you for your time!`);
}
}));
bot.on('guildCreate', async((guild) => {
postStats();
bu.logger.debug('added to guild');
let storedGuild = await(bu.r.table('guild').get(guild.id).run());
if (!storedGuild || !storedGuild.active) {
var message = `I was added to the guild \`${guild.name}\``
+ ` (\`${guild.id}\`)!`;
bu.sendMessageToDiscord(`205153826162868225`, message);
if (bot.guilds.size % 100 == 0) {
bu.sendMessageToDiscord(`205153826162868225`, `🎉 I'm now `
+ `in ${bot.guilds.size} guilds! 🎉`);
}
var message2 = `Hi! My name is blargbot, a multifunctional discord bot here to serve you!
- 💻 For command information, please do \`${bu.config.discord.defaultPrefix}help\`!
- 📢 For Bot Commander commands, please make sure you have a role titled \`Bot Commander\`.
- 🛠 For Admin commands, please make sure you have a role titled \`Admin\`.
If you are the owner of this server, here are a few things to know.
- 🗨 To enable modlogging, please create a channel for me to log in and do \`${bu.config.discord.defaultPrefix}modlog\`
- 🙈 To mark channels as NSFW, please go to them and do \`${bu.config.discord.defaultPrefix}nsfw\`.
- ❗ To change my command prefix, please do \`${bu.config.discord.defaultPrefix}setprefix <anything>\`.
❓ If you have any questions, comments, or concerns, please do \`${bu.config.discord.defaultPrefix}suggest <suggestion>\`. Thanks!
👍 I hope you enjoy my services! 👍`;
bu.sendMessageToDiscord(guild.id, message2);
if (!storedGuild)
bu.r.table('guild').insert({
guildid: guild.id,
active: true,
name: guild.name,
settings: {},
channels: {},
commandperms: {},
ccommands: {},
modlog: []
}).run();
else
bu.r.table('guild').get(guild.id).update({
active: true
}).run();
}
}));
bot.on('messageUpdate', async((msg, oldmsg) => {
if (oldmsg) {
if (msg.content == oldmsg.content) {
return;
}
if (msg.author.id == bot.user.id) {
bu.logger.output(`Message ${msg.id} was updated to '${msg.content}''`);
}
if (msg.channel.id != '204404225914961920') {
var nsfw = await(bu.isNsfwChannel(msg.channel.id));
bu.r.table('chatlogs').insert({
content: msg.content,
attachment: msg.attachments[0] ? msg.attachments[0].url : null,
userid: msg.author.id,
msgid: msg.id,
channelid: msg.channel.id,
guildid: msg.channel.guild.id,
msgtime: bu.r.epochTime(moment(msg.editedTimestamp) / 1000),
nsfw: nsfw,
mentions: msg.mentions.map(u => u.username).join(','),
type: 1
}).run();
}
}
}));
bot.on('guildBanAdd', (guild, user) => {
var mod;
var type = 'Ban';
var reason;
if (!bu.bans[guild.id])
bu.bans[guild.id] = {};
if (bu.bans[guild.id].mass && bu.bans[guild.id].mass.users && bu.bans[guild.id].mass.users.indexOf(user.id) > -1) {
bu.bans[guild.id].mass.newUsers.push(user);
bu.bans[guild.id].mass.users.splice(bu.bans[guild.id].mass.users.indexOf(user.id), 1);
if (bu.bans[guild.id].mass.users.length == 0) {
mod = bu.bans[guild.id].mass.mod;
type = bu.bans[guild.id].mass.type;
reason = bu.bans[guild.id].mass.reason;
bu.logAction(guild, bu.bans[guild.id].mass.newUsers, mod, type, reason);
}
return;
} else if (bu.bans[guild.id][user.id]) {
mod = bu.bans[guild.id][user.id].mod;
type = bu.bans[guild.id][user.id].type;
reason = bu.bans[guild.id][user.id].reason;
delete bu.bans[guild.id][user.id];
}
bu.logAction(guild, user, mod, type, reason);
});
bot.on('guildBanRemove', (guild, user) => {
var mod;
if (bu.unbans[guild.id] && bu.unbans[guild.id][user.id]) {
mod = bot.users.get(bu.unbans[guild.id][user.id]);
delete bu.unbans[guild.id][user.id];
}
bu.logAction(guild, user, mod, 'Unban');
});
bot.on('messageDelete', async((msg) => {
if (commandMessages[msg.channel.guild.id] && commandMessages[msg.channel.guild.id].indexOf(msg.id) > -1) {
let val = await(bu.guildSettings.get(msg.channel.guild.id, 'deletenotif'));
if (val && val != 0)
bu.sendMessageToDiscord(msg.channel.id, `**${msg.member.nick
|| msg.author.username}** deleted their command message.`);
commandMessages[msg.channel.guild.id].splice(commandMessages[msg.channel.guild.id].indexOf(msg.id), 1);
}
if (msg.channel.id != '204404225914961920') {
var nsfw = await(bu.isNsfwChannel(msg.channel.id));
bu.r.table('chatlogs').insert({
content: msg.content,
attachment: msg.attachments[0] ? msg.attachments[0].url : null,
userid: msg.author.id,
msgid: msg.id,
channelid: msg.channel.id,
guildid: msg.channel.guild.id,
msgtime: bu.r.epochTime(moment() / 1000),
nsfw: nsfw,
mentions: msg.mentions.map(u => u.username).join(','),
type: 2
}).run();
}
}));
bot.on('messageCreate', async(function (msg) {
processUser(msg);
let isDm = msg.channel.guild == undefined;
if (bu.awaitMessages.hasOwnProperty(msg.channel.id)
&& bu.awaitMessages[msg.channel.id].hasOwnProperty(msg.author.id)) {
let firstTime = bu.awaitMessages[msg.channel.id][msg.author.id].time;
if (moment.duration(moment() - firstTime).asMinutes() <= 5) {
bu.emitter.emit(bu.awaitMessages[msg.channel.id][msg.author.id].event, msg);
}
}
if (msg.channel.id != '194950328393793536')
if (msg.author.id == bot.user.id) {
if (!isDm)
bu.logger.output(`${msg.channel.guild.name} (${msg.channel.guild.id})> ${msg.channel.name} `
+ `(${msg.channel.id})> ${msg.author.username}> ${msg.content} (${msg.id})`);
else
bu.logger.output(`PM> ${msg.channel.name} (${msg.channel.id})> `
+ `${msg.author.username}> ${msg.content} (${msg.id})`);
}
if (msg.channel.id === config.discord.channel) {
if (!(msg.author.id == bot.user.id && msg.content.startsWith('\u200B'))) {
var message;
if (msg.content.startsWith('_') && msg.content.endsWith('_'))
message = ` * ${msg.member.nick ? msg.member.nick : msg.author.username} ${msg.cleanContent
.substring(1, msg.cleanContent.length - 1)}`;
else {
if (msg.author.id == bot.user.id) {
message = `${msg.cleanContent}`;
} else {
message = `\<${msg.member.nick ? msg.member.nick : msg.author.username}\> ${msg.cleanContent}`;
}
}
bu.logger.output(message);
var attachUrl = '';
if (msg.attachments.length > 0) {
bu.logger.debug(util.inspect(msg.attachments[0]));
attachUrl += ` ${msg.attachments[0].url}`;
}
sendMessageToIrc(message + attachUrl);
}
}
if (msg.author.id !== bot.user.id) {
let antimention;
if (!isDm) antimention = await(bu.guildSettings.get(msg.channel.guild ? msg.channel.guild.id : '', 'antimention'));
var parsedAntiMention = parseInt(antimention);
if (!(parsedAntiMention == 0 || isNaN(parsedAntiMention))) {
if (msg.mentions.length >= parsedAntiMention) {
bu.logger.info('BANN TIME');
if (!bu.bans[msg.channel.guild.id])
bu.bans[msg.channel.guild.id] = {};
bu.bans[msg.channel.guild.id][msg.author.id] = { mod: bot.user, type: 'Auto-Ban', reason: 'Mention spam' };
bot.banGuildMember(msg.channel.guild.id, msg.author.id, 1).then(() => {
}).catch(() => {
delete bu.bans[msg.channel.guild.id][msg.author.id];
bu.send(msg.channel.id, `${msg.author.username} is mention spamming, but I lack the permissions to ban them!`);
});
}
}
let prefix = await(bu.guildSettings.get(!isDm ? msg.channel.guild.id : '', 'prefix'));
if (isDm) {
prefix = '';
}
if (msg.content.toLowerCase().startsWith('blargbot')) {
var index = msg.content.toLowerCase().indexOf('t');
prefix = msg.content.substring(0, index + 1);
} else if (msg.content.toLowerCase().startsWith(bu.config.discord.defaultPrefix)) {
prefix = bu.config.discord.defaultPrefix;
}
let blacklisted;
if (!isDm) blacklisted = await(bu.isBlacklistedChannel(msg.channel.id));
if (blacklisted &&
msg.content.replace(prefix, '').split(' ')[0].toLowerCase() != 'blacklist') {
return;
}
if (msg.content.indexOf('(╯°□°)╯︵ ┻━┻') > -1 && !msg.author.bot) {
flipTables(msg, false);
}
if (msg.content.indexOf('┬─┬ ノ( ゜-゜ノ)') > -1 && !msg.author.bot) {
flipTables(msg, true);
}
var doCleverbot = false;
if (msg.content.startsWith(`<@${bot.user.id}>`) || msg.content.startsWith(`<@!${bot.user.id}>`)) {
prefix = msg.content.match(/<@!?[0-9]{17,21}>/)[0];
bu.logger.debug('Was a mention');
doCleverbot = true;
}
if (msg.content.startsWith(prefix)) {
var command = msg.content.replace(prefix, '').trim();
bu.logger.command('Incoming Command:', `${prefix} ${command}`);
try {
let wasCommand = await(handleDiscordCommand(msg.channel, msg.author, command, msg));
bu.logger.command('Was command:', wasCommand);
if (wasCommand) {
let deletenotif = await(bu.guildSettings.get(msg.channel.guild.id, 'deletenotif'));
if (deletenotif != '0') {
if (!commandMessages[msg.channel.guild.id]) {
commandMessages[msg.channel.guild.id] = [];
}
commandMessages[msg.channel.guild.id].push(msg.id);
if (commandMessages[msg.channel.guild.id].length > 100) {
commandMessages[msg.channel.guild.id].shift();
}
}
if (msg.channel.guild) {
bu.r.table('user').get(msg.author.id).update({
lastcommand: msg.cleanContent,
lastcommanddate: bu.r.epochTime(moment() / 1000)
}).run();
}
} else {
if (doCleverbot && !msg.author.bot) {
Cleverbot.prepare(function () {
var username = msg.channel.guild.members.get(bot.user.id).nick
? msg.channel.guild.members.get(bot.user.id).nick
: bot.user.username;
var msgToSend = msg.cleanContent.replace(new RegExp('@' + username + ',?'), '').trim();
bu.logger.debug(msgToSend);
bu.cleverbotStats++;
cleverbot.write(msgToSend
, function (response) {
bot.sendChannelTyping(msg.channel.id);
setTimeout(function () {
bu.sendMessageToDiscord(msg.channel.id, response.message);
}, 1500);
});
});
}
}
} catch (err) {
bu.logger.error(err.stack);
}
} else {
if (msg.author.id == bu.CAT_ID && msg.content.indexOf('discord.gg') == -1) {
var prefixes = ['!', '@', '#', '$', '%', '^', '&'
, '*', ')', '-', '_', '=', '+', '}', ']', '|'
, ';', ':', '\'', '>', '?', '/', '.', '"'];
if (!msg.content ||
(prefixes.indexOf(msg.content.substring(0, 1)) == -1)
&& !msg.content.startsWith('k!')
&& !msg.content.startsWith('b!')
&& msg.channel.guild) {
let last = await(bu.r.table('catchat').orderBy({ index: bu.r.desc('id') }).nth(1).run());
if ((last && last.content != msg.content) || msg.content == '') {
var content = msg.content;
try {
while (/<@!?[0-9]{17,21}>/.test(content)) {
content = content.replace(/<@!?[0-9]{17,21}>/, '@' + await(bu.getUserFromName(msg, content.match(/<@!?([0-9]{17,21})>/)[1], true)).username);
}
} catch (err) {
bu.logger.error(err.stack);
}
let nsfw = true;
if (!isDm) nsfw = await(bu.isNsfwChannel(msg.channel.id));
bu.r.table('catchat').insert({
// id: await(bu.r.table('chatlogs').count().run()),
content: msg.content,
attachment: msg.attachments[0] ? msg.attachments[0].url : null,
userid: msg.author.id,
msgid: msg.id,
channelid: msg.channel.id,
guildid: isDm ? 'DM' : msg.channel.guild.id,
msgtime: bu.r.epochTime(moment(msg.timestamp) / 1000),
nsfw: nsfw,
mentions: msg.mentions.map(u => u.username).join(','),
}).run();
}
}
}
}
}
if (msg.channel.id != '204404225914961920') {
let nsfw = true;
if (!isDm) nsfw = await(bu.isNsfwChannel(msg.channel.id));
bu.r.table('chatlogs').insert({
content: msg.content,
attachment: msg.attachments[0] ? msg.attachments[0].url : null,
userid: msg.author.id,
msgid: msg.id,
channelid: msg.channel.id,
guildid: isDm ? 'DM' : msg.channel.guild.id,
msgtime: bu.r.epochTime(moment(msg.timestamp) / 1000),
nsfw: nsfw,
mentions: msg.mentions.map(u => u.username).join(','),
type: 0
}).run();
}
}));
initCommands();
bot.connect();
};
/**
* Reloads the misc variables object
*/
function reloadVars() {
fs.readFileSync(path.join(__dirname, 'vars.json'), 'utf8', function (err, data) {
if (err) throw err;
vars = JSON.parse(data);
});
}
/**
* Saves the misc variables to a file
*/
function saveVars() {
fs.writeFileSync(path.join(__dirname, 'vars.json'), JSON.stringify(vars, null, 4));
}
var gameId;
/**
* Switches the game the bot is playing
* @param forced - if true, will not set a timeout (Boolean)
*/
function switchGame(forced) {
var name = '';
var oldId = gameId;
while (oldId == gameId) {
gameId = bu.getRandomInt(0, 6);
}
switch (gameId) {
case 0:
name = `with ${bot.users.size} users!`;
break;
case 1:
name = `in ${bot.guilds.size} guilds!`;
break;
case 2:
name = `in ${Object.keys(bot.channelGuildMap).length} channels!`;
break;
case 3:
name = `with tiny bits of string!`;
break;
case 4:
name = `with delicious fish!`;
break;
case 5:
name = `on version ${bu.VERSION}!`;
break;
case 6:
name = `type 'blargbot help'!`;
break;
}
bot.editStatus(null, {
name: name
});
if (!forced)
setTimeout(function () {
switchGame();
}, 60000);
}
var avatarId;
/**
* Switches the avatar
* @param forced - if true, will not set a timeout (Boolean)
*/
function switchAvatar(forced) {
bot.editSelf({ avatar: avatars[avatarId] });
avatarId++;
if (avatarId == 8)
avatarId = 0;
if (!forced)
setTimeout(function () {
switchAvatar();
}, 300000);
}
var commandMessages = {};
var handleDiscordCommand = async((channel, user, text, msg) => {
let words = bu.splitInput(text);
if (msg.channel.guild)
bu.logger.command(`Command '${text}' executed by ${user.username} (${user.id}) on server ${msg.channel.guild.name} (${msg.channel.guild.id}) on channel ${msg.channel.name} (${msg.channel.id}) Message ID: ${msg.id}`);
else
bu.logger.command(`Command '${text}' executed by ${user.username} (${user.id}) in a PM (${msg.channel.id}) Message ID: ${msg.id}`);
if (msg.author.bot) {
return false;
}
let val = await(bu.ccommand.get(msg.channel.guild ? msg.channel.guild.id : '', words[0]));
if (val) {
var command = text.replace(words[0], '').trim();
var response = await(tags.processTag(msg, val, command));
if (response !== 'null') {
bu.sendMessageToDiscord(channel.id, response);
}
return true;
} else {
if (config.discord.commands[words[0]] != null) {
bu.sendMessageToDiscord(channel.id, `${
config.discord.commands[words[0]]
.replace(/%REPLY/, `<@${user.id}>`)}`);
return true;
} else {
if (bu.commandList.hasOwnProperty(words[0].toLowerCase())) {
let commandName = bu.commandList[words[0].toLowerCase()].name;
let val2 = await(bu.canExecuteCommand(msg, commandName));
if (val2[0]) {
executeCommand(commandName, msg, words, text);
}
return val2[0];
} else {
return false;
}
}
}
});
var executeCommand = async(function (commandName, msg, words, text) {
bu.r.table('stats').get(commandName).update({
uses: bu.r.row('uses').add(1),
lastused: bu.r.epochTime(moment() / 1000)
}).run();
if (bu.commandStats.hasOwnProperty(commandName)) {
bu.commandStats[commandName]++;
} else {
bu.commandStats[commandName] = 1;
}
bu.commandUses++;
bu.commands[commandName].execute(msg, words, text);
return true;
});
var messageLogs = [];
var messageI = 0;
/**
* Function to be called manually (through eval) to generate logs for any given channel
* @param channelid - channel id (String)
* @param msgid - id of starting message (String)
* @param times - number of times to repeat the cycle (int)
*/
function createLogs(channelid, msgid, times) {
if (messageI < times)
bot.getMessages(channelid, 100, msgid).then((kek) => {
bu.logger.info(`finished ${messageI + 1}/${times}`);
for (var i = 0; i < kek.length; i++) {
messageLogs.push(`${kek[i].author.username}> ${kek[i].author.id}> ${kek[i].content}`);
}
messageI++;
setTimeout(() => {
createLogs(channelid, kek[kek.length - 1].id, times);
}, 5000);
});
else {
}
}
/**
* Function to be used with createLogs
* @param name - file name (String)
*/
function saveLogs(name) {
messageI = 0;
fs.writeFile(path.join(__dirname, name), JSON.stringify(messageLogs, null, 4));
}
/**
* Posts stats about the bot to https://bots.discord.pw
*/
function postStats() {
var stats = JSON.stringify({
server_count: bot.guilds.size
});
var options = {
hostname: 'bots.discord.pw',
method: 'POST',
port: 443,
path: `/api/bots/${bot.user.id}/stats`,
headers: {
'User-Agent': 'blargbot/1.0 (ratismal)',
'Authorization': vars.botlisttoken,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(stats)
}
};
bu.logger.info('Posting to abal');
var req = https.request(options, function (res) {
var body = '';
res.on('data', function (chunk) {
bu.logger.debug(chunk);
body += chunk;
});
res.on('end', function () {
bu.logger.debug('body: ' + body);
});
res.on('error', function (thing) {
bu.logger.warn(`Result error occurred! ${thing}`);
});
});
req.on('error', function (err) {
bu.logger.warn(`Request error occurred! ${err}`);
});
req.write(stats);
req.end();
if (!config.general.isbeta) {
bu.logger.info('Posting to matt');
request.post({
'url': 'https://www.carbonitex.net/discord/data/botdata.php',
'headers': { 'content-type': 'application/json' }, 'json': true,
body: {
'key': config.general.carbontoken,
'servercount': bot.guilds.size,
'logoid': 'https://i.imgur.com/uVq0zdO.png'
}
});
}
}
var lastUserStatsKek;
/**
* Gets information about a bot - test function
* @param id - id of bot
*/
function fml(id) {
var options = {
hostname: 'bots.discord.pw',
method: 'GET',
port: 443,
path: `/api/users/${id}`,
headers: {
'User-Agent': 'blargbot/1.0 (ratismal)',
'Authorization': vars.botlisttoken
}
};
var req = https.request(options, function (res) {
var body = '';
res.on('data', function (chunk) {
bu.logger.debug(chunk);
body += chunk;
});
res.on('end', function () {
bu.logger.debug('body: ' + body);
lastUserStatsKek = JSON.parse(body);
bu.logger.debug(lastUserStatsKek);
});
res.on('error', function (thing) {
bu.logger.warn(`Result Error: ${thing}`);
});
});
req.on('error', function (err) {
bu.logger.warn(`Request Error: ${err}`);
});
req.end();
}
/**
* Displays the contents of a function
* @param msg - message
* @param text - command text
*/
function eval2(msg, text) {
if (msg.author.id === bu.CAT_ID) {
var commandToProcess = text.replace('eval2 ', '');
bu.logger.debug(commandToProcess);
try {
bu.sendMessageToDiscord(msg.channel.id, `\`\`\`js
${eval(`${commandToProcess}.toString()`)}
\`\`\``);
} catch (err) {
bu.sendMessageToDiscord(msg.channel.id, err.message);
}
} else {
bu.sendMessageToDiscord(msg.channel.id, `You don't own me!`);
}
}
/**
* Evaluates code
* @param msg - message (Message)
* @param text - command text (String)
*/
var eval1 = async((msg, text) => {
if (msg.author.id === bu.CAT_ID) {
var commandToProcess = text.replace('eval ', '');
if (commandToProcess.startsWith('```js') && commandToProcess.endsWith('```'))
commandToProcess = commandToProcess.substring(6, commandToProcess.length - 3);
else if (commandToProcess.startsWith('```') && commandToProcess.endsWith('```'))
commandToProcess = commandToProcess.substring(4, commandToProcess.length - 3);
try {
bu.sendMessageToDiscord(msg.channel.id, `Input:
\`\`\`js
${commandToProcess}
\`\`\`
Output:
\`\`\`js
${commandToProcess == '1/0' ? 1 : eval(commandToProcess)}
\`\`\``);
if (commandToProcess.indexOf('vars') > -1) {
saveVars();
}
} catch (err) {
bu.sendMessageToDiscord(msg.channel.id, `An error occured!
\`\`\`js
${err.stack}
\`\`\``);
}
} else {
bu.sendMessageToDiscord(msg.channel.id, `You don't own me!`);
}
});
/**
* Processes a user into the database
* @param msg - message (Message)
*/
var processUser = async(function (msg) {
let storedUser = await(bu.r.table('user').get(msg.author.id).run());
if (!storedUser) {
bu.logger.debug(`inserting user ${msg.author.id} (${msg.author.username})`);
bu.r.table('user').insert({
userid: msg.author.id,
username: msg.author.username,
usernames: [{
name: msg.author.username,
date: bu.r.epochTime(moment() / 1000)
}],