-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
262 lines (249 loc) · 6.77 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
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
const corsHeaders = {
'Access-Control-Allow-Methods': 'POST,OPTIONS',
'Access-Control-Allow-Origin': `${ORIGIN_URL}`,
'Access-Control-Max-Age': '86400',
}
/**
* readRequestBody reads in the incoming request body
* Use await readRequestBody(..) in an async function to get the string
* @param {Request} request the incoming request to read from
*/
async function readRequestBody(request) {
const { headers } = request
const contentType = headers.get('content-type') || ''
if (contentType.includes('application/json')) {
return JSON.stringify(await request.json())
} else if (contentType.includes('application/text')) {
return request.text()
} else if (contentType.includes('text/html')) {
return request.text()
} else if (contentType.includes('form')) {
const formData = await request.formData()
const body = {}
for (const entry of formData.entries()) {
body[entry[0]] = entry[1]
}
return JSON.stringify(body)
} else {
// Perhaps some other type of data was submitted in the form
// like an image, or some other binary data.
return 'a file'
}
}
/**
* Inserts the application into a google sheet. The sheet is behind nocodeapi.com due to rate limiting and ease of use.
* @param {Object} body
* @returns {Bool} if the insertion was successful.
*/
async function insertIntoSheet(body) {
try {
const response = await fetch(`${NOCODEAPI_URL}`, {
method: 'post',
body: JSON.stringify([JSON.parse(body)]),
headers: {
'Content-Type': 'application/json',
},
})
const json = await response.json()
if (json.message === 'Successfully Inserted') {
return true
}
} catch (error) {
return false
}
return false
}
/**
* This function sends a discord embed to the provided webhook url
* @param {Object} body incoming POST body containing the application details
*/
async function sendDiscordEmbed(body) {
const [
accName,
discordName,
mainGuild,
apiKey,
kpLink,
requirements,
powerBuilds,
condiBuilds,
soloLogs,
soloTimes,
soloFindMember,
experience,
whyJoin,
] = JSON.parse(body)
// Create the Discord Embed
const fields = [
{
name: 'Account',
value: accName,
inline: true, // consecutive inline elements go into the same line
},
{
name: 'Discord',
value: discordName,
inline: true,
},
{
name: 'Main Guild',
value: mainGuild,
inline: true,
},
{
name: '\u200B', // vertical space between fields
value: '\u200B',
},
{
name: 'API Key',
value: '[' + apiKey + '](https://gw2efficiency.com/user/api-keys)',
inline: true,
},
]
fields.push({
name: 'Killproof.me',
value: '[Click me](https://killproof.me/proof/' + accName + ')',
inline: true,
})
fields.push({
name: '\u200B', // vertical space between fields
value: '\u200B',
})
fields.push({
name: 'Power Builds',
value: powerBuilds.replace(/,/g, ', '),
inline: true,
})
fields.push({
name: 'Condi Builds',
value: condiBuilds.replace(/,/g, ', '),
inline: true,
})
fields.push(
{
name: 'Logs',
value: soloLogs,
},
{
name: 'Playtimes',
value: soloTimes,
},
{
name: 'Who do you play with?',
value: soloFindMember.replace(/,/g, ', '),
inline: true,
},
)
fields.push(
{
name: 'Experience?',
value: experience,
},
{
name: 'Why do you want to join us?',
value: whyJoin,
},
)
const payload = {
// content = message the embed will be attached to (up to 2000 chars)
content:
'<@&730372255758155837> <:dTpepedFeelsamazingman:549285673899786251>', // mention Trial Runner role
embeds: [
{
title: 'New Application!',
thumbnail: {
// dT Logo (displayed top right of embed)
url: 'https://cdn.discordapp.com/attachments/765177472836435979/831614589909205042/logo.png',
},
fields,
timestamp: new Date(),
footer: {
text: 'I made this :)',
},
},
],
}
try {
// eslint-disable-next-line no-unused-vars
await fetch(
// webhook link (Trial-Alert channel)
`${DISCORD_WEBHOOK}`,
{
method: 'post',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
},
) // there is no response to await here, just hope it worked :)
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error:', error)
}
}
async function handlePostRequest(event) {
const body = await readRequestBody(event.request)
await sendDiscordEmbed(body)
// When we came this far, nothing went fundamentally wrong. In the worst case the discord embed failed somehow, which is not a reason to tell the client the application failed
return new Response(JSON.stringify({ status: 'SUCCESS' }, null, 2), {
headers: {
'Content-Type': 'application/json;charset=UTF-8',
...corsHeaders,
},
})
}
function handleOptions(request) {
// Make sure the necessary headers are present
// for this to be a valid pre-flight request
let headers = request.headers
if (
headers.get('Origin') !== null &&
headers.get('Access-Control-Request-Method') !== null &&
headers.get('Access-Control-Request-Headers') !== null
) {
// Handle CORS pre-flight request.
// If you want to check or reject the requested method + headers
// you can do that here.
let respHeaders = {
...corsHeaders,
// Allow all future content Request headers to go back to browser
// such as Authorization (Bearer) or X-Client-Name-Version
'Access-Control-Allow-Headers': request.headers.get(
'Access-Control-Request-Headers',
),
}
return new Response(null, {
headers: respHeaders,
})
} else {
// Handle standard OPTIONS request.
// If you want to allow other HTTP Methods, you can do that here.
return new Response(null, {
headers: {
Allow: 'GET, HEAD, POST, OPTIONS',
},
})
}
}
// this here is the entry point
addEventListener('fetch', (event) => {
try {
const request = event.request
if (request.headers.get('Origin') === `${ORIGIN_URL}`) {
if (request.method.toUpperCase() === 'OPTIONS') {
// Handle CORS preflight requests
return event.respondWith(handleOptions(request))
} else if (request.method.toUpperCase() === 'POST')
return event.respondWith(handlePostRequest(event))
} else {
event.respondWith(
new Response(null, {
status: 405,
statusText: 'Method Not Allowed',
}),
)
}
} catch (e) {
return event.respondWith(new Response('Error thrown ' + e.message))
}
})