-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
executable file
·211 lines (186 loc) · 5.73 KB
/
app.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
import { Client, Events, GatewayIntentBits } from "discord.js";
import sqlite3 from "better-sqlite3";
import dotenv from "dotenv";
import DiscordHelpersCore from "./lib/helpers/discordHelpers.core";
import { MessageId, UserId } from "./lib/types";
import {
handlePortal,
handleCommands,
handleDeleteMessage,
handleReact,
handleInteraction,
handleMessageUpdate,
} from "./lib/handlers";
import fullSetup from "./lib/setup";
import { MessageEvent, Queue } from "./lib/messageEventClasses";
dotenv.config();
Error.stackTraceLimit = Infinity; //! Remove in production
Error.prepareStackTrace = (err, stack) => {
// Only keep the trace that is part of this file
const trace = stack.filter((call) => call.getFileName() === __filename);
return (
err.name +
": " +
err.message +
" at " +
trace
.map(
(call) =>
call.getFunctionName() +
" (" +
call.getFileName() +
":" +
call.getLineNumber() +
")"
)
.join(" -> ")
);
};
const token = process.env.TOKEN;
// Database
const db = sqlite3("./db.sqlite");
process.on("exit", () => {
db.close();
});
// Prevent crashes
process.on("uncaughtException", (err) => {
console.log("Uncaught exception!");
console.error(err);
});
fullSetup(db);
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessageReactions,
],
});
// Helpers
const helpers = new DiscordHelpersCore(client, db);
// Keep track of setups
const connectionSetups = new Map<
UserId,
{ channelId: string; portalId: string; expires: number }
>();
const portalSetups = new Map<
UserId,
{
name: string;
emoji: string;
customEmoji: boolean;
portalId: string;
channelId: string;
nsfw: boolean;
exclusive: boolean;
password: string;
expires: number;
}
>();
// Enqueue message operations to make sure we don't edit before the webhook has sent
const messageEventQueueMap = new Map<MessageId, Queue<MessageEvent>>();
client.once(Events.ClientReady, (c) => {
console.log(`Ready! Logged in as ${c.user?.tag}`);
});
// Receive messages
client.on(Events.MessageCreate, async (message) => {
// Ignore if webhook
if (message.webhookId) return;
// Ignore if DM
if (!helpers.isGuildMessage(message)) return;
// Ignore if not ValidMessage (implies ValidChannel)
if (!helpers.isValidMessage(message)) return;
// Ignore if self
if (message.author.id === client.user?.id) return;
// Portal functionality
helpers.enqueueMessageEvent(
new MessageEvent(message.id, messageEventQueueMap, async () => {
await handlePortal(message, helpers);
})
);
// Commands
helpers.enqueueMessageEvent(
new MessageEvent(message.id, messageEventQueueMap, async () => {
await handleCommands(message, helpers, connectionSetups);
})
);
});
// Delete messages
client.on(Events.MessageDelete, async (message) => {
// Ignore webhook deletions
if (message.webhookId) return;
helpers.enqueueMessageEvent(
new MessageEvent(message.id, messageEventQueueMap, async () => {
await handleDeleteMessage(message, helpers);
})
);
});
// Edit messages
client.on(Events.MessageUpdate, async (oldMessage, newMessage) => {
// Ignore webhook edits
if (newMessage.webhookId) return;
// Fetch message if partial
const sourceMessage = newMessage.partial
? await newMessage.fetch()
: newMessage;
// Ignore if not valid message
if (!helpers.isValidMessage(sourceMessage)) return;
helpers.enqueueMessageEvent(
new MessageEvent(sourceMessage.id, messageEventQueueMap, async () => {
await handleMessageUpdate(sourceMessage, helpers);
})
);
});
// Channel updates
client.on(Events.ChannelUpdate, (oldChannel, newChannel) => {
// Return if not a valid channel
if (!helpers.isValidChannel(newChannel)) return;
// Check if channel is a portal channel
const portalConnection = helpers.getPortalConnection(newChannel.id);
if (!portalConnection) return;
// Update portal connection
helpers.updatePortalConnection(newChannel.id, {
channelName: newChannel.name,
guildName: newChannel.guild.name,
});
// Check of nsfw change
const portal = helpers.getPortal(portalConnection.portalId);
if (!portal) return;
// Suspend portal if nsfw changed
if (portal.nsfw !== newChannel.nsfw) {
//TODO: Not implemented
}
});
// Guild updates
client.on(Events.GuildUpdate, (oldGuild, newGuild) => {
// Check if guild has any portal connections
const portalConnections = helpers.getGuildPortalConnections(newGuild.id);
if (!portalConnections) return;
// Update portal connections
portalConnections.forEach((portalConnection) => {
helpers.updatePortalConnection(portalConnection.channelId, {
guildName: newGuild.name,
});
});
});
// Reactions
client.on(Events.MessageReactionAdd, async (reaction, user) => {
// Ignore self
if (user.id === client.user?.id) return;
await handleReact(reaction, helpers);
});
client.on(Events.InteractionCreate, async (interaction) => {
try {
await handleInteraction(
interaction,
helpers,
connectionSetups,
portalSetups
);
} catch (err) {
// Probably timed out
console.log("Error in interaction");
console.error(err);
}
});
client.login(token);