Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
car-crushers-portal/functions/api/game-appeals/submit.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
75 lines (60 sloc)
2.16 KB
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
import { jsonError } from "../../common.js"; | |
import precheck from "./precheck.js"; | |
export async function onRequestPost(context: RequestContext) { | |
const authHeader = context.request.headers.get("rbx-auth"); | |
if (authHeader !== context.env.ROBLOX_APPEALS_TOKEN) | |
return jsonError("Unauthorized", 401); | |
const { id, reasonForUnban, username, whatHappened } = context.data.body; | |
if ( | |
typeof id !== "number" || | |
typeof reasonForUnban !== "string" || | |
typeof username !== "string" || | |
typeof whatHappened !== "string" | |
) | |
return jsonError("Invalid data", 400); | |
if (!reasonForUnban.length || !whatHappened.length) | |
return jsonError("Missing required fields", 400); | |
if (reasonForUnban.length > 5000 || whatHappened.length > 5000) | |
return jsonError( | |
"The maximum length of each text field is 5000 characters", | |
400, | |
); | |
if (reasonForUnban.length < 100) | |
return jsonError( | |
"Your explanation of why you should be unbanned must be longer", | |
400, | |
); | |
if (whatHappened.length < 50) | |
return jsonError("Your explanation of your ban must be longer", 400); | |
const precheckData = await precheck(context, id); | |
if (precheckData.error) | |
return jsonError("Unable to check your eligibility, try again later", 500); | |
if (!precheckData.can_appeal) | |
return jsonError(precheckData.reason as string, 400); | |
const appealId = `${id}${ | |
context.request.headers.get("cf-ray")?.split("-")[0] | |
}${Date.now()}`; | |
await context.env.D1.prepare( | |
"INSERT INTO game_appeals (created_at, id, reason_for_unban, roblox_id, roblox_username, what_happened) VALUES (?, ?, ?, ?, ?, ?);", | |
) | |
.bind(Date.now(), appealId, reasonForUnban, id, username, whatHappened) | |
.run(); | |
await fetch(context.env.REPORTS_WEBHOOK, { | |
body: JSON.stringify({ | |
embeds: [ | |
{ | |
color: 3756250, | |
description: `${username} has pleaded for forgiveness! Head to https://carcrushers.cc/mod-queue?id=${appealId}&type=gma`, | |
title: "Appeal Submitted", | |
}, | |
], | |
}), | |
headers: { | |
"content-type": "application/json", | |
}, | |
method: "POST", | |
}); | |
return new Response(null, { | |
status: 204, | |
}); | |
} |