forked from Tupperbox/TupperboxLegacy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.js
1005 lines (941 loc) · 41.6 KB
/
bot.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
//dependencies
const Eris = require("eris");
const logger = require("winston");
const request = require("request");
const fs = require("fs");
const validUrl = require("valid-url");
const util = require("util");
//create data files if they don't exist
["/auth.json","/tulpae.json","/servercfg.json","/webhooks.json"].forEach(file => {
if(!fs.existsSync(__dirname + file))
fs.writeFileSync(__dirname + file, "{ }", (err) => { if(err) throw err; });
});
const auth = require("./auth.json");
const tulpae = require("./tulpae.json");
const config = require("./servercfg.json");
const webhooks = require("./webhooks.json");
const recent = {};
const feedbackID = "431722290971934721";
const zwsp = String.fromCharCode(8203); //zero-width space for embed formatting
var disconnects = 0;
logger.configure({
level: "debug",
transports: [
new logger.transports.Console(),
new logger.transports.File({ filename: "output.log" })
],
format: logger.format.combine(
logger.format((info) => {info.message = util.format(info.message); return info; })(),
logger.format.colorize(),
logger.format.printf(info => `${info.level}: ${info.message}`)
)
});
// Initialize Bot
var bot = new Eris(auth.discord);
bot.on("ready", () => {
logger.info(`Connected\nLogged in as:\n${bot.user.username} - (${bot.user.id})`);
updateStatus();
setInterval(updateStatus, 1800000);
bot.guilds.forEach(validateGuildCfg);
});
bot.on("guildCreate", validateGuildCfg);
bot.on("disconnect", function() {
logger.warn("Bot disconnected! Attempting to reconnect.");
disconnects++;
if(disconnects < 50)
bot.connect();
});
bot.on("error", console.error);
bot.on("messageCreate", async function (msg) {
if(msg.author.bot) return;
let cfg = msg.channel.guild && config[msg.channel.guild.id] || { prefix: "tul!", rolesEnabled: false, lang: "tulpa"};
if (msg.content.startsWith(cfg.prefix) && (!cfg.cmdblacklist || !cfg.cmdblacklist.includes(msg.channel.id))) {
var args = msg.content.substr(cfg.prefix.length).split(" ");
var cmd = args.shift();
if(bot.cmds[cmd] && checkPermissions(cmd,msg,args)) {
logger.info(`${msg.channel.guild ? msg.channel.guild.name + ": " : "private message: "}${msg.author.username} executed command ${msg.content}`);
return bot.cmds[cmd].execute(msg, args, cfg);
}
} else if(tulpae[msg.author.id] && !(msg.channel instanceof Eris.PrivateChannel) && (!cfg.blacklist || !cfg.blacklist.includes(msg.channel.id))) {
let clean = msg.cleanContent || msg.content;
clean = clean.replace(/(<:.+?:\d+?>)|(<@!?\d+?>)/,"cleaned");
let cleanarr = clean.split("\n");
let lines = msg.content.split("\n");
let replace = [];
for(let i = 0; i < lines.length; i++) {
tulpae[msg.author.id].forEach(t => {
if(checkTulpa(msg, t, cleanarr[i])) {
replace.push([msg,cfg,t,lines[i].substring(t.brackets[0].length, lines[i].length-t.brackets[1].length)]);
}
});
}
if(replace.length < 2) replace = [];
if(!replace[0]) {
for(let t of tulpae[msg.author.id]) {
if(checkTulpa(msg, t, clean)) {
replace.push([msg, cfg, t, msg.content.substring(t.brackets[0].length, msg.content.length-t.brackets[1].length)]);
break;
}
}
}
if(replace[0]) {
Promise.all(replace.map(r => replaceMessage(...r)))
.then(() => {
if(msg.channel.permissionsOf(bot.user.id).has("manageMessages"))
msg.delete().catch(e => { if(e.code == 50013) { send(msg.channel, "Warning: I'm missing permissions needed to properly replace messages."); }});
save("tulpae",tulpae);
}).catch(e => send(msg.channel, e));
}
}
});
async function replaceMessage(msg, cfg, tulpa, content) {
const hook = await fetchWebhook(msg.channel);
const data = {
wait: true,
content: content,
username: `${tulpa.name} ${tulpa.tag ? tulpa.tag : ""} ${checkTulpaBirthday(tulpa) ? "\uD83C\uDF70" : ""}`,
avatarURL: tulpa.url,
};
if(recent[msg.channel.id] && msg.author.id !== recent[msg.channel.id].userID && data.username === recent[msg.channel.id].name) {
data.username = data.username.substring(0,1) + "\u200a" + data.username.substring(1);
}
if(msg.attachments[0]) {
return sendAttachmentsWebhook(msg, cfg, data, content, hook);
}
try {
await bot.executeWebhook(hook.id,hook.token,data);
} catch (e) {
console.log(e);
if(e.code === 10015) {
delete webhooks[msg.channel.id];
const hook = await fetchWebhook(msg.channel);
return bot.executeWebhook(hook.id,hook.token,data);
}
}
if(cfg.log && msg.channel.guild.channels.has(cfg.log)) {
send(msg.channel.guild.channels.get(cfg.log),
`Name: ${tulpa.name}\nRegistered by: ${msg.author.username}#${msg.author.discriminator}\nChannel: <#${msg.channel.id}>\nMessage: ${content}`);
}
if(!tulpa.posts) tulpa.posts = 0;
tulpa.posts++;
if(!recent[msg.channel.id] && !msg.channel.permissionsOf(bot.user.id).has("manageMessages")) {
send(msg.channel, `Warning: I do not have permission to delete messages. Both the original message and ${cfg.lang} webhook message will show.`);
}
recent[msg.channel.id] = {
userID: msg.author.id,
name: data.username,
tulpa: tulpa,
};
}
function checkTulpa(msg, tulpa, clean) {
return clean.startsWith(tulpa.brackets[0]) && clean.endsWith(tulpa.brackets[1]) && ((clean.length == (tulpa.brackets[0].length + tulpa.brackets[1].length) && msg.attachments[0]) || clean.length > (tulpa.brackets[0].length + tulpa.brackets[1].length));
}
async function sendAttachmentsWebhook(msg, cfg, data, content, hook, tulpa) {
let files = [];
for(let i = 0; i < msg.attachments.length; i++) {
files.push({ file: await attach(msg.attachments[i].url), name: msg.attachments[i].filename });
}
data.file = files;
return new Promise((resolve, reject) => {
bot.executeWebhook(hook.id,hook.token,data)
.catch(e => {
console.log(e);
if(e.code == 10015) {
delete webhooks[msg.channel.id];
return fetchWebhook(msg.channel).then(hook => {
return bot.executeWebhook(hook.id,hook.token,data);
}).catch(e => reject("Webhook deleted and error creating new one. Check my permissions?"));
}
}).then(() => {
if(cfg.log && msg.channel.guild.channels.has(cfg.log)) {
let logchannel = msg.channel.guild.channels.get(cfg.log);
if(!recent[msg.channel.id] && !logchannel.permissionsOf(bot.user.id).has("sendMessages")) {
send(msg.channel, "Warning: There is a log channel configured but I do not have permission to send messages to it. Logging has been disabled.");
cfg.log = null;
save("servercfg",config);
}
else if(logchannel.permissionsOf(bot.user.id).has("sendMessages"))
send(logchannel, `Name: ${tulpa.name}\nRegistered by: ${msg.author.username}#${msg.author.discriminator}\nChannel: <#${msg.channel.id}>\nMessage: ${content}`);
}
if(!tulpa.posts) tulpa.posts = 0;
tulpa.posts++;
if(!recent[msg.channel.id] && !msg.channel.permissionsOf(bot.user.id).has("manageMessages"))
send(msg.channel, "Warning: I do not have permission to delete messages. Both the original message and " + cfg.lang + " webhook message will show.");
recent[msg.channel.id] = { userID: msg.author.id, name: data.username, tulpa: tulpa };
resolve();
}).catch(reject);
});
}
function fetchWebhook(channel) {
return new Promise((resolve, reject) => {
if(webhooks[channel.id])
resolve(webhooks[channel.id]);
else if(!channel.permissionsOf(bot.user.id).has("manageWebhooks"))
reject("Proxy failed: Missing 'Manage Webhooks' permission in this channel.");
else {
channel.createWebhook({ name: "Tupperhook" }).then(hook => {
webhooks[channel.id] = { id: hook.id, token: hook.token };
resolve(webhooks[channel.id]);
save("webhooks",webhooks);
}).catch(e => { reject("Proxy failed with unknown reason: Error " + e.code); });
}
});
}
function attach(url, name) {
return new Promise(function(resolve, reject) {
request({url:url,encoding:null}, (err, res, data) => {
console.log(`${url}: ${data.length}`);
resolve(data);
});
});
}
bot.cmds = {
help: {
help: cfg => "Print this message, or get help for a specific command",
usage: cfg => ["help - print list of commands",
"help [command] - get help on a specific command"],
permitted: () => true,
execute: function(msg, args, cfg) {
let output = "";
if(args[0]) { //help for a specific command
if(bot.cmds[args[0]] && checkPermissions(args[0],msg,args) && bot.cmds[args[0]].usage) {
output = { embed: {
title: "Bot Command | " + args[0],
description: bot.cmds[args[0]].help(cfg) + "\n\n**Usage:**\n",
timestamp: new Date().toJSON(),
color: 0x999999,
author: {
name: "Tupperware",
icon_url: bot.user.avatarURL
},
footer: {
text: "If something is wrapped in <> or [], do not include the brackets when using the command. They indicate whether that part of the command is required <> or optional []."
}
}};
for(let u of bot.cmds[args[0]].usage(cfg))
output.embed.description += `${cfg.prefix + u}\n`;
if(bot.cmds[args[0]].desc)
output.embed.description += `\n${bot.cmds[args[0]].desc(cfg)}`;
} else output += "Command not found.";
} else { //general help
output = { embed: {
title: "Tupperware | Help",
description: "I am Tupperware, a bot made to give " + cfg.lang + "s a voice using Discord webhooks.\nTo get started, register a " + cfg.lang + " with `" + cfg.prefix + "register` and enter a message with the brackets you set!\n\n**Command List**\nType `"+cfg.prefix+"help command` for detailed help on a command.\n" + zwsp + "\n",
timestamp: new Date().toJSON(),
color: 0x999999,
author: {
name: "Tupperware",
icon_url: bot.user.avatarURL
},
footer: {
text: "By Keter#1730"
}
}};
for(let cmd of Object.keys(bot.cmds)) {
if(bot.cmds[cmd].help && bot.cmds[cmd].permitted(msg,args))
output.embed.description += `**${cfg.prefix + cmd}** - ${bot.cmds[cmd].help(cfg)}\n`;
}
}
send(msg.channel, output);
}
},
//owner-only eval command for testing and on-the-spot hotfixes/changes
js: {
permitted: (msg) => { return msg.author.id === auth.owner; },
execute: function(msg, args, cfg) {
if(msg.author.id != auth.owner) return;
let message = msg.content.substr(7);
let out = "";
try {
out = eval(message);
} catch(e) {
out = e.toString();
}
send(msg.channel, util.inspect(out).slice(0,2000));
}
},
//owner-only feedback reply command
reply: {
permitted: (msg) => { return msg.author.id === auth.owner; },
execute: function(msg, args, cfg) {
if(msg.author.id != auth.owner) return;
bot.getMessage(feedbackID, args[0]).then(message => {
let parts = message.content.split("\n");
if(parts[3] && parts[0].startsWith("User") && parts[1].startsWith("Server") && parts[2].startsWith("Channel") && parts[3].startsWith("Message")) {
let user = bot.users.get(parts[0].split(" ")[1]);
let server = bot.guilds.get(parts[1].split(" ")[1]);
let channel = server && server.channels.get(parts[2].split(" ")[1]);
let message = parts[3].split(" ").slice(1).join(" ");
let embed = { embed: {
title: "Reply to Feedback",
description: `**Original by ${user.username}#${user.discriminator}**\n${message}\n\n**Reply**\n${args.slice(1).join(" ")}`,
timestamp: new Date().toJSON(),
color: 0x999999,
footer: {
text: "Keter#1730",
icon_url: bot.users.get(auth.owner).avatarURL
}
}};
send(channel, embed);
}
}).catch(e => send(msg.channel, e.toString()));
}
},
//attach webhook(s) to channel(s)
hook: {
permitted: () => true,
execute: function(msg, args, cfg) {
send(msg.channel, "This command no longer has a purpose. Webhooks have been changed to opt-out rather than opt-in: they are automatically generated for non-blacklisted channels when a user attempts to proxy in them.");
}
},
//register new tulpa
register: {
help: cfg => "Register a new " + cfg.lang + "",
usage: cfg => ["register <name> <brackets> - Register a new " + cfg.lang + ".\n\t<name> - the " + cfg.lang + "'s name, for multi-word names surround this argument in `''`\n\t<brackets> - the word 'text' surrounded by any characters on one or both sides"],
desc: cfg => "Example use: `register Test >text<` - registers a " + cfg.lang + " named 'Test' that is triggered by messages surrounded by ><\nBrackets can be anything, one sided or both. For example `text<<` and `T:text` are both valid\nNote that you can enter multi-word names by surrounding the full name in apostrophes `''`.",
permitted: () => true,
execute: function(msg, args, cfg) {
args = getMatches(msg.content,/['](.*?)[']|(\S+)/gi).slice(1);
let out = "";
let brackets = args.slice(1).join(" ").split("text");
if(!args[0]) {
return bot.cmds.help.execute(msg, ["register"], cfg);
} else if(!args[1]) {
out = "Missing argument 'brackets'. Try `" + cfg.prefix + "help register` for usage details.";
} else if(args[0].length < 2 || args[0].length > 28) {
out = "Name must be between 2 and 28 characters.";
} else if(brackets.length < 2) {
out = "No 'text' found to detect brackets with. For the last part of your command, enter the word 'text' surrounded by any characters (except `''`).\nThis determines how the bot detects if it should replace a message.";
} else if(!brackets[0] && !brackets[1]) {
out = "Need something surrounding 'text'.";
} else if(tulpae[msg.author.id] && tulpae[msg.author.id].find(t => t.name === args[0])) {
out = proper(cfg.lang) + " with that name under your user account already exists.";
} else if(tulpae[msg.author.id] && tulpae[msg.author.id].find(t => t.brackets[0] == brackets[0] && t.brackets[1] == brackets[1])) {
out = proper(cfg.lang) + " with those brackets under your user account already exists.";
} else {
if(!tulpae[msg.author.id]) tulpae[msg.author.id] = [];
let tulpa = {
name: args[0],
url: "https://i.imgur.com/ZpijZpg.png",
brackets: brackets,
posts: 0,
host: msg.author.id
};
tulpae[msg.author.id].push(tulpa);
let guilds = Object.keys(config).filter(id => config[id].rolesEnabled && bot.guilds.has(id) && bot.guilds.get(id).members.has(msg.author.id)).map(id => bot.guilds.get(id));
if(guilds[0]) {
tulpa.roles = {};
Promise.all(guilds.map(g => {
return g.createRole({ name: tulpa.name, mentionable: true}).then(r => {
tulpa.roles[g.id] = r.id;
g.members.get(msg.author.id).addRole(r.id);
});
})).then(() => {
save("tulpae",tulpae);
});
}
save("tulpae",tulpae);
out = proper(cfg.lang) + " registered successfully!\nName: " + tulpa.name + "\nBrackets: " + `${brackets[0]}text${brackets[1]}` + "\nUse `" + cfg.prefix + "rename`, `" + cfg.prefix + "brackets`, and `" + cfg.prefix + "avatar` to set/update your " + cfg.lang + "'s info.";
}
send(msg.channel, out);
}
},
//unregister tulpa
remove: {
help: cfg => "Unregister a " + cfg.lang + "",
usage: cfg => ["remove <name> - Unregister the named " + cfg.lang + " from your list"],
permitted: () => true,
execute: function(msg, args, cfg) {
let out = "";
args = getMatches(msg.content,/['](.*?)[']|(\S+)/gi).slice(1);
let name = args.join(" ");
if(!args[0]) {
return bot.cmds.help.execute(msg, ["remove"], cfg);
} else if(!tulpae[msg.author.id]) {
out = "You do not have any " + cfg.lang + "s registered.";
} else if(!tulpae[msg.author.id].find(t => t.name.toLowerCase() == name.toLowerCase())) {
out = "Could not find " + cfg.lang + " with that name registered under your account.";
} else {
out = proper(cfg.lang) + " unregistered.";
let arr = tulpae[msg.author.id];
let tul = arr.find(t => t.name.toLowerCase() == name.toLowerCase());
Object.keys(config).filter(t => config[t].rolesEnabled && bot.guilds.has(t)).map(t => bot.guilds.get(t)).forEach(g => {
if(tul.roles && tul.roles[g.id]) g.deleteRole(tul.roles[g.id]);
});
arr.splice(arr.indexOf(tul), 1);
save("tulpae",tulpae);
}
send(msg.channel, out);
}
},
list: {
help: cfg => "Get a detailed list of yours or another user's registered " + cfg.lang + "s",
usage: cfg => ["list [user] - Sends a list of the user's registered " + cfg.lang + "s, their brackets, post count, and birthday (if set). If user is not specified it defaults to the message author."],
permitted: () => true,
execute: function(msg, args, cfg) {
let out = "";
let target;
if(args[0]) {
target = resolveUser(msg, args.join(" "));
} else {
target = msg.author;
}
if(!target) {
out = "User not found.";
} else if(!tulpae[target.id]) {
out = (target.id == msg.author.id) ? "You have not registered any " + cfg.lang + "s." : "That user has not registered any " + cfg.lang + "s.";
} else {
out = { embed: {
title: `${target.username}#${target.discriminator}'s registered ${cfg.lang}s`,
author: {
name: target.username,
icon_url: target.avatarURL
},
fields: []
}};
let len = 200;
let page = 1;
tulpae[target.id].forEach(t => {
let field = generateTulpaField(t);
len += field.name.length;
len += field.value.length;
if(len < 5000) {
out.embed.fields.push(field);
} else {
out.embed.title += ` (page ${page})`;
send(msg.channel, out);
len = 200;
page++;
out.embed.title = `${target.username}#${target.discriminator}'s registered ${cfg.lang}s (page ${page})`;
out.embed.fields = [field];
}
});
}
send(msg.channel, out);
}
},
rename: {
help: cfg => "Change a " + cfg.lang + "'s name",
usage: cfg => ["rename <name> <newname> - Set a new name for the " + cfg.lang + ""],
permitted: () => true,
execute: function(msg, args, cfg) {
let out = "";
args = getMatches(msg.content,/['](.*?)[']|(\S+)/gi).slice(1);
if(!args[0]) {
return bot.cmds.help.execute(msg, ["rename"], cfg);
} else if(!args[1]) {
out = "Missing argument 'newname'.";
} else if(args[1].length < 2 || args[1].length > 28) {
out = "New name must be between 2 and 28 characters.";
} else if(!tulpae[msg.author.id] || !tulpae[msg.author.id].find(t => t.name.toLowerCase() == args[0].toLowerCase())) {
out = "You don't have a " + cfg.lang + " with that name registered.";
} else if(tulpae[msg.author.id].find(t => t.name.toLowerCase() == args[1].toLowerCase())) {
out = "You already have a " + cfg.lang + " with that new name.";
} else {
tulpae[msg.author.id].find(t => t.name.toLowerCase() == args[0].toLowerCase()).name = args[1];
save("tulpae",tulpae);
out = proper(cfg.lang) + " renamed successfully.";
}
send(msg.channel, out);
}
},
avatar: {
help: cfg => "View or change a " + cfg.lang + "'s avatar",
usage: cfg => ["avatar <name> [url] - if url is specified, change the " + cfg.lang + "'s avatar, if not, simply echo the current one"],
permitted: () => true,
desc: cfg => "The specified URL must be a direct link to an image - that is, the URL should end in .jpg or .png or another common image filetype. Also, it can't be over 1mb in size, as Discord doesn't accept images over this size as webhook avatars.",
execute: function(msg, args, cfg) {
let out = "";
args = getMatches(msg.content,/['](.*?)[']|(\S+)/gi).slice(1);
if(!args[0]) {
return bot.cmds.help.execute(msg, ["avatar"], cfg);
} else if(!tulpae[msg.author.id] || !tulpae[msg.author.id].find(t => t.name.toLowerCase() == args[0].toLowerCase())) {
out = "You don't have a " + cfg.lang + " with that name registered.";
} else if(!args[1]) {
out = tulpae[msg.author.id].find(t => t.name.toLowerCase() == args[0].toLowerCase()).url;
} else if(!validUrl.isWebUri(args[1])) {
out = "Malformed url.";
} else {
request(args[1], { method: "HEAD" }, (err, res) => {
if(err || !res.headers["content-type"] || !res.headers["content-type"].startsWith("image")) return send(msg.channel, "I couldn't find an image at that URL. Make sure it's a direct link (ends in .jpg or .png for example).");
if(Number(res.headers["content-length"]) > 1000000) {
return send(msg.channel, "That image is too large and Discord will not accept it. Please use an image under 1mb.");
}
tulpae[msg.author.id].find(t => t.name.toLowerCase() == args[0].toLowerCase()).url = args[1];
save("tulpae",tulpae);
send(msg.channel, "Avatar changed successfully.");
});
return;
}
send(msg.channel, out);
}
},
describe: {
help: cfg => "View or change a " + cfg.lang + "'s description",
usage: cfg => ["describe <name> [desc] - if desc is specified, change the " + cfg.lang + "'s describe, if not, simply echo the current one"],
permitted: () => true,
execute: function(msg, args, cfg) {
let out = "";
args = getMatches(msg.content,/['](.*?)[']|(\S+)/gi).slice(1);
if(!args[0]) {
return bot.cmds.help.execute(msg, ["describe"], cfg);
} else if(!tulpae[msg.author.id] || !tulpae[msg.author.id].find(t => t.name.toLowerCase() == args[0].toLowerCase())) {
out = "You don't have a " + cfg.lang + " with that name registered.";
} else if(!args[1]) {
out = tulpae[msg.author.id].find(t => t.name.toLowerCase() == args[0].toLowerCase()).desc;
} else {
tulpae[msg.author.id].find(t => t.name.toLowerCase() == args[0].toLowerCase()).desc = args.slice(1).join(" ").slice(0,500);
save("tulpae",tulpae);
out = "Description updated successfully.";
}
send(msg.channel, out);
}
},
birthday: {
help: cfg => "View or change a " + cfg.lang + "'s birthday, or see upcoming birthdays",
usage: cfg => ["birthday [name] [date] -\n\tIf name and date are specified, set the named " + cfg.lang + "'s birthday to the date.\n\tIf name only is specified, show the " + cfg.lang + "'s birthday.\n\tIf neither are given, show the next 5 birthdays on the server."],
desc: cfg => "Date must be given in format MM/DD/YY",
permitted: () => true,
execute: function(msg, args, cfg) {
let out = "";
args = getMatches(msg.content,/['](.*?)[']|(\S+)/gi).slice(1);
if(!args[0]) {
let tulps = Object.keys(tulpae)
.filter(id => id == msg.author.id || (msg.channel.guild && msg.channel.guild.members.has(id)))
.reduce((arr, tul) => arr.concat(tulpae[tul]), []);
if(!tulps[0])
return send(msg.channel, "No " + cfg.lang + "s have been registered on this server.");
tulps = tulps.filter(t => !!t.birthday);
if(!tulps[0])
return send(msg.channel, "No " + cfg.lang + "s on this server have birthdays set.");
let n = new Date();
let now = new Date(n.getFullYear(),n.getMonth(),n.getDate());
out = "Here are the next few upcoming " + cfg.lang + " birthdays in this server:\n" +
tulps.sort((a,b) => {
let first = new Date(a.birthday);
first.setFullYear(now.getFullYear());
if(first < now) first.setFullYear(now.getFullYear()+1);
let second = new Date(b.birthday);
second.setFullYear(now.getFullYear());
if(second < now) second.setFullYear(now.getFullYear()+1);
return first.getTime()-second.getTime();
}).slice(0,5)
.map(t => {
let bday = new Date(t.birthday);
bday.setFullYear(now.getFullYear());
if(bday < now) bday.setFullYear(now.getFullYear()+1);
return (bday.getTime() == now.getTime()) ? `${t.name}: Birthday today! \uD83C\uDF70` : `${t.name}: ${bday.toDateString()}`;
}).join("\n");
} else if(!tulpae[msg.author.id] || !tulpae[msg.author.id].find(t => t.name.toLowerCase() === args[0].toLowerCase())) {
out = "You don't have a " + cfg.lang + " with that name registered.";
} else if(!args[1]) {
let bday = tulpae[msg.author.id].find(t => t.name.toLowerCase() === args[0].toLowerCase()).birthday;
out = bday ? new Date(bday).toDateString() : "No birthday currently set for " + args[0];
} else if(!(new Date(args[1]).getTime())) {
out = "I can't understand that date. Please enter in the form MM/DD/YYYY with no spaces.";
} else {
let date = new Date(args[1]);
tulpae[msg.author.id].find(t => t.name.toLowerCase() === args[0].toLowerCase()).birthday = date.getTime();
save("tulpae",tulpae);
out = `${proper(cfg.lang)} '${args[0]}' birthday set to ${date.toDateString()}.`;
}
send(msg.channel, out);
}
},
brackets: {
help: cfg => "View or change a " + cfg.lang + "'s brackets",
usage: cfg => ["brackets <name> [brackets] - if brackets are given, change the " + cfg.lang + "'s brackets, if not, simply echo the current one"],
desc: cfg => "Brackets must be the word 'text' surrounded by any symbols or letters, i.e. `[text]` or `>>text`",
permitted: () => true,
execute: function(msg, args, cfg) {
let out = "";
args = getMatches(msg.content,/['](.*?)[']|(\S+)/gi).slice(1);
if(!args[0]) {
return bot.cmds.help.execute(msg, ["brackets"], cfg);
} else if(!tulpae[msg.author.id] || !tulpae[msg.author.id].find(t => t.name.toLowerCase() == args[0].toLowerCase())) {
out = "You don't have a " + cfg.lang + " with that name registered.";
} else if(!args[1]) {
let brackets = tulpae[msg.author.id].find(t => t.name.toLowerCase() == args[0].toLowerCase()).brackets;
out = `Brackets for ${args[0]}: ${brackets[0]}text${brackets[1]}`;
} else {
let brackets = args.slice(1).join(" ").split("text");
if(brackets.length < 2) {
out = "No 'text' found to detect brackets with. For the last part of your command, enter the word 'text' surrounded by any characters (except `''`).\nThis determines how the bot detects if it should replace a message.";
} else if(!brackets[0] && !brackets[1]) {
out = "Need something surrounding 'text'.";
} else {
tulpae[msg.author.id].find(t => t.name.toLowerCase() == args[0].toLowerCase()).brackets = brackets;
save("tulpae",tulpae);
out = "Brackets updated successfully.";
}
}
send(msg.channel, out);
}
},
tag: {
help: cfg => "Remove or change a " + cfg.lang + "'s tag (displayed next to name when proxying)",
usage: cfg => ["tag <name> [tag] - if tag is given, change the " + cfg.lang + "'s tag, if not, clear the tag"],
desc: cfg => "A " + cfg.lang + "'s tag is shown next to their name when speaking.",
permitted: () => true,
execute: function(msg, args, cfg) {
let out = "";
args = getMatches(msg.content,/['](.*?)[']|(\S+)/gi).slice(1);
if(!args[0]) {
return bot.cmds.help.execute(msg, ["tag"], cfg);
} else if(!tulpae[msg.author.id] || !tulpae[msg.author.id].find(t => t.name.toLowerCase() == args[0].toLowerCase())) {
out = "You don't have a " + cfg.lang + " with that name registered.";
} else if(!args[1]) {
delete tulpae[msg.author.id].find(t => t.name.toLowerCase() == args[0].toLowerCase()).tag;
save("tulpae",tulpae);
out = "Tag cleared.";
} else if (args.slice(1).join(" ").length + tulpae[msg.author.id].find(t => t.name.toLowerCase() == args[0].toLowerCase()).name.length > 27) {
out = "That tag is too long to use with that " + cfg.lang + "'s name. The combined total must be less than 28 characters.";
} else {
tulpae[msg.author.id].find(t => t.name.toLowerCase() == args[0].toLowerCase()).tag = args.slice(1).join(" ");
save("tulpae",tulpae);
out = "Tag updated successfully.";
}
send(msg.channel, out);
}
},
showuser: {
help: cfg => "Show the user that registered the " + cfg.lang + " that last spoke",
usage: cfg => ["showuser - Finds the user that registered the " + cfg.lang + " that last sent a message in this channel"],
permitted: (msg) => true,
execute: function(msg, args, cfg) {
if(!recent[msg.channel.id]) send(msg.channel, "No " + cfg.lang + "s have spoken in this channel since I last started up, sorry.");
else {
let user = bot.users.get(recent[msg.channel.id].userID);
send(msg.channel, `Last ${cfg.lang} message sent by ${recent[msg.channel.id].tulpa.name}, registered to ${user ? user.username + "#" + user.discriminator : "(unknown user " + recent[msg.channel.id].userID + ")"}`);
}
}
},
find: {
help: cfg => "Find and display info about " + cfg.lang + "s by name",
usage: cfg => ["find <name> - Attempts to find a " + cfg.lang + " with exactly the given name, and if none are found, tries to find " + cfg.lang + "s with names containing the given name."],
permitted: (msg) => true,
execute: function(msg, args, cfg) {
if(msg.channel instanceof Eris.PrivateChannel)
return send(msg.channel, "This command cannot be used in private messages.");
if(!args[0])
return bot.cmds.help.execute(msg, ["find"], cfg);
let all = Object.keys(tulpae)
.filter(id => msg.channel.guild.members.has(id))
.reduce((arr, tul) => arr.concat(tulpae[tul]), []);
if(!all[0]) {
return send(msg.channel, "There are no " + cfg.lang + "s registered on this server.");
}
let search = args.join(" ").toLowerCase();
let tul = all.filter(t => t.name.toLowerCase() == search);
if(!tul[0])
tul = all.filter(t => t.name.toLowerCase().includes(search));
if(!tul[0])
send(msg.channel, "Couldn't find a " + cfg.lang + " with that name.");
else {
if(tul.length == 1) {
let t = tul[0];
let host = bot.users.get(t.host);
let embed = { embed: {
author: {
name: t.name,
icon_url: t.url
},
description: `Host: ${host ? host.username + "#" + host.discriminator : "Unknown user " + t.host}\n${generateTulpaField(t).value}`,
}};
send(msg.channel, embed);
} else {
tul = tul.slice(0,10);
let embed = { embed: {
title: "Results",
fields: []
}};
tul.forEach(t => {
let host = bot.users.get(t.host);
embed.embed.fields.push({name: t.name, value: `Host: ${host ? host.username + "#" + host.discriminator : "Unknown user " + t.host}\n${generateTulpaField(t).value}`});
});
send(msg.channel, embed);
}
}
}
},
invite: {
help: cfg => "Get the bot's invite URL",
usage: cfg => ["invite - sends the bot's oauth2 URL in this channel"],
permitted: (msg) => true,
execute: function(msg, args, cfg) {
send(msg.channel, `https://discordapp.com/api/oauth2/authorize?client_id=${auth.inviteCode}&permissions=805314560&scope=bot`);
}
},
feedback: {
help: cfg => "Send a message to the developer, who may reply through the bot",
usage: cfg => ["feedback <message> - send the message to the developer"],
desc: cfg => "Dev note: I'm always happy to answer questions too, or just to chat!",
permitted: (msg) => true,
execute: function(msg, args, cfg) {
if(!args[0]) return bot.cmds.help.execute(msg, ["feedback"], cfg);
bot.createMessage("431722290971934721", `User: ${msg.author.id} ${msg.author.username}#${msg.author.discriminator}\nServer: ${msg.channel.guild ? msg.channel.guild.id + " " + msg.channel.guild.name : "DM"}\nChannel: ${msg.channel.id} ${msg.channel.name}\nMessage: ${args.join(" ")}`);
send(msg.channel, "I've passed along your message, thank you.");
}
},
cfg: {
help: cfg => "Configure server-specific settings",
usage: cfg => ["cfg prefix <newPrefix> - Change the bot's prefix",
"cfg roles <enable|disable> - Enable or disable automatically managed mentionable " + cfg.lang + " roles, so that users can mention " + cfg.lang + "s",
"cfg rename <newname> - Change all instances of the default name 'tulpa' in bot replies in this server to the specified term",
"cfg log <channel> - Enable the bot to send a log of all " + cfg.lang + " messages and some basic info like who registered them. Useful for having a searchable channel and for distinguishing between similar names.",
"cfg blacklist <add|remove> <channel(s)> - Add or remove channels to the bot's proxy blacklist - users will be unable to proxy in blacklisted channels.",
"cfg cmdblacklist <add|remove> <channel(s)> - Add or remove channels to the bot's command blacklist - users will be unable to issue commands in blacklisted channels."],
permitted: (msg) => (msg.member && msg.member.permission.has("administrator")),
execute: function(msg, args, cfg) {
let out = "";
if(msg.channel instanceof Eris.PrivateChannel) {
out = "This command cannot be used in private messages.";
} else if(!args[0] || !["prefix","roles","rename","log","blacklist","cmdblacklist"].includes(args[0])) {
return bot.cmds.help.execute(msg, ["cfg"], cfg);
} else if(args[0] == "prefix") {
if(!args[1]) {
out = "Missing argument 'prefix'.";
} else {
cfg.prefix = args[1];
out = "Prefix changed to " + args[1];
save("servercfg",config);
}
} else if(args[0] == "roles") {
if(!msg.channel.guild.members.get(bot.user.id).permission.has("manageRoles")) {
out = "I don't have permission to manage roles.";
} else if(args[1] === "enable") {
let guild = msg.channel.guild;
if(cfg.rolesEnabled)
out = proper(cfg.lang) + " roles already enabled on this server.";
else {
cfg.rolesEnabled = true;
Promise.all(Object.keys(tulpae).filter(t => guild.members.has(t)).map(t => {
let mem = guild.members.get(t);
return Promise.all(tulpae[t].map(tul => {
if(!mem.roles.find(r => guild.roles.get(r).name === tul.name))
return guild.createRole({name: tul.name, mentionable: true}).then(r => {
mem.addRole(r.id);
if(!tul.roles) tul.roles = {};
tul.roles[guild.id] = r.id;
});
return true;
}));
})).then(() => {
save("tulpae",tulpae);
});
save("servercfg",config);
out = proper(cfg.lang) + " roles enabled. Adding the roles may take some time.";
}
} else if(args[1] === "disable") {
let guild = msg.channel.guild;
if(!cfg.rolesEnabled)
out = proper(cfg.lang) + " roles already disabled on this server.";
else {
cfg.rolesEnabled = false;
Object.keys(tulpae).filter(t => guild.members.has(t)).forEach(t => {
let mem = guild.members.get(t);
tulpae[t].forEach(tul => {
if(tul.roles && tul.roles[guild.id]) {
guild.deleteRole(tul.roles[guild.id]);
delete tul.roles[guild.id];
if(!Object.keys(tul.roles)[0]) delete tul.roles;
}
});
});
save("tulpae",tulpae);
save("servercfg",config);
out = proper(cfg.lang) + " roles disabled. Deleting the roles may take some time.";
}
} else {
out = "Missing argument 'enable|disable'.";
}
} else if(args[0] == "rename") {
if(!args[1]) {
out = "Missing argument 'newname'";
} else {
cfg.lang = args.slice(1).join(" ");
out = "Entity name changed to " + cfg.lang;
save("servercfg",config);
}
} else if(args[0] == "log") {
if(!args[1]) {
out = "Logging channel unset. Logging is now disabled.";
cfg.log = null;
save("servercfg",config);
} else {
let channel = resolveChannel(msg,args[1]);
if(!channel) {
out = "Channel not found.";
} else {
out = `Logging channel set to <#${channel.id}>`;
cfg.log = channel.id;
save("servercfg",config);
}
}
} else if(args[0] == "blacklist") {
if(!args[1]) {
if(cfg.blacklist) out = `Currently blacklisted channels: ${cfg.blacklist.map(id => "<#"+id+">").join(" ")}`;
else out = "No channels currently blacklisted.";
} else if(args[1] == "add") {
if(!args[2]) {
out = "Must provide name/mention/id of channel to blacklist.";
} else {
let channels = args.slice(2).map(arg => resolveChannel(msg,arg)).map(ch => { if(ch) return ch.id; else return ch; });
if(!channels.find(ch => ch != undefined)) {
out = `Could not find ${channels.length > 1 ? "those channels" : "that channel"}.`;
} else if(channels.find(ch => ch == undefined)) {
out = "Could not find these channels: ";
for(let i = 0; i < channels.length; i++)
if(!channels[i]) out += args.slice(2)[i];
} else {
if(!cfg.blacklist) cfg.blacklist = [];
cfg.blacklist = cfg.blacklist.concat(channels);
out = `Channel${channels.length > 1 ? "s" : ""} blacklisted successfully.`;
save("servercfg",config);
}
}
} else if(args[1] == "remove") {
if(!args[2]) {
out = "Must provide name/mention/id of channel to allow.";
} else {
let channels = args.slice(2).map(arg => resolveChannel(msg,arg)).map(ch => { if(ch) return ch.id; else return ch; });
if(!channels.find(ch => ch != undefined)) {
out = `Could not find ${channels.length > 1 ? "those channels" : "that channel"}.`;
} else if(channels.find(ch => ch == undefined)) {
out = "Could not find these channels: ";
for(let i = 0; i < channels.length; i++)
if(!channels[i]) out += args.slice(2)[i] + " ";
} else {
if(!cfg.blacklist) cfg.blacklist = [];
channels.forEach(ch => { if(cfg.blacklist.includes(ch)) cfg.blacklist.splice(cfg.blacklist.indexOf(ch),1); });
out = `Channel${channels.length > 1 ? "s" : ""} removed from blacklist.`;
if(!cfg.blacklist[0]) delete cfg.blacklist;
save("servercfg",config);
}
}
} else {
out = "Invalid argument: must be 'add' or 'remove'";
}
} else if(args[0] == "cmdblacklist") {
if(!args[1]) {
if(cfg.cmdblacklist) out = `Currently cmdblacklisted channels: ${cfg.cmdblacklist.map(id => "<#"+id+">").join(" ")}`;
else out = "No channels currently cmdblacklisted.";
} else if(args[1] == "add") {
if(!args[2]) {
out = "Must provide name/mention/id of channel to cmdblacklist.";
} else {
let channels = args.slice(2).map(arg => resolveChannel(msg,arg)).map(ch => { if(ch) return ch.id; else return ch; });
if(!channels.find(ch => ch != undefined)) {
out = `Could not find ${channels.length > 1 ? "those channels" : "that channel"}.`;
} else if(channels.find(ch => ch == undefined)) {
out = "Could not find these channels: ";
for(let i = 0; i < channels.length; i++)
if(!channels[i]) out += args.slice(2)[i];
} else {
if(!cfg.cmdblacklist) cfg.cmdblacklist = [];
cfg.cmdblacklist = cfg.cmdblacklist.concat(channels);
out = `Channel${channels.length > 1 ? "s" : ""} blacklisted successfully.`;
save("servercfg",config);
}
}
} else if(args[1] == "remove") {
if(!args[2]) {
out = "Must provide name/mention/id of channel to allow.";
} else {
let channels = args.slice(2).map(arg => resolveChannel(msg,arg)).map(ch => { if(ch) return ch.id; else return ch; });
if(!channels.find(ch => ch != undefined)) {
out = `Could not find ${channels.length > 1 ? "those channels" : "that channel"}.`;
} else if(channels.find(ch => ch == undefined)) {
out = "Could not find these channels: ";
for(let i = 0; i < channels.length; i++)
if(!channels[i]) out += args.slice(2)[i] + " ";
} else {
if(!cfg.cmdblacklist) cfg.cmdblacklist = [];
channels.forEach(ch => { if(cfg.cmdblacklist.includes(ch)) cfg.cmdblacklist.splice(cfg.cmdblacklist.indexOf(ch),1); });
out = `Channel${channels.length > 1 ? "s" : ""} removed from cmdblacklist.`;
if(!cfg.cmdblacklist[0]) delete cfg.cmdblacklist;
save("servercfg",config);
}
}
} else {
out = "Invalid argument: must be 'add' or 'remove'";
}
}
send(msg.channel, out);
}
}
};
bot.cmds.showhost = {
permitted: true,
execute: bot.cmds.showuser.execute
};
if (!auth.inviteCode) {
delete bot.cmds.invite;
}
function updateStatus() {
bot.editStatus({ name: `tul!help | ${Object.values(tulpae).reduce((acc,val) => acc + val.length, 0)} registered`});
}
function validateGuildCfg(guild) {
if(!config[guild.id])
config[guild.id] = {};
if(config[guild.id].prefix == undefined)
config[guild.id].prefix = "tul!";
if(config[guild.id].rolesEnabled == undefined)
config[guild.id].rolesEnabled = false;
if(config[guild.id].lang == undefined)
config[guild.id].lang = "tulpa";
if(config[guild.id].log == undefined)
config[guild.id].log = null;
save("servercfg",config);
}
function proper(text) {
return text.substring(0,1).toUpperCase() + text.substring(1);
}
function save(name, obj) {
return fs.writeFile(`${__dirname}/${name}.json`,JSON.stringify(obj,null,2), printError);
}
function generateTulpaField(tulpa) {
return {
name: tulpa.name,
value: `${tulpa.tag ? ("Tag: " + tulpa.tag + "\n") : ""}Brackets: ${tulpa.brackets[0]}text${tulpa.brackets[1]}\nAvatar URL: ${tulpa.url}${tulpa.birthday ? ("\nBirthday: "+new Date(tulpa.birthday).toDateString()) : ""}\nTotal messages sent: ${tulpa.posts}${tulpa.desc ? ("\n"+tulpa.desc) : ""}`
};
}
function checkTulpaBirthday(tulpa) {
if(!tulpa.birthday) return false;
let day = new Date(tulpa.birthday);
let now = new Date();
return day.getDate() == now.getDate() && day.getMonth() == now.getMonth();
}
function resolveUser(msg, text) {
let target = bot.users.get(/<@!?(\d+)>/.test(text) && text.match(/<@!?(\d+)>/)[1]) || bot.users.get(text) || msg.channel.guild.members.find(m => m.username.toLowerCase() == text.toLowerCase() || (m.nick && m.nick.toLowerCase()) == text.toLowerCase() || text.toLowerCase() == `${m.username.toLowerCase()}#${m.discriminator}`);
if(target && target.user) target = target.user;
return target;
}
function resolveChannel(msg, text) {
let g = msg.channel.guild;
return g.channels.get(/<#(\d+)>/.test(text) && text.match(/<#(\d+)>/)[1]) || g.channels.get(text) || g.channels.find(m => m.name.toLowerCase() == text.toLowerCase());
}
function checkPermissions(cmd, msg, args) {
return (msg.author.id === auth.owner) || (bot.cmds[cmd].permitted(msg,args));
}
function printError(err) {
if(err) return console.error(err);
}
function send(channel, message, file, typing) {
if(typing) {
return channel.sendTyping().then(() => {
setTimeout(() => channel.createMessage(message,file), Math.min(6*message.length+750,4000));
});
}
channel.createMessage(message, file);
}
function getMatches(string, regex) {
var matches = [];
var match;
while (match = regex.exec(string)) {
match.splice(1).forEach(m => { if(m) matches.push(m); });
}
return matches;
}