forked from discord/user-install-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
145 lines (133 loc) · 4.33 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
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
import 'dotenv/config';
import express from 'express';
import { InteractionType, InteractionResponseType } from 'discord-interactions';
import {
VerifyDiscordRequest,
getServerLeaderboard,
createPlayerEmbed,
} from './utils.js';
import { getFakeProfile, getWikiItem } from './game.js';
// Create an express app
const app = express();
// Get port, or default to 8080
const PORT = process.env.PORT || 8080;
// 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, data } = req.body;
/**
* Handle verification requests
*/
if (type === InteractionType.PING) {
return res.send({ type: InteractionResponseType.PONG });
}
// Log request bodies
console.log(req.body);
/**
* Handle slash command requests
* See https://discord.com/developers/docs/interactions/application-commands#slash-commands
*/
if (type === InteractionType.APPLICATION_COMMAND) {
const { name } = data;
// "leaderboard" command
if (name === 'leaderboard') {
// Send a message into the channel where command was triggered from
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: await getServerLeaderboard(req.body.guild.id),
},
});
}
// "profile" command
if (name === 'profile') {
const profile = getFakeProfile(0);
const profileEmbed = createPlayerEmbed(profile);
// Use interaction context that the interaction was triggered from
const interactionContext = req.body.context;
// Construct `data` for our interaction response. The profile embed will be included regardless of interaction context
let profilePayloadData = {
embeds: [profileEmbed],
};
// If profile isn't run in a DM with the app, we'll make the response ephemeral and add a share button
if (interactionContext !== 1) {
// Make message ephemeral
profilePayloadData['flags'] = 64;
// Add button to components
profilePayloadData['components'] = [
{
type: 1,
components: [
{
type: 2,
label: 'Share Profile',
custom_id: 'share_profile',
style: 2,
},
],
},
];
}
// Send response
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: profilePayloadData,
});
}
// "link" command
if (name === 'link') {
// Send a message into the channel where command was triggered from
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content:
'Authorize your Quests of Wumpus account with your Discord profile.',
components: [
{
type: 1,
components: [
{
type: 2,
label: 'Link Account',
style: 5,
// If you were building this functionality, you could guide the user through authorizing via your game/site
url: 'https://discord.com/developers/docs/intro',
},
],
},
],
},
});
}
// "wiki" command
if (name === 'wiki') {
const option = data.options[0];
const selectedItem = getWikiItem(option.value);
// Send a message into the channel where command was triggered from
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `${selectedItem.emoji} **${selectedItem.name}**: ${selectedItem.description}`,
},
});
}
}
// handle button interaction
if (type === InteractionType.MESSAGE_COMPONENT) {
const profile = getFakeProfile(0);
const profileEmbed = createPlayerEmbed(profile);
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
embeds: [profileEmbed],
},
});
}
});
app.listen(PORT, () => {
console.log('Listening on port', PORT);
});