This repository has been archived by the owner on May 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
71 lines (66 loc) · 2.79 KB
/
index.ts
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
import {handleInteract} from './handler'
import {verifyKey} from 'discord-interactions'
import {getClientCredentialToken} from "./helpers";
async function handleRequest(event: FetchEvent) {
let url = new URL(event.request.url)
if (url.pathname == '/interactions') {
if (event.request.method != 'POST') {
return new Response('Invalid method', {status: 400})
}
let signature = event.request.headers.get('x-signature-ed25519')
let timestamp = event.request.headers.get('x-signature-timestamp')
let isValidRequest = verifyKey(
await event.request.clone().arrayBuffer(),
signature,
timestamp,
CLIENT_PUB_KEY,
)
if (!isValidRequest) {
return new Response('Bad request signature', {status: 401})
}
let body = await event.request.json()
return await handleInteract(body, event.request)
}
else if (url.pathname == '/install') {
if (event.request.method != 'GET') {
return new Response('Invalid method', {status: 400})
}
// make sure this isn't a public bot if you want to restrict installs
return new Response(null, {headers: {location: `https://discord.com/oauth2/authorize?client_id=${CLIENT_ID}&scope=applications.commands`}, status: 302})
}
else if (url.pathname == '/main_install') {
// not entirely sure about if this needs to be rate limited or behind auth in some fashion; just need an example.
if (event.request.method != 'GET') {
return new Response('Invalid method', {status: 400})
}
let to_url: string;
to_url = `https://discord.com/api/v8/applications/${CLIENT_ID}/commands`;
// uncomment the below and comment the above to quickly register the command on your guild for testing (changing the guild ID in the URL below).
//to_url = `https://discord.com/api/v8/applications/${CLIENT_ID}/guilds/123456789/commands`;
await fetch(to_url, {
body: JSON.stringify({
name: 'echo',
description: 'repeat after me!',
options: [
{
name: 'text',
description: 'what should I be puppeted to say?',
type: 3,
required: true
}
]
}),
method: "POST",
headers: {
'content-type': 'application/json',
'authorization': `Bearer ${(await getClientCredentialToken())}`
}
})
return new Response('done!');
}
return new Response('Not implemented', {status: 501})
}
addEventListener('fetch', (event) => {
// @ts-ignore
event.respondWith(handleRequest(event))
})