-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
539 lines (521 loc) · 23.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
/** @file Entrypoint for the Discord bot. */
import * as discord from 'discord.js'
import * as chat from './chat'
import * as database from './database'
import * as newtype from './newtype'
import * as schema from './schema'
import CONFIG from './config.json' assert { type: 'json' }
// =================
// === Constants ===
// =================
/** The maximum number of messages to fetch when opening a new thread. */
const MESSAGE_HISTORY_LENGTH = 25
/** The port on which the WebSocket server will be started. */
const WEBSOCKET_PORT = CONFIG.websocketPort
// ===========
// === Bot ===
// ===========
// These should not be methods, to help inlining.
function getMessageId(message: discord.Message | discord.PartialMessage) {
return newtype.asNewtype<schema.MessageId>(message.id)
}
function getMessageThreadId(message: discord.Message | discord.PartialMessage) {
return newtype.asNewtype<schema.ThreadId>(message.channelId)
}
function getMessageAuthorId(message: discord.Message) {
return newtype.asNewtype<schema.DiscordUserId>(message.author.id)
}
function getThreadId(thread: discord.ThreadChannel) {
return newtype.asNewtype<schema.ThreadId>(thread.id)
}
class Bot {
private readonly client = new discord.Client({
intents: [
/** This is required to get guild info, which is required for listening to all
* threads. */
discord.GatewayIntentBits.Guilds,
discord.GatewayIntentBits.GuildMessages,
/* Required for registering staff. */
discord.GatewayIntentBits.DirectMessages,
discord.GatewayIntentBits.MessageContent,
],
partials: [discord.Partials.Channel],
})
private readonly db: database.Database
private threads: Record<
string,
discord.PrivateThreadChannel | discord.PublicThreadChannel | null
> = {}
private guild!: discord.Guild
private channel!: discord.TextChannel
private webhook!: discord.Webhook
// This is a parameter property, so it is only ever shadowed in the constructor.
// eslint-disable-next-line @typescript-eslint/no-shadow
constructor(private readonly config: typeof CONFIG, private readonly chat: chat.Chat) {
this.db = new database.Database(config.storageLocation)
}
/** This MUST be called and awaited before doing anything with the bot. */
async start() {
this.client.on(discord.Events.MessageCreate, async message => {
try {
await this.onDiscordMessageCreate(message)
} catch (error) {
console.error(error)
}
})
this.client.on(discord.Events.MessageUpdate, async (oldMessage, newMessage) => {
try {
await this.onDiscordMessageUpdate(oldMessage, newMessage)
} catch (error) {
console.error(error)
}
})
// Consider handling thread delete events as well.
await this.client.login(this.config.discordToken)
this.chat.onMessage(async (userId, message) => {
try {
await this.onMessage(userId, message)
} catch (error) {
console.error(error)
}
})
this.guild = await this.client.guilds.fetch({ guild: CONFIG.discordServerId, force: true })
const channelId = this.config.discordChannelId
const channel = await this.client.channels.fetch(channelId)
if (channel == null) {
throw new Error(`No channel with ID '${channelId}' exists.`)
} else if (channel.type !== discord.ChannelType.GuildText) {
throw new Error(`The channel with ID '${channelId}' is not a guild text channel.`)
} else {
this.channel = channel
let webhook = (await channel.fetchWebhooks()).find(
fetchedWebhook => fetchedWebhook.token != null
)
if (webhook == null) {
webhook = await channel.createWebhook({ name: 'Enso Support' })
}
this.webhook = webhook
return
}
}
async getThread(threadId: chat.ThreadId) {
if (threadId in this.threads) {
return this.threads[threadId] ?? null
} else {
const thread = await this.client.channels.fetch(threadId)
let result: discord.PrivateThreadChannel | discord.PublicThreadChannel | null
if (thread == null) {
// May cause a race condition. Testing may be needed.
console.error(`No thread with ID '${threadId}' exists.`)
result = null
} else if (!thread.isThread()) {
console.error(`The channel with ID '${threadId}' is not a thread.`)
result = null
} else if (!thread.isTextBased()) {
console.error(`The channel with ID '${threadId}' is not a text channel.`)
result = null
} else {
result = thread
}
this.threads[threadId] = result
return result
}
}
async onDiscordMessageCreate(message: discord.Message) {
const threadId = getMessageThreadId(message)
if (message.author.bot || message.author.system) {
// Ignore automated messages.
} else if (
message.channel.isThread() &&
this.db.hasThread(threadId) &&
message.type === discord.MessageType.Default
) {
const authorId = getMessageAuthorId(message)
let staff = this.db.getUserByDiscordId(authorId)
if (staff == null) {
await message.delete()
const author = await this.client.users.fetch(authorId)
await author.send(
`You are not registered with ${CONFIG.botName}.\n` +
`Please send a message with your full name and profile picture.\n` +
`Note that the picture will be stretched into a square and cropped ` +
`to a circle on the client side.`
)
// This is fine, as it is an exceptional situation.
// eslint-disable-next-line no-restricted-syntax
return
}
const messageId = getMessageId(message)
this.db.createMessage({
discordMessageId: messageId,
discordThreadId: getMessageThreadId(message),
discordAuthorId: getMessageAuthorId(message),
content: message.content,
createdAt: message.createdTimestamp,
editedAt: message.createdTimestamp,
})
const thread = this.db.updateThread(threadId, oldThread => ({
...oldThread,
lastMessageSentId: getMessageId(message),
}))
const user = this.db.getUser(thread.userId)
if (threadId === user.currentThreadId) {
await chat.Chat.default(WEBSOCKET_PORT).send(thread.userId, {
type: chat.ChatMessageDataType.serverMessage,
id: newtype.asNewtype<schema.MessageId>(message.id),
authorAvatar: staff.avatarUrl,
authorName: staff.name,
content: message.content,
reactions: [],
timestamp: message.createdTimestamp,
editedTimestamp: null,
})
}
} else if (message.channel.isDMBased() && message.type === discord.MessageType.Default) {
const guildUser = await this.guild.members.fetch(getMessageAuthorId(message))
if (guildUser.roles.cache.has(CONFIG.discordStaffRoleId)) {
const avatar = message.attachments.at(0)
if (message.attachments.size !== 1 || avatar == null) {
await message.channel.send('You must upload exactly one photo for your avatar.')
} else if (/^\s*$/.test(message.content)) {
await message.channel.send('You must send your full name with your image.')
} else if (message.content.includes('\n')) {
await message.channel.send('Your name must not span multiple names.')
} else {
const authorId = getMessageAuthorId(message)
const userId: string = authorId
const dbUserId = newtype.asNewtype<schema.UserId>(userId)
if (this.db.getUserByDiscordId(authorId) == null) {
// This is a new user.
this.db.createUser({
id: dbUserId,
discordId: authorId,
email: null,
name: message.content,
avatarUrl: avatar.proxyURL,
currentThreadId: null,
})
await message.channel.send('Created user profile.')
} else {
this.db.updateUser(dbUserId, user => ({
...user,
name: message.content,
avatarUrl: avatar.proxyURL,
}))
// This is an existing user updating their name and/or profile picture.
await message.channel.send('Updated user profile.')
}
}
}
}
}
async onDiscordMessageUpdate(
_oldMessage: discord.Message | discord.PartialMessage,
newMessage: discord.Message | discord.PartialMessage
) {
const threadId = getMessageThreadId(newMessage)
if (newMessage.channel.isThread() && this.db.hasThread(threadId)) {
const messageId = getMessageId(newMessage)
this.db.updateMessage(messageId, message => ({
...message,
// This will never be `null` as this event is always emitted with an edited message.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
editedAt: newMessage.editedTimestamp!,
}))
if (newMessage.content == null) {
console.error(
`The content of the updated message with ID '${newMessage.id}' is 'null'.`
)
} else if (newMessage.editedTimestamp == null) {
console.error(
`The timestamp of the updated message with ID '${newMessage.id}' is 'null'.`
)
} else {
const thread = this.db.getThread(threadId)
const user = this.db.getUser(thread.userId)
if (threadId === user.currentThreadId) {
await chat.Chat.default(WEBSOCKET_PORT).send(thread.userId, {
type: chat.ChatMessageDataType.serverEditedMessage,
timestamp: newMessage.editedTimestamp,
id: newtype.asNewtype<schema.MessageId>(newMessage.id),
content: newMessage.content,
})
}
}
}
}
async sendThread(
userId: schema.UserId,
threadId: schema.ThreadId | null,
requestType: chat.ChatServerThreadRequestType,
getBefore: schema.MessageId | null
) {
if (threadId != null) {
const thread = this.db.getThread(threadId)
const messages = this.db.getThreadLastMessages(
threadId,
MESSAGE_HISTORY_LENGTH + 1,
getBefore
)
const firstMessage = messages[0]
const lastMessage = messages[messages.length - 1]
const reactions =
firstMessage != null && lastMessage != null
? this.db
.getReactions(
threadId,
firstMessage.discordMessageId,
lastMessage.discordMessageId
)
.reduce<Record<schema.MessageId, database.ReactionSymbol[]>>(
(mapping, reaction) => {
const reactionsForMessage = (mapping[reaction.discordMessageId] =
mapping[reaction.discordMessageId] ?? [])
reactionsForMessage.push(reaction.reaction)
return mapping
},
{}
)
: {}
const isAtBeginning = messages.length <= MESSAGE_HISTORY_LENGTH
await this.chat.send(userId, {
type: chat.ChatMessageDataType.serverThread,
requestType,
id: thread.discordThreadId,
title: thread.title,
isAtBeginning,
messages: messages
.slice(-MESSAGE_HISTORY_LENGTH)
.flatMap(
(
dbMessage
): (
| chat.ChatServerMessageMessageData
| chat.ChatServerReplayedMessageMessageData
)[] => {
if (dbMessage.discordAuthorId == null) {
// It is a message from the user to staff.
const message: chat.ChatServerReplayedMessageMessageData = {
type: chat.ChatMessageDataType.serverReplayedMessage,
id: dbMessage.discordMessageId,
content: dbMessage.content,
timestamp: dbMessage.createdAt,
}
return [message]
} else {
let staff = this.db.getUserByDiscordId(dbMessage.discordAuthorId)
if (staff == null) {
// This should never happen, as staff messages are deleted
// if the staff member is not registered.
return []
} else {
const message: chat.ChatServerMessageMessageData = {
type: chat.ChatMessageDataType.serverMessage,
id: dbMessage.discordMessageId,
content: dbMessage.content,
reactions: reactions[dbMessage.discordMessageId] ?? [],
authorAvatar: staff.avatarUrl,
authorName: staff.name,
timestamp: dbMessage.createdAt,
editedTimestamp:
dbMessage.editedAt !== dbMessage.createdAt
? dbMessage.editedAt
: null,
}
return [message]
}
}
}
),
})
}
}
async onMessage(
userId: schema.UserId,
message: chat.ChatClientMessageData | chat.ChatInternalMessageData
) {
switch (message.type) {
case chat.ChatMessageDataType.internalAuthenticate: {
if (!this.db.hasUser(message.userId)) {
this.db.createUser({
id: message.userId,
discordId: null,
email: null,
name: message.userName,
avatarUrl: null,
currentThreadId: null,
})
}
break
}
case chat.ChatMessageDataType.internalAuthenticateAnonymously: {
if (!this.db.hasUser(message.userId)) {
this.db.createUser({
id: message.userId,
discordId: null,
email: message.email,
name: `${message.email}`,
avatarUrl: null,
currentThreadId: null,
})
}
break
}
case chat.ChatMessageDataType.authenticate: {
const threads = this.db.getUserThreads(userId)
await this.chat.send(userId, {
type: chat.ChatMessageDataType.serverThreads,
threads: threads.map(thread => ({
id: thread.discordThreadId,
title: thread.title,
hasUnreadMessages: thread.lastMessageReadId !== thread.lastMessageSentId,
})),
})
await this.sendThread(
userId,
this.db.getUser(userId).currentThreadId,
chat.ChatMessageDataType.authenticate,
null
)
break
}
case chat.ChatMessageDataType.authenticateAnonymously: {
// Do not send any message history.
break
}
case chat.ChatMessageDataType.historyBefore: {
const user = this.db.getUser(userId)
if (user.email) break // The user is chatting from the website, which does not support history.
await this.sendThread(
userId,
this.db.getUser(userId).currentThreadId,
message.type,
message.messageId
)
break
}
case chat.ChatMessageDataType.newThread: {
const thread = await this.channel.threads.create({ name: message.title })
const user = this.db.updateUser(userId, oldUser => ({
...oldUser,
currentThreadId: getThreadId(thread),
}))
const discordMessage = await this.webhook.send({
threadId: thread.id,
username: user.name,
...(user.avatarUrl != null ? { avatarURL: user.avatarUrl } : {}),
content: message.content,
})
const messageId = getMessageId(discordMessage)
const threadId = getMessageThreadId(discordMessage)
this.db.createThread({
discordThreadId: getThreadId(thread),
userId: userId,
title: message.title,
lastMessageSentId: messageId,
lastMessageReadId: messageId,
})
this.db.createMessage({
discordMessageId: messageId,
discordThreadId: threadId,
// The user using the web frontend is the author of this message.
discordAuthorId: null,
content: discordMessage.content,
createdAt: discordMessage.createdTimestamp,
editedAt: discordMessage.createdTimestamp,
})
await chat.Chat.default(WEBSOCKET_PORT).send(user.id, {
type: chat.ChatMessageDataType.serverThread,
requestType: chat.ChatMessageDataType.newThread,
id: threadId,
title: message.title,
isAtBeginning: true,
messages: [
{
type: chat.ChatMessageDataType.serverReplayedMessage,
id: messageId,
content: message.content,
timestamp: discordMessage.createdTimestamp,
},
],
})
break
}
case chat.ChatMessageDataType.renameThread: {
const thread = await this.getThread(message.threadId)
if (thread != null) {
await thread.setName(message.title)
this.db.updateThread(message.threadId, oldThread => ({
...oldThread,
title: message.title,
}))
}
break
}
case chat.ChatMessageDataType.switchThread: {
this.db.updateUser(userId, user => ({ ...user, currentThreadId: message.threadId }))
await this.sendThread(userId, message.threadId, message.type, null)
break
}
case chat.ChatMessageDataType.message: {
const thread = await this.getThread(message.threadId)
const user = this.db.getUser(userId)
if (thread != null) {
const newMessage = await this.webhook.send({
threadId: message.threadId,
username: user.name,
...(user.avatarUrl != null ? { avatarURL: user.avatarUrl } : {}),
content: message.content,
})
this.db.createMessage({
discordMessageId: getMessageId(newMessage),
discordThreadId: getMessageThreadId(newMessage),
// The user using the web frontend is the author of this message.
discordAuthorId: null,
content: newMessage.content,
createdAt: newMessage.createdTimestamp,
editedAt: newMessage.createdTimestamp,
})
}
break
}
case chat.ChatMessageDataType.reaction:
case chat.ChatMessageDataType.removeReaction: {
const threadId = this.db.getUser(userId).currentThreadId
const thread = threadId != null ? await this.getThread(threadId) : null
if (thread != null) {
const discordMessage = await thread.messages.fetch(message.messageId)
const isAdding = message.type === chat.ChatMessageDataType.reaction
if (isAdding) {
await discordMessage.react(message.reaction)
this.db.createReaction({
discordMessageId: getMessageId(discordMessage),
reaction: message.reaction,
})
} else {
const botId = this.client.user?.id
if (botId != null) {
await discordMessage.reactions
.resolve(message.reaction)
?.users.remove(botId)
}
this.db.deleteReaction({
discordMessageId: getMessageId(discordMessage),
reaction: message.reaction,
})
}
}
break
}
case chat.ChatMessageDataType.markAsRead: {
this.db.updateThread(message.threadId, thread => ({
...thread,
lastMessageReadId: message.messageId,
}))
break
}
}
}
}
const BOT = new Bot(CONFIG, chat.Chat.default(WEBSOCKET_PORT))
void BOT.start()