-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
87 lines (77 loc) · 2.39 KB
/
app.js
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
import 'dotenv/config';
import express from 'express';
import fs from 'fs';
import FormData from 'form-data';
import { InteractionType, InteractionResponseType } from 'discord-interactions';
import { VerifyDiscordRequest } from './utils/requests.js';
import { GenerateImage } from './utils/images.js';
import { GetRandomImage, GetRandomFortune } from './utils/fortune.js';
// Create an express app
const app = express();
// Get port, or default to 3000
const PORT = process.env.PORT || 3000;
// Parse request body and verifies incoming requests using discord-interactions package
app.use(express.json({ verify: VerifyDiscordRequest(process.env.PUBLIC_KEY) }));
/**
* Interactions endpoint URL where Discord will send HTTP requests
*/
app.post('/interactions', async function (req, res) {
// Interaction type and data
const { type, id, data, token, member } = req.body;
/**
* Handle verification requests
*/
if (type === InteractionType.PING) {
return res.send({ type: InteractionResponseType.PONG });
}
/**
* Handle slash command requests
* See https://discord.com/developers/docs/interactions/application-commands#slash-commands
*/
if (type === InteractionType.APPLICATION_COMMAND) {
console.log(data);
// Handle your command here
}
});
/**
* Function to interface with fortune.js and generate the fortune for a request
* @param id - the interaction ID which will be used to create unique filename
*/
async function generateRandomFortune(id) {
const randomImg = GetRandomImage();
const randomFortune = GetRandomFortune();
await GenerateImage(randomImg, randomFortune, `${id}-fortune.png`);
return;
}
/**
* Function to create payload for the embed to send back to Discord\
* @param userId Discord user ID
* @param fileName File name of the image included in the embed
* @param header Text to include at the top of the embed
*/
function buildFortuneEmbed(userId, fileName, header) {
const attachments = [
{
id: 0,
description: `Fortune for <@${userId}>`,
filename: fileName,
},
];
const payload = {
embeds: [
{
description: header,
image: { url: `attachment://${fileName}` },
color: 8226557,
},
],
attachments,
};
return JSON.stringify(payload);
}
/**
* Start express server so we can receive requests from Discord
*/
app.listen(PORT, () => {
console.log('Listening on port', PORT);
});