-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
440 lines (427 loc) · 13.8 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//third party lib dependencies
import "dotenv/config"
import express from "express"
import NodeCache from "node-cache"
const appCache = new NodeCache()
//These are all imports from sample code, delete unused when done, maybe re-write interaction type since it doesnt work half the time.
import {
InteractionType,
InteractionResponseType,
InteractionResponseFlags,
MessageComponentTypes,
ButtonStyleTypes,
} from "discord-interactions"
import {
verifydiscordRequest,
getRandomEmoji,
discordRequest,
memberAdmin,
notAdminMsg,
testQueue,
} from "./utils.js"
// Imported Faction Methods
import {
createFactions,
printTeams,
assignAllUsers,
assignUser,
unassign,
unassignAll,
flipQueue,
join,
printQueue,
} from "./faction.js"
// Imported Commands
import {
assignRolesWait,
hasGuildCommands,
getGuildMembers,
TEST_COMMAND,
START_FACTION_COMMAND,
JOIN_COMMAND,
RESET_COMMAND,
ASSIGN_COMMAND,
ASSIGN_ALL_COMMAND,
UNASSIGN_COMMAND,
PRINT_FACTIONS_COMMAND,
UNASSIGN_ALL_COMMAND,
PRINT_QUEUE_COMMAND,
START_WAR_COMMAND,
RESET_ROLES_COMMAND,
} from "./commands.js"
// Create an express app
const app = express()
// 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, member } = req.body
//Handle verification requests
const isAdmin = memberAdmin(member)
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) {
const { name, custom_id } = data
// "test" guild command
if (name === "test") {
if (isAdmin) {
console.log(await getGuildMembers(process.env.GUILD_ID))
console.log("pushed test Queue to cache")
// Send a message into the channel where command was triggered from
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
// Fetches a random emoji to send from a helper function
content: "this is a test command",
},
})
} else {
return notAdminMsg(res, InteractionResponseType)
}
}
if (name === "start_faction") {
if (isAdmin) {
console.log(`faction command was run by ${member.user.username}`)
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: "How many factions?",
components: [
{
type: 1,
components: [
{
type: 3,
custom_id: "faction_select",
options: [
{
label: "2 Factions",
value: "2",
},
{
label: "3 Factions",
value: "3",
},
{
label: "4 Factions",
value: "4",
},
],
placeholder: "Choose a number of factions.",
min_values: 1,
max_values: 1,
},
],
},
],
},
})
} else {
return notAdminMsg(res, InteractionResponseType)
}
}
if (name === "reset") {
if (isAdmin) {
console.log(`factions and queue were reset by ${member.user.username}`)
appCache.flushAll()
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
// Fetches a random emoji to send from a helper function
content:
`factions and queue were reset by ${member.user.username}` +
getRandomEmoji(),
},
})
} else {
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
// Fetches a random emoji to send from a helper function
content: "You must be an admin to run that command",
},
})
}
}
if (name === "join") {
let currentQueue = appCache.get("queue")
let newQueue = join(currentQueue, member)
appCache.set("queue", newQueue, 100000)
console.log(appCache.get("queue"))
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
// Fetches a random emoji to send from a helper function
content:
"HA! You're in! Now be ready to join your faction if you get picked. " +
getRandomEmoji(),
flags: 1 << 6, //ephemeral msg flag
},
})
}
if (name === "assign") {
if (isAdmin) {
// assign will assign a specific person to one faction
let currentFactions = appCache.get("factions")
if (currentFactions === undefined) {
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `You must first initiate a faction war by typing /faction and picking how many factions you want`,
},
})
} else {
let viewer = await req.body.data.options[0].value
let targetFaction = await req.body.data.options[1].value
console.log(viewer, targetFaction)
let updatedFactions = assignUser(
viewer,
targetFaction,
currentFactions
)
appCache.set("factions", updatedFactions)
console.log(appCache.get("factions"))
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `**<@${viewer}>has been assigned to team ${targetFaction}**`,
},
})
}
} else {
return notAdminMsg(res, InteractionResponseType)
}
}
if (name === "assign_all") {
if (isAdmin) {
//assign all uses to a random faction
let currentFactions = appCache.get("factions")
let currentQueue = appCache.get("queue")
if (currentQueue && currentFactions) {
let queuedUsers = currentQueue.map((user) => {
return user.user_id
})
let newFactions = assignAllUsers(queuedUsers, currentFactions)
appCache.set("factions", newFactions)
let factionTeamList = printTeams(newFactions)
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `${member.user.username} has assigned all queued users to factions: \n ${factionTeamList}`,
},
})
} else {
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `There is no one in queue to assign or there are factions initiated.`,
},
})
}
} else {
return notAdminMsg(res, InteractionResponseType)
}
}
if (name === "unassign") {
if (isAdmin) {
let viewer = await req.body.data.options[0].value
let factions = appCache.get("factions")
if (factions) {
appCache.set("factions", unassign(viewer, factions))
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `**<@${viewer}> has been unassigned**`,
},
})
} else {
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `You cannot unassign users until you create a faction`,
},
})
}
} else {
return notAdminMsg(res, InteractionResponseType)
}
}
if (name === "unassign_all") {
if (isAdmin) {
let factions = appCache.get("factions")
if (factions) {
appCache.set("factions", unassignAll(factions))
console.log(`${member.user.username} unassigned all users`)
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `${
member.user.username
} unassigned all users: \n ${printTeams(
appCache.get("factions")
)}`,
},
})
} else {
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `You cannot unassign users until you create a faction`,
},
})
}
} else {
return notAdminMsg(res, InteractionResponseType)
}
}
if (name === "print_factions") {
let currentFactions = await appCache.get("factions")
if (currentFactions) {
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: printTeams(currentFactions),
},
})
} else {
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `No factions exist yet to be printed.`,
},
})
}
}
if (name === "print_queue") {
let currentQueue = appCache.get("queue")
if (currentQueue) {
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: printQueue(currentQueue),
},
})
} else {
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `There is currently no queue to print. Ask people to /join!`,
},
})
}
}
if (name === "reset_roles") {
if (isAdmin) {
let currentQueue = appCache.get("queue")
if (currentQueue) {
let userIds = currentQueue.map((user) => user.user_id)
unassignRolesMultiplePeople(process.env.GUILD_ID, userIds)
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `${member.user.username} reset faction roles for all members in queue.`,
},
})
} else {
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `There is no one in queue to unassign roles.`,
},
})
}
} else {
return notAdminMsg(res, InteractionResponseType)
}
}
if (name === "start_war") {
if (isAdmin) {
let currentFactions = appCache.get("factions")
let currentQueue = appCache.get("queue")
if (currentFactions && currentQueue) {
appCache.set("queue", flipQueue(currentQueue, currentFactions))
for await (const faction of currentFactions) {
assignRolesWait(process.env.GUILD_ID, faction.users, faction.roleId)
}
// print all current faction assignments
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `HERE ARE THE FINAL FACTIONS, JOIN YOUR VOICE CHANNEL:\n ${printTeams(
appCache.get("factions")
)}`,
},
})
} else {
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `You must create a faction and have people type /join first.`,
},
})
}
} else {
return notAdminMsg(res, InteractionResponseType)
}
}
}
if (type === 3) {
if (isAdmin) {
// listening for when an admin chooses the number of factions
const { custom_id, values } = data
// creates factions and pushes them to dynamo DB, alerts channel that factions have been created.
if (custom_id === "faction_select") {
console.log(`selected ${values}`)
let factions = createFactions(values)
let factionTeamList = printTeams(factions)
appCache.set("factions", factions, 100000000)
console.log(appCache.get("factions"))
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
// Fetches a random emoji to send from a helper function
content: `**${member.user.username} has created a faction war, type /join to queue up for the war!**\n factions: \n ${factionTeamList}`,
},
})
}
} else {
return notAdminMsg(res, InteractionResponseType)
}
}
})
app.listen(3000, () => {
console.log("Listening on port 3000")
//DELETE SCRIPT
// getCommandsAttributes(process.env.APP_ID, process.env.GUILD_ID, "id").then(
// (res) => {
// DeleteGuildCommands(process.env.APP_ID, process.env.GUILD_ID, res)
// }
// )
// GetScript
// console.log(
// getCommandsAttributes(process.env.APP_ID, process.env.GUILD_ID, "id")
// )
// DeleteGuildCommands(process.env.APP_ID, process.env.GUILD_ID, [
// "974565739816185876",
// ])
//INSTALLATION SCRIPT
hasGuildCommands(process.env.APP_ID, process.env.GUILD_ID, [
TEST_COMMAND,
START_FACTION_COMMAND,
RESET_COMMAND,
JOIN_COMMAND,
ASSIGN_COMMAND,
ASSIGN_ALL_COMMAND,
UNASSIGN_COMMAND,
UNASSIGN_ALL_COMMAND,
PRINT_FACTIONS_COMMAND,
PRINT_QUEUE_COMMAND,
START_WAR_COMMAND,
RESET_ROLES_COMMAND,
])
})