Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Create GME add endpoint
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
function makeResponse(body: string, status: number): Response { | ||
return new Response(body, { | ||
headers: { | ||
"content-type": "application/json", | ||
}, | ||
status, | ||
}); | ||
} | ||
|
||
export async function onRequestPost(context: RequestContext) { | ||
const { user } = context.data.body; | ||
|
||
if (!user) return makeResponse('{"error":"No user provided"}', 400); | ||
|
||
const existingUser = await context.env.DATA.get(`gamemod_${user}`); | ||
|
||
if (existingUser) | ||
return makeResponse('{"error":"Cannot add an existing user"}', 400); | ||
|
||
if ( | ||
["165594923586945025", "289372404541554689", "396347223736057866"].includes( | ||
user | ||
) | ||
) | ||
return new Response(null, { | ||
status: 204, | ||
}); | ||
|
||
if (!user.match(/^\d{17,19}$/)) | ||
return makeResponse('{"error":"Invalid User ID"}', 400); | ||
|
||
await context.env.DATA.put( | ||
`gamemod_${user}`, | ||
JSON.stringify({ time: Date.now(), user: context.data.current_user.id }) | ||
); | ||
|
||
return new Response(null, { | ||
status: 204, | ||
}); | ||
} |