-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
72 lines (66 loc) · 2.36 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
import { App, ExpressReceiver, LogLevel } from '@slack/bolt';
import { registerHandlers } from './handlers';
import { config, detectEnvForChannel } from './lib/env';
import { PrismaClient } from "@prisma/client";
import { env } from 'process';
import { ConsoleLogger } from '@slack/logger';
import { botAdmins, queueChannel } from './lib/constants';
import { sendDM } from './lib/utils';
import("./lib/sentry.js")
// Globals
export const prisma = new PrismaClient();
export const logOps = new ConsoleLogger()
export const slackApp = new App({
token: config.slack.botToken,
appToken: config.slack.appToken,
signingSecret: config.slack.sigSecret,
logLevel: env.LOGOPS_DEBUG !== undefined ? LogLevel.DEBUG : LogLevel.INFO,
socketMode: config.slack.socketMode,
customRoutes: [
{
path: "/",
method: ["GET"],
handler(req, res) {
const redirect = "Redirecting to <a href='https://leeksbot.hackclub.lorebooks.wiki'>docs</a>"
res.writeHead(301, {
"content-length": Buffer.byteLength(redirect),
location: "https://leeksbot.hackclub.lorebooks.wiki"
}).end(redirect)
}
},
{
path: "/ping",
method: ["GET"],
handler(req, res) {
const message = "leeksbot is running here now"
res.statusCode = 200
res.statusMessage = message
res.setHeaders(new Headers({
"Content-Type": "text/plain",
"content-length": Buffer.byteLength(message).toString()
}))
},
}
]
});
registerHandlers(slackApp);
(async () => {
logOps.setLevel(env.LOGOPS_DEBUG !== undefined ? LogLevel.DEBUG : LogLevel.INFO);
logOps.setName("leeksbot");
try {
// connect to db first before bolt.js
await prisma.$connect();
// then do the rest
await slackApp.start({
port: config.port
});
logOps.info("slackAppBase", `⚡️ Bolt app now up and running`);
if (config.slack.socketMode !== true) logOps.info("API server now reachable at port", config.port)
// notify @ajhalili2006 when the bot is up
await sendDM(botAdmins[0], process.env.NODE_ENV == "production" ? "Leeks bot is now online in nest!" : "Leeks bot is now online! (development, apologies for spamming if this annoys you)")
} catch (error) {
console.error('Something went wrong during startup - ', error);
await slackApp.stop()
await prisma.$disconnect()
}
})();