-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
63 lines (57 loc) · 2.11 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
const pubkey = Buffer.from(globalThis.DISCORD_PUBKEY || "", "hex")
const DiscordSig = require("./crypto")()
const { Response } = require("node-fetch")
const commands = require("./commands")
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request){
const url = new URL(request.url)
if(request.method == "POST" && url.pathname == "/"){
const timestamp = Buffer.from(request.headers.get("X-Signature-Timestamp") || "")
const sig = Buffer.from(request.headers.get("X-Signature-Ed25519") || "", "hex")
const body = await request.text()
const hashbody = Buffer.concat([timestamp, Buffer.from(body)])
const sig_ok = await DiscordSig.verify(pubkey, sig, hashbody)
if(sig_ok){
const payload = JSON.parse(body)
const response = JSON.stringify(await handlePayload(payload))
console.log(`Command response: ${response}`)
return new Response(response, {headers: {"Content-Type": "application/json"}, status: 200})
}
else {
return new Response("Signature Validation Failed", {status: 401})
}
}
}
async function handlePayload(payload){
console.log(`Got payload type: ${payload.type}`)
switch(payload.type){
case 1: return {type: 1}
case 2:
return {type: 4, data: await handleInteraction(payload)}
}
}
async function handleInteraction(payload) {
const args = {};
(payload.data.options || []).forEach(option => {
args[option.name] = option.value
});
console.log(`Invoke: ${payload.data.name}(${JSON.stringify(args)})`)
const cmd = commands[payload.data.name]
const resp = await (() => {
switch(cmd.length){
case 1:
return cmd(args)
case 2:
return cmd(args, {user: payload.member, channel: payload.channel, guild: payload.guild, id: payload.id})
}
})()
console.log(`Response: ${JSON.stringify(resp)}`)
if (resp.content) {
return resp
}
else{
return { content: resp }
}
}