Add roblox id to et member object
This commit is contained in:
parent
f830de45f7
commit
29724729ed
@ -22,7 +22,7 @@ export async function onRequestDelete(context: RequestContext) {
|
||||
}
|
||||
|
||||
export async function onRequestPost(context: RequestContext) {
|
||||
const { id, name } = context.data.body;
|
||||
const { id, name, roblox_username } = context.data.body;
|
||||
|
||||
if (
|
||||
typeof id !== "string" ||
|
||||
@ -38,6 +38,51 @@ export async function onRequestPost(context: RequestContext) {
|
||||
if (await context.env.DATA.get(`etmember_${id}`))
|
||||
return jsonError("User is already a member", 400);
|
||||
|
||||
if (!["string", "undefined"].includes(typeof roblox_username))
|
||||
return jsonError("Roblox username must be a string", 400);
|
||||
|
||||
let roblox_id: number | undefined = undefined;
|
||||
|
||||
if (roblox_username) {
|
||||
if (
|
||||
roblox_username.length < 3 ||
|
||||
roblox_username.length > 20 ||
|
||||
roblox_username.match(/\D/) ||
|
||||
roblox_username.match(/_/g)?.length > 1
|
||||
)
|
||||
return jsonError("Username is invalid", 400);
|
||||
|
||||
const usernameResolveResp = await fetch(
|
||||
"https://users.roblox.com/v1/usernames/users",
|
||||
{
|
||||
body: JSON.stringify({
|
||||
excludeBannedUsers: true,
|
||||
usernames: [roblox_username],
|
||||
}),
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
method: "POST",
|
||||
},
|
||||
);
|
||||
|
||||
if (!usernameResolveResp.ok) {
|
||||
console.log(await usernameResolveResp.text());
|
||||
return jsonError("An error occurred when looking up that username", 500);
|
||||
}
|
||||
|
||||
const { data }: { data: { id: number }[] } =
|
||||
await usernameResolveResp.json();
|
||||
|
||||
if (!data.length)
|
||||
return jsonError(
|
||||
"No user was found with that name, either they don't exist or they are banned",
|
||||
400,
|
||||
);
|
||||
|
||||
roblox_id = data[0].id;
|
||||
}
|
||||
|
||||
const createdAt = Date.now();
|
||||
const addingUser = context.data.current_user.id;
|
||||
|
||||
@ -47,12 +92,13 @@ export async function onRequestPost(context: RequestContext) {
|
||||
created_at: createdAt,
|
||||
created_by: addingUser,
|
||||
name,
|
||||
roblox_id,
|
||||
}),
|
||||
);
|
||||
await context.env.D1.prepare(
|
||||
"INSERT INTO et_members (created_at, created_by, id, name) VALUES (?, ?, ?, ?);",
|
||||
"INSERT INTO et_members (created_at, created_by, id, name, roblox_id) VALUES (?, ?, ?, ?, ?);",
|
||||
)
|
||||
.bind(createdAt, addingUser, id, name)
|
||||
.bind(createdAt, addingUser, id, name, roblox_id || null)
|
||||
.run();
|
||||
|
||||
return new Response(null, {
|
||||
|
Loading…
x
Reference in New Issue
Block a user