Greatly reduce repeated code
This commit is contained in:
@ -1,21 +1,13 @@
|
||||
function makeResponse(body: string, status: number): Response {
|
||||
return new Response(body, {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status,
|
||||
});
|
||||
}
|
||||
import { jsonError } from "../../common.js";
|
||||
|
||||
export async function onRequestPost(context: RequestContext) {
|
||||
const { user } = context.data.body;
|
||||
|
||||
if (!user) return makeResponse('{"error":"No user provided"}', 400);
|
||||
if (!user) return jsonError("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 (existingUser) return jsonError("Cannot add an existing user", 400);
|
||||
|
||||
if (
|
||||
["165594923586945025", "289372404541554689", "396347223736057866"].includes(
|
||||
@ -26,8 +18,7 @@ export async function onRequestPost(context: RequestContext) {
|
||||
status: 204,
|
||||
});
|
||||
|
||||
if (!user.match(/^\d{17,19}$/))
|
||||
return makeResponse('{"error":"Invalid User ID"}', 400);
|
||||
if (!user.match(/^\d{17,19}$/)) return jsonError("Invalid User ID", 400);
|
||||
|
||||
const data = { time: Date.now(), user: context.data.current_user.id };
|
||||
|
||||
|
Reference in New Issue
Block a user