-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.ts
More file actions
88 lines (78 loc) · 3.36 KB
/
Main.ts
File metadata and controls
88 lines (78 loc) · 3.36 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import "temporal-polyfill"
import { verifyKeyMiddleware } from "discord-interactions"
import express, { type Response } from "express"
import { GetCommandOptions, ParseCommandString, ParseUserId } from "./Discord/pure.ts"
import {
ApplicationCommandOptionType,
ApplicationCommandType,
InteractionContextType,
InteractionResponseType,
Interaction,
InteractionType,
InteractionDataOption,
} from "./Discord/types.ts"
import { Flow, Option, Pipe, S } from "./Lib/pure.ts"
import * as Handle from "./Respawn_Window/handler.ts"
import { Config } from "./env.ts"
import "./Respawn_Window/Reminder.handler.ts"
const router = express()
const parseFirstSelectOption = (interaction: Interaction.MessageComponent) => Pipe(
Option.some(interaction.data),
Option.flatMapNullable(x => x.values),
Option.flatMapNullable(xs => xs[0]),
)
const onCommand = async (res: Response, interaction: Interaction.ApplicationCommand): Promise<Response> => {
switch (interaction.data.name) {
case "clear":
return Handle.Clear(interaction).then(x => res.send(x))
case "description":
return Handle.Description(interaction).then(x => res.send(x))
case "emoji":
return Handle.Emoji(interaction).then(x => res.send(x))
case "kill":
return Handle.Kill(interaction).then(x => res.send(x))
case "timezone":
return Handle.TimeZone(interaction).then(x => res.send(x))
default:
console.error("unknown command", interaction.data.name)
return res.status(400).json({ error: "unknown command" })
}
}
const onCommandAutocomplete = (res: Response, interaction: Interaction.ApplicationCommandAutocomplete): Response =>
res.status(400).json({ error: "unknown autocompletion" })
const onMessage = (res: Response, interaction: Interaction.MessageComponent): Response => {
const messageId = interaction.message.id
// interaction.data.custom_id
const userId = ParseUserId(interaction)
const option = parseFirstSelectOption(interaction)
// void HttpDelete(`webhooks/${Config.APP_ID}/${interaction.token}/messages/${messageId}`)
return res.status(400).json({ error: "Message handlers not implemented" })
}
const onModalSubmit = (res: Response, interaction: Interaction.ModalSubmit): Response =>
res.status(400).json({ error: "unknown modal submit" })
router.post("/interactions", verifyKeyMiddleware(Config.PUBLIC_KEY), async (req, res): Promise<Response> => {
const interaction = S.Validate(Interaction.Interaction, req.body)
switch (interaction.type) {
case InteractionType.PING:
// Authentication short-circuits on pings
// https://github.com/discord/discord-interactions-js/blob/main/src/index.ts
console.error("Unexpected ping", JSON.stringify(interaction))
return res.send({ type: InteractionResponseType.PONG })
case InteractionType.APPLICATION_COMMAND:
return onCommand(res, interaction)
case InteractionType.APPLICATION_COMMAND_AUTOCOMPLETE:
return onCommandAutocomplete(res, interaction)
case InteractionType.MESSAGE_COMPONENT:
return onMessage(res, interaction)
case InteractionType.MODAL_SUBMIT:
return onModalSubmit(res, interaction)
default:
interaction satisfies never
console.error("unknown interaction type", (interaction as Interaction.Interaction).type)
return res.status(400).json({ error: "unknown interaction type" })
}
})
router.listen(Config.PORT, "0.0.0.0", (e) => {
if (e) { console.log("Startup error", e.message) }
console.log("Listening on port " + Config.PORT, Config.PORT)
});