-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
72 lines (63 loc) · 2.33 KB
/
Copy pathapp.js
File metadata and controls
72 lines (63 loc) · 2.33 KB
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 express from 'express';
import {InteractionResponseType, InteractionType, verifyKeyMiddleware,} from 'discord-interactions';
import {ThemeManager} from "./src/ThemeManager.js";
import {Commands} from "./src/Commands.js";
import {ColorManager} from "./src/ColorManager.js";
import {ImageGenerator} from "./src/ImageGenerator.js";
process.title = "BrickBot";
// Create an express app
const app = express();
// Get port, or default to 3000
const PORT = process.env.PORT || 3000;
console.log(`${Object.keys(Commands).length} commands found: ${Object.keys(Commands).join(', ')}.`);
await Promise.all([
ColorManager.load(process.env.REBRICKABLE_KEY),
ThemeManager.load(process.env.REBRICKABLE_KEY),
ImageGenerator.init(),
]).then(([colors, themes, images]) => {
console.log(`Loaded ${colors.count} colors. My favourite is ${colors.random().name}`);
console.log(`Loaded ${themes.count} themes. My favourite is ${themes.getByID(themes.randomID()).name}`);
});
app.get('/healthcheck', (req, res) => {
res.send("All good!");
});
/**
* Interactions endpoint URL where Discord will send HTTP requests
* Parse request body and verifies incoming requests using discord-interactions package
*/
app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), async function (req, res) {
try {
// Interaction id, type and data
const { id, type, data } = req.body;
/**
* Handle verification requests
*/
if (type === InteractionType.PING) {
return res.send({ type: InteractionResponseType.PONG });
}
const { name } = data;
const command = Commands[name];
if (command) {
switch (type) {
case InteractionType.APPLICATION_COMMAND:
return command.run(req, res, data);
case InteractionType.APPLICATION_COMMAND_AUTOCOMPLETE:
return command.autocomplete(req, res, data);
default:
console.error(`unknown interaction type: ${type}`);
return res.status(400).json({ error: `unknown interaction type: ${type}` });
}
}
console.error(`unknown command: "${name}"`);
return res.status(400).json({ error: `unknown command: "${name}"` });
} catch (err) {
console.error("Unhandled exception!", err);
try {
return res.sendStatus(500);
} catch (_) {
}
}
});
app.listen(PORT, () => {
console.log('Listening on port', PORT);
});