-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
executable file
·1216 lines (1095 loc) · 33.7 KB
/
index.ts
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
import * as Discord from "discord.js";
import { mkdirSync, promises as fs } from "fs";
import moment from "moment";
import mdf from "moment-duration-format";
import * as d from "discord-api-types/v10";
import path from "path";
import client from "./bot";
import { messages, safe } from "./messages";
import "./src/commands/about";
import "./src/commands/channelmanagement";
import "./src/commands/emoji";
import "./src/commands/fun";
import "./src/commands/help";
import "./src/commands/logging";
import "./src/commands/quote";
import "./src/commands/settings";
import "./src/commands/speedrun";
import "./src/commands/test";
import "./src/commands/role";
import "./src/commands/apdocs";
import "./src/commands/customcommands";
import {
onMessageReactionAdd as ticketMessageReactionAdd,
onMessage as ticketMessage,
} from "./src/commands/ticket";
import { globalConfig } from "./src/config";
import Database, { Event } from "./src/Database";
import Info, { memberCanManageRole, handleReactions } from "./src/Info";
import * as nr from "./src/NewRouter";
import { dgToDiscord } from "./src/parseDiscordDG";
import { handleList } from "./src/commands/quote";
import { createTimer } from "./src/commands/fun/helpers";
import {
findAllProvidedRoles,
getRankSuccessMessage,
} from "./src/commands/role";
import { getGuilds } from "./src/ShardHelper";
import fetch from "node-fetch";
import { deleteLogs } from "./src/commands/logging";
import { sendPinBottom } from "./src/commands/channelmanagement";
import * as SlashCommandManager from "./src/SlashCommandManager";
import { getPanelByInfo, encodePanel, displayPanel } from "./src/commands/fun/buttongames/paneleditor";
import { cancelAllEventsForSearch, queueEvent } from "./src/fancy/lib/TimedEventsAt2";
import { reportError } from "./src/fancy/lib/report_error";
import { Routes } from "discord.js";
mdf(moment as any);
try {
mkdirSync(path.join(process.cwd(), `logs`));
} catch (e) {}
export let serverStartTime = 0;
export const production = process.env.NODE_ENV === "production";
const mostRecentCommands: { content: string, date: string }[] = [];
function devlog(...msg: any) {
if (!production) {
global.console.log(...msg);
}
}
export type ErrorWithID = Error & { errorCode: string };
export function wrapErrorAddID(error: Error): ErrorWithID {
if (typeof error !== "object")
error = new Error("primitive error: " + error);
(error as ErrorWithID).errorCode = Math.floor(
Math.random() * 1000000000000000,
).toString(36);
return error as ErrorWithID;
}
export async function ilt<T>(
v: Promise<T> /*, reason: string (added to error message)*/,
reason: string | false,
): Promise<
{ error: ErrorWithID, result: undefined } | { error: undefined, result: T }
> {
let result: T;
try {
result = await v;
} catch (error) {
const ewid = wrapErrorAddID(error as Error);
if (typeof reason === "string") {
void reportILTFailure(ewid, new Error(reason));
}
return { error: ewid, result: undefined };
}
return { result, error: undefined };
}
export function perr(
v: Promise<unknown> /*, reason: string (added to error message)*/,
reason: string | false,
): void {
void ilt(v, reason);
}
nr.globalCommand(
"/help/depricated/spoiler",
"spoiler",
{
usage: "spoiler {Required|message}",
description: "send a spoiler",
examples: [],
perms: {
raw_message: true,
bot: ["manage_messages"],
},
},
nr.passthroughArgs,
async ([msgt], info) => {
let msg = msgt;
if(msg && !msg.includes("||")) {
msg = "||"+msg+"||";
}
msgt ||= "\u200b";
msg = info.message.author.toString()+" sent a spoiler: " + msg;
if(msgt.length > 2000) {
msgt = msgt.substr(0, 1999) + "…";
}
const attch = [...info.raw_message?.attachments.values() ?? []];
if(attch.length === 0) return info.error("Attach an image.");
if(attch.length > 1) return info.error("Attach only one image.");
await info.channel.send({
content: msg,
files: [{
attachment: attch[0].url,
name: "SPOILER_" + (attch[0].name ?? "spoiler"),
spoiler: true,
}],
allowedMentions: { parse: [], roles: [], users: [] },
});
perr(info.message.delete(), "Deleting original message for spoiler");
// if(er) send message...
},
);
depricate("spaceChannels", "channels spacing", "2.0"); // 1.0 -> 2.0
remove(
"channels spacing",
"Unfortunately, discord has removed the ability for bots to put spaces in channel names.",
);
nr.globalAlias("send", "channels sendMany");
// usage.add("channels", require("./src/commands/channelmanagement")); !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// router.add("help", [], async (cmd, info, next) => {
// await info.result(
// messages.help(info, info.db ? await info.db.getLists() : {}),
// );
// });
nr.globalAlias("messages set welcome", "settings events welcome");
nr.globalAlias("messages set goodbye", "settings events goodbye");
nr.globalAlias("log download", "downloadLog");
nr.globalAlias("log reset", "resetLog");
remove("listRoles", "3.0");
remove(
"settings listroles",
"Discord now has a builtin way for you to get the ID of roles by right clicking a role in the Roles section of settings. Also, most interpunct commands will now accept a role name instead of ID.",
"3.0",
);
function depricate(oldcmd: string, newcmd: string, version = "3.0") {
nr.globalCommand(
"/help/depricated/" + oldcmd.toLowerCase().replace(" ", "/"),
oldcmd.toLowerCase(),
{
usage: oldcmd.toLowerCase(),
description: "depricated",
examples: [],
perms: {},
},
nr.list(),
async ([], info) => {
return await info.error(
`\`${oldcmd}\` has been renamed to \`${newcmd}\` as part of Interpunct Bot ${version}. See \`help\` for more information. Join the support server in \`about\` if you have any issues.`,
);
},
);
}
function remove(oldcmd: string, reason: string, version = "3.0") {
nr.globalCommand(
"/help/removed/" + oldcmd.toLowerCase(),
oldcmd.toLowerCase(),
{
usage: oldcmd.toLowerCase(),
description: "removed",
examples: [],
perms: {},
},
nr.passthroughArgs,
async ([], info) => {
return await info.error(
messages.failure.command_removed(info, oldcmd, version, reason),
);
},
);
}
depricate("settings prefix", "set prefix [new prefix]");
depricate("settings lists", "lists [add/edit/remove]");
depricate("settings discmoji", "emoji");
depricate("settings rankmoji", "emoji");
remove(
"settings permreplacements",
"Permreplacements were never tested and probably didn't work.",
);
depricate("settings speedrun", "speedrun <add/remove/default>");
depricate("settings nameScreening", "autoban");
depricate("settings logging", "log <enable/disable>");
depricate("settings events", "messages");
depricate(
"settings unknownCommandMessages",
"set show unknown command [always/admins/never]",
);
depricate(
"settings commandFailureMessages",
"set show errors [always/admins/never]",
);
depricate("settings autospaceChannels", "space channels automatically");
depricate("settings", "help");
/*
# ip!settings prefix [new prefix...]
# ip!settings lists [list] [pastebin id of list|remove]
# ip!settings discmoji restrict [role id] [emoji|emoji id]
# ip!settings discmoji unrestrict [role id] [emoji|emoji id]
# ip!settings discmoji list [emoji|emoji id]
@ ip!settings rankmoji
@ ip!settings rankmoji add [role id] [emoji]
@ ip!settings rankmoji remove [role id|emoji]
@ ip!settings rankmoji channel [channel]
# ip!settings permreplacements
# ip!settings permreplacements set [perm] [replacementID]
# ip!settings permreplacements remove [perm]
# ip!settings speedrun [abbreviation] [category]
# ip!settings nameScreening
# ip!settings nameScreening add [name parts...]
# ip!settings nameScreening remove [name parts...]
# ip!settings logging [true|false]
# ip!settings events welcome [none|welcome @s/%s message...]
# ip!settings events goodbye [none|goodbye @s/%s message...]
# ip!settings unknownCommandMessages [true|false]
# ip!settings commandFailureMessages [true|false]
# ip!settings autospaceChannels [true|false]
# ip!settings listRoles [true|false]
# ip!ping
# ip!speedrun rules [category...]
# ip!speedrun leaderboard [top how many?] [category...]
# ip!log download
# ip!log reset
@ ip!purge [msgs to delete]
# ip!spoiler [message...]
# ip!channels spacing [space|dash]
# ip!channels sendMany [...message] [#channels #to #send #to]
# ip!about
# ip!crash
*/
async function unknownCommandHandler(cmd: string, info: Info) {
if (info.db) {
const lists = await info.db.getCustomCommands(); // TODO info.db.lists
const listNames = Object.keys(lists);
for (const listName of listNames) {
if (
cmd.toLowerCase().startsWith(listName.toLowerCase()) &&
(cmd.substr(listName.length).trim() !==
cmd.substr(listName.length) ||
!cmd.substr(listName.length))
) {
const args = cmd.substr(listName.length).trim();
const ll = lists[listName];
if (ll.type === "list") {
await handleList(listName, ll.pastebin, args, info);
return;
}
if (ll.type === "command") {
if (args) return await info.error("No args.");
await info.result(ll.text);
return;
}
if(ll.type === "sendpanel") {
if(info.guild == null) return await info.error("not in guid");
const panelcont = await getPanelByInfo(info.guild.id, ll.guild_command_name);
if(panelcont == null) return await info.error("Error displaying panel, it no longer exists. Named: ["+ll.guild_command_name+"]. tell a server mod to do command /delete or something.");
const display_res = displayPanel(encodePanel(panelcont.saved_state), info, "error");
if(display_res.result === "error") {
return await info.error("Error displaying a panel: " + display_res.error);
}
await client.rest.post(Routes.channelMessages(info.message.channel.id), {
body: display_res.message,
});
return;
}
assertNever(ll);
}
}
}
const autoResolution =
"/" +
cmd
.trim()
.split(" ")
.join("/");
const docsPage =
nr.globalDocs["/help" + autoResolution] ||
nr.globalDocs[autoResolution];
if (docsPage) {
const bodyText = dgToDiscord(docsPage.body, info);
await info.result(
// dgToDiscord(`{Var|bodyText}\n\n{Bold|Full Help}: {Link|${url}}`) // concept
(
bodyText +
"\n\n" +
"**Full Help**: <https://interpunct.info" +
docsPage.path +
">"
).replace(/\n\n+/g, "\n\n"),
);
return;
}
const unknownCommandMessages = info.db
? await info.db.getUnknownCommandMessages()
: "always";
if (
unknownCommandMessages === "always" ||
(unknownCommandMessages === "admins" && await info.theyHavePermsToManageBot())
) {
if (/^[^a-zA-Z]/.exec(cmd)) return; // for people using different prefixes like $ so $10 doesn't trigger
return await info.errorAlways(
messages.failure.command_not_found(info, cmd),
);
} // else do nothing
}
async function updateActivity() {
console.log("Posting activity...");
if (client.user) {
const count = await getGuilds(client);
client.user.setPresence({
activities: [{
name:
count === -1
? "ip!help on a bunch of servers"
: `ip!help on ${count} servers`,
type: Discord.ActivityType.Watching,
url: "https://interpunct.info/",
}],
status: production ? "online" : "idle",
});
const dbgToken = globalConfig.listings?.["discord.bots.gg"];
if (count !== -1 && dbgToken) {
const fres = await fetch(
"https://discord.bots.gg/api/v1/bots/" +
client.user.id +
"/stats",
{
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: dbgToken,
},
body: JSON.stringify({
guildCount: count, //client.guilds.cache.size,
// shardCount: client.shard ? client.shard.count : 1,
// shardId: client.shard ? client.shard.ids[0] : 1,
}),
// ^ or, post {guildCount: count on this shard, shardCount: count, shardId: number}
},
);
console.log("dbgg post success", await fres.text());
}
const tggToken = globalConfig.listings?.["top.gg"];
if (count !== -1 && tggToken) {
const fres = await fetch(
"https://top.gg/api/bots/" + client.user.id + "/stats",
{
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: tggToken,
},
body: JSON.stringify({
server_count: count,
}),
},
);
console.log("topgg post success", await fres.json());
}
}
console.log("Post succesful!");
// if(process.env.NODE_ENV !== "production") return; // only production should post
// let options = {
// url: `https://bots.discord.pw/api/bots/${config.bdpid}/stats`,
// headers: {
// Authorization: config["bots.discord.pw"]
// },
// json: {
// server_count: count // eslint-disable-line camelcase
// }
// };
// request.post(options, (er, res) => {});
}
export function shouldIgnore(user: Discord.User): boolean {
return user.bot && !globalConfig.testing?.users?.includes(user.id);
}
client.on("debug", console.log);
client.on("warn", console.log);
client.on("ready", () => {
global.console.log("Ready");
serverStartTime = new Date().getTime();
perr(updateActivity(), "activity update");
perr(SlashCommandManager.start(), "slash command manager");
});
setInterval(() => perr(updateActivity(), "activity update"), 30 * 60 * 1000); // update every 30 min
function streplace(str: string, eplace: { [key: string]: string }) {
const uids: { [key: string]: string } = {};
Object.keys(eplace).forEach(key => {
uids[key] = "!!!{{<>::}}" + Math.random().toString(36);
str = str.split(key).join(uids[key]);
});
Object.keys(eplace).forEach(key => {
str = str.split(uids[key]).join(eplace[key]);
});
return str;
}
async function guildMemberAdd(
member: Discord.GuildMember | Discord.PartialGuildMember,
) {
if (member.partial) {
// partial is not supported
console.log("!!! PARTIAL MEMBER WAS AQUIRED IN A MEMBER ADD EVENT");
await member.fetch();
if (!tsAssert<Discord.GuildMember>(member)) return;
}
const db = new Database(member.guild.id);
const nameParts = (await db.getAutoban()).filter(screen =>
member.displayName.toLowerCase().includes(screen.toLowerCase()),
);
if (nameParts.length > 0) {
// if any part of name contiains screen
if (member.bannable) {
await member.ban({
reason: `Name contains dissallowed words: ${nameParts.join(
`, `,
)}`,
});
// if (info.logging) {
// try {
// guildLog(
// member.guild.id,
// `[${moment().format("YYYY-MM-DD HH:mm:ss Z")}] Banned ${
// member.displayName
// } because their name contains ${nameParts.join`, `}`
// );
// } catch (e) {
// throw e;
// }
// } !!!!!!!!!!!!!!!!!!!!!!!!!!!!! this should be logged on bot.on(ban)
} else {
await db.addError(
safe`Unable to ban user named ${member.displayName}, possibly because interpunct bot does not have permission to ban members.`,
"name screening",
);
}
}
const events = await db.getEvents();
if (events.userJoin) {
await runEvent(events.userJoin, db, member.guild, {
"{Mention}": member.toString(),
"{Name}": safe(member.displayName),
}); // should (usually) not error
}
}
client.on("guildMemberAdd", member => {
perr(guildMemberAdd(member), "member joined");
});
function tsAssert<V>(a: any): a is V {
return !!a || true;
}
export function assertNever(value: never): never {
console.log(value);
throw new Error("Never!");
}
async function runEvent(
event: Event,
db: Database,
guild: Discord.Guild,
vars: { [key: string]: string },
) {
if (event.action === "none") {
return;
} else if (event.action === "message") {
let channel = event.channel;
if (channel === "{SystemMessagesChannel}") {
if (!guild.systemChannel)
return await db.addError(
"/errors/ipscript/system-channel-not-set",
"unused",
);
channel = guild.systemChannel.id;
}
const channelDiscord = guild.channels.resolve(channel);
if (!channelDiscord) {
return await db.addError(
"/errors/ipscript/channel-id-not-found",
"",
);
}
if (channelDiscord.type !== Discord.ChannelType.GuildText) {
return await db.addError(
"/errors/ipscript/channel-not-text-channel",
"",
);
}
return await channelDiscord.send(streplace(event.message, vars));
} else {
return assertNever(event);
}
}
client.on("guildMemberRemove", member => {
perr(
(async () => {
if (member.partial) {
// partial is not supported
console.log(
"!!! PARTIAL MEMBER WAS AQUIRED IN A MEMBER REMOVE EVENT",
"the member is:",
member.toString(),
);
}
if (!member.guild) return;
const db = new Database(member.guild.id); // it seems bad creating these objects just to forget them immediately
const events = await db.getEvents();
if (events.userLeave) {
await runEvent(events.userLeave, db, member.guild, {
"{Mention}": member.toString(),
"{Name}": safe(
member.displayName || "(name could not be determined)",
),
}); // should (usually) not error
}
})(),
"member left.",
);
});
// async function spaceChannelIfNecessary(channel: Discord.GuildChannel) {
// if (!channel.guild) return; // ???
// const db = new Database(channel.guild.id);
// if (await db.getAutospaceChannels()) {
// if (doesChannelRequireSpacing(channel, "-")) {
// await spaceChannel(
// channel,
// "-",
// `automatically spaced. \`${await db.getPrefix()}space channels disable\` to stop`,
// );
// }
// }
// }
//
// client.on("channelCreate", (newC: Discord.GuildChannel) =>
// perr(spaceChannelIfNecessary(newC), "space channel on create"),
// );
// client.on(
// "channelUpdate",
// (_oldC: Discord.GuildChannel, newC: Discord.GuildChannel) =>
// perr(spaceChannelIfNecessary(newC), "space channel on update"),
// );
function logMsg({ msg, prefix }: { msg: Discord.Message, prefix: string }) {
if (msg.guild) {
devlog(
`${prefix}< [${msg.guild.nameAcronym}] ${
channelNameForLog(msg.channel)
} \`${msg.author.tag}\`: ${msg.content}`,
);
} else {
devlog(`${prefix}< pm: ${msg.author.tag}: ${msg.content}`);
}
}
async function guildLog(id: string, log: string) {
await fs.appendFile(
path.join(process.cwd(), `logs/${id}.log`),
`${log}\n`,
"utf8",
);
}
export function logCommand(
guild_id: string | undefined,
channel_id: string | undefined,
author_bot: boolean,
author_id: string | undefined,
message: string,
): void {
try {
guildLog(
"__commands", // db ? guild! : guild?
`[${moment().format("YYYY-MM-DD HH:mm:ss Z")}] (${
guild_id || "DM"
}) <#${channel_id ?? "ENOCH"}> ${author_bot ? "[BOT] " : ""}<@${
author_id ?? "ENOA"
}>: ${message}`,
).catch(() => {});
} catch (e) {}
}
async function onMessage(msg: Discord.Message | Discord.PartialMessage) {
if (msg.partial) {
// partial is not supported
console.log("!!! PARTIAL MESSAGE WAS AQUIRED IN A MESSAGE EVENT");
await msg.fetch();
if (!tsAssert<Discord.Message>(msg)) return;
}
if (!msg.author) {
return logError(
new Error(
"MESSAGE DOES NOT HAVE AUTHOR. This should never happen.",
),
);
}
if (msg.author.id === client.user!.id) {
devlog(`i> ${msg.content}`);
}
const message_like = {
channel: msg.channel,
guild: msg.guild,
member: msg.member,
author: msg.author,
client: msg.client,
content: msg.content,
delete: async () => {
await new Promise(r => setTimeout(r, 10));
await msg.delete();
},
};
const info = new Info(message_like, {
startTime: new Date().getTime(),
infoPerSecond: -1,
raw_message: msg,
});
if (info.db && info.guild && info.raw_message) {
await ticketMessage(msg, info.db);
const autodelete = await info.db.getAutodelete();
for (const rule of autodelete.rules) {
let deleteMsg = false;
if (rule.type === "channel" && msg.channel.id === rule.channel) {
deleteMsg = true;
}else if(rule.type === "textonly" && msg.channel.id === rule.channel) {
if(!msg.author.bot
&& msg.attachments.size === 0
&& msg.components.length === 0
&& msg.embeds.length === 0
&& !(msg.content.includes("http://") || msg.content.includes("https://"))) {
deleteMsg = true;
}
} else if (rule.type === "user" && msg.author.id === rule.user) {
deleteMsg = true;
} else if (
rule.type === "prefix" &&
msg.content.startsWith(rule.prefix)
) {
deleteMsg = true;
} else if (
rule.type === "role" &&
msg.member!.roles.cache.has(rule.role)
) {
deleteMsg = true;
} else if (
rule.type === "counting" &&
msg.channel.id === rule.channel
) {
// TODO counting
} else {
// assertNever(rule);
}
const apply_to_only = rule.apply_roles?.include_only || [];
const exclude_roles = rule.apply_roles?.exclude || [];
if (deleteMsg) {
const member = msg.member!;
// member.fetch(); // this should be auto because they sent a message
const has_exclude = exclude_roles.some(v =>
member.roles.cache.has(v),
);
const has_apto = apply_to_only.some(v =>
member.roles.cache.has(v),
);
if (!has_apto && apply_to_only.length > 0) deleteMsg = false;
if (has_exclude) deleteMsg = false;
}
if (deleteMsg) {
if (typeof rule.duration === "number") {
if (
msg.content.includes("{{DoNotDelete}}") &&
info.authorPerms.manageMessages
) {
// bypass rule
} else {
await queueEvent({
for_guild: info.guild.id,
search: "AUTODELETE:"+info.raw_message.id,
content: {
kind: "delete_message",
channel_id: info.message.channel.id,
message_id: info.raw_message.id,
},
}, rule.duration);
}
} else if (rule.duration.type === "autoreact") {
// TODO
} else if (rule.duration.type === "autopublish") {
await client.rest.post(Routes.channelMessageCrosspost(msg.channelId, msg.id));
}
}
}
}
logMsg({ prefix: "I", msg: msg });
if (shouldIgnore(msg.author)) {
return;
}
try {
await sendPinBottom(info, info.message.channel.id);
} catch (e) {}
// await newInfo.setup(knex)
let commandText: string | undefined;
{
const lcCutContent = msg.content.toLowerCase();
const allPrefixes = [
"<@" + client.user!.id + ">",
"<@!" + client.user!.id + ">",
...(!info.db ? ["ip!"] : ""),
info.db ? await info.db.getPrefix() : "",
];
const matchingPrefix = allPrefixes.find(prefix =>
lcCutContent.startsWith(prefix),
);
if (matchingPrefix != null)
commandText = msg.content.substr(matchingPrefix.length).trim();
}
if (commandText) {
const lcCutContent = commandText.toLowerCase();
logCommand(msg.guild?.id, msg.channel.id, msg.author.bot, msg.author.id, msg.content);
const allCommands = Object.keys(nr.globalCommandNS)
.sort()
.reverse(); // so "ip!a b" comes before "ip!a"
const matchingCommand = allCommands.find(cmd =>
lcCutContent.startsWith(cmd),
);
if (
matchingCommand != null &&
(commandText.substr(matchingCommand.length).trim() !==
commandText.substr(matchingCommand.length) ||
!commandText.substr(matchingCommand.length))
) {
const matchingCommandData = nr.globalCommandNS[matchingCommand];
const matchingCommandArgs = commandText
.substr(matchingCommand.length)
.trim();
matchingCommandData.handler(matchingCommandArgs, info);
} else {
ilt(unknownCommandHandler(commandText, info), "command handler")
.then(result => {
if (result.error) nr.reportError(result.error, info);
})
.catch(e => console.error(e));
}
}
}
// 'edit message' is also emitted when pinned messages update so we can use that instead
client.ws.on(d.GatewayDispatchEvents.MessageCreate, (msg: d.GatewayMessageCreateDispatchData) => {
if(msg.guild_id == null) return;
const guild_id = msg.guild_id;
if(msg.type === d.MessageType.ChannelPinnedMessage) {
if(msg.message_reference != null) {
cancelAllEventsForSearch("AUTODELETE:"+msg.message_reference.message_id).catch(e => {
reportError(guild_id, "CancelPin", e, {msg});
});
}
}
});
client.on("disconnect", () => {
console.log("Bot disconnected!");
});
let handlingCount = 0;
// maybe don't try to limit the max concurrent things
// without putting a timeout limit on how long stuff
// can run for. if anything gets stuck (ie 'await
// new Promise(r => {})'), it will refuse to accept
// any more messages
client.on("messageCreate", msg => {
if (handlingCount > Infinity)
return console.log("Handling too many messages, skipping one.");
perr(
(async () => {
// const myid = msgid++;
handlingCount++;
// console.log(myid + " start (handling " + handlingCount + ")");
try {
await onMessage(msg);
} catch (e) {
reportError(msg.guildId ?? "0", "MessageCreate", e, {content: msg.content, channel_id: msg.channelId, message_id: msg.id});
}
handlingCount--;
// console.log(myid + " done (handling " + handlingCount + ")");
})(),
"on message",
);
});
function channelNameForLog(channel: Discord.TextBasedChannel): string {
if(channel.type === Discord.ChannelType.PrivateThread || channel.type === Discord.ChannelType.PublicThread) {
const parent = channel.parent;
if(parent) {
return "<#"+parent.name+"→"+(channel.type === Discord.ChannelType.PrivateThread ? "[private]" : "")+channel.name+">";
}
// channel.
}
if(channel.type === Discord.ChannelType.DM) {
return "<DM?>";
}
return "<#"+channel.name+">";
}
async function onMessageUpdate(
from: Discord.Message | Discord.PartialMessage,
msg: Discord.Message | Discord.PartialMessage,
) {
if (from.partial || msg.partial) {
//console.log("!! MESSAGE UPDATE HAD A PARTIAL MESSAGE");
return;
}
if (shouldIgnore(msg.author)) {
return;
}
logMsg({ prefix: "Edit From", msg: from });
logMsg({ prefix: "Edit To ", msg: msg });
}
client.on("messageUpdate", (from, msg) => {
perr(onMessageUpdate(from, msg), "message update");
});
const ignoreReactionsOnFrom: { [key: string]: true } = {};
async function rankingMessageReactionAdd(
reaction: Discord.MessageReaction,
user: Discord.User | Discord.PartialUser,
db: Database,
): Promise<boolean> {
let msg = reaction.message;
if (!msg.guild) return false;
const reactor = msg.guild.members.resolve(user.id);
if (!reactor) {
return false;
}
// now there needs to be some filtering system or something
// to ignore most reactions
if (reaction.emoji instanceof Discord.ReactionEmoji) {
return false;
}
const irfKey = msg.guild.id + "|" + msg.id + "|" + user.id;
if (ignoreReactionsOnFrom[irfKey]) return false;
ignoreReactionsOnFrom[irfKey] = true;
const qr = await db.getQuickrank();
const isManager = qr.managerRole && reactor.roles.cache.has(qr.managerRole);
if (!(isManager || reactor.permissions.has("ManageRoles"))) {
delete ignoreReactionsOnFrom[irfKey];
return false; // bad person
}
const hndlr = qr.emojiAlias[reaction.emoji.id];
if (!hndlr) {
delete ignoreReactionsOnFrom[irfKey];
return false;
}
const roleIDs = [hndlr.role];
let rank = false;
const timer = createTimer([
60 * 1000,
async () => {
// timer over.
},
]);
if(msg.partial) msg = await msg.fetch();
const huser = user;
const rxnh = handleReactions(msg, async (gotrx, gotuser) => {
if (huser.id !== gotuser.id) return;
const gotmsg = gotrx.message;
if (!gotmsg.guild) {
return;
}
const gotusr = gotmsg.guild.members.resolve(gotuser);
if (!gotusr) {
return;
}
if (gotrx.emoji instanceof Discord.ReactionEmoji) {
if (gotrx.emoji.name === "✅") {
rank = true;
timer.end();
return;
}
return;
}
const gothndlr = qr.emojiAlias[gotrx.emoji.id];
if (!gothndlr) {
return;
}
roleIDs.push(gothndlr.role);
});
const myreaxn = await msg.react("✅");
await timer.over();
rxnh.end();
delete ignoreReactionsOnFrom[irfKey];
const guild = msg.guild!;
if (!rank) {
await myreaxn.remove();
return true;
}
let member = await msg.guild?.members.fetch(msg.author);
if (!member) {
await msg.channel.send(
reactor.toString() +
", Member " +
msg.author.toString() +
" not found. Are they still on the server?",
);
return true;
}
if(member.id === client.user!.id) {
// oop
// check if in ticket