-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (79 loc) · 2.48 KB
/
index.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
88
89
90
91
92
93
94
import dotenv from 'dotenv/config';
import express from 'express';
import {
getEmailJustSent,
sendMessageJustSent,
VerifyDiscordRequest
} from './utils.js';
import {
TEST_COMMAND,
HasGuildCommands,
LATEST_COMMAND
} from "./commands.js";
import {
InteractionType,
InteractionResponseType,
} from 'discord-interactions';
const app = express();
app.use('/interactions', express.json({ verify: VerifyDiscordRequest(process.env.PUBLIC_KEY) }));
app.use('/webhooks', express.json());
app.post('/interactions', async function (req, res) {
// Interaction type and data
const { type, id, data } = req.body;
if (type === InteractionType.PING) {
return res.send({ type: InteractionResponseType.PONG });
};
/**
* Handle slash command requests
*/
if (type === InteractionType.APPLICATION_COMMAND) {
const { name } = data;
// "test" guild command
if (name === 'test') {
// Send a message into the channel where command was triggered from
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: 'Hello World!',
},
});
} else if (name === 'latest') {
if (!data.options) {
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
// Setting URL as env variable for now
content: `https://untilitsnotfun.com/posts/${process.env.LATEST_POST}`
},
});
} else {
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
// Setting URL as env variable for now
content: `https://untilitsnotfun.com/posts/${process.env.LATEST_POST}/${data.options[0].value}`
},
});
}
}
}
});
// Handle webhooks sent from Buttondown
app.post('/webhooks', async function (req, res) {
const emailId = req.body.data.email;
const email = await getEmailJustSent(emailId);
const subject = email.subject;
const issueNumber = email.secondary_id;
// Setting URL as env variable
sendMessageJustSent(subject, issueNumber, `https://untilitsnotfun.com/posts/${process.env.NEXT_POST}`)
return res.end()
});
const PORT = process.env.PORT || 3000
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
// Check if guild commands from commands.js are installed (if not, install them)
HasGuildCommands(process.env.APP_ID, process.env.GUILD_ID, [
TEST_COMMAND,
LATEST_COMMAND
]);
});