Skip to content
This repository was archived by the owner on Jan 30, 2026. It is now read-only.

Commit 87f29ed

Browse files
authored
Merge pull request #1 from WolvenKit/error_handling
Error handling
2 parents 6f85991 + acdeb1b commit 87f29ed

File tree

12 files changed

+744
-432
lines changed

12 files changed

+744
-432
lines changed

bun.lockb

8.26 KB
Binary file not shown.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"@elysiajs/jwt": "^1.2.0",
1818
"@elysiajs/swagger": "^1.2.2",
1919
"@prisma/client": "^6.6.0",
20+
"discord.js": "^14.19.3",
2021
"elysia": "latest",
2122
"elysia-rate-limit": "^4.3.0",
2223
"octokit": "^4.1.3",

src/api/admin.ts

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
import { Elysia, t } from "elysia";
2+
import { prisma } from "../utils/prisma";
3+
import { bearer } from "@elysiajs/bearer";
4+
import { ValidateAuth } from "src/middleware/auth";
5+
import { NexusQuery, GithubQuery, errorLog, client as DClient } from "../utils";
6+
import type { GuildMember, GuildMemberRoleManager } from "discord.js";
7+
import { Prisma } from "@prisma/client";
8+
9+
export const admin = new Elysia({ prefix: "/admin" }).use(bearer()).guard(
10+
{
11+
beforeHandle({ set, bearer, error, path }) {
12+
return ValidateAuth(set, bearer, error, path, "admin");
13+
},
14+
},
15+
(app) =>
16+
app
17+
.get("/update/users", async ({ error }) => {
18+
try {
19+
const allUsers = await prisma.user.findMany();
20+
21+
if (!allUsers) return error;
22+
23+
allUsers.map(async (user) => {
24+
try {
25+
if (JSON.parse(user.Roles as string) == 0) {
26+
const DiscordUser = (await DClient.guilds.cache
27+
.find((guild) => {
28+
return guild.id === process.env.GUILD_ID;
29+
})
30+
?.members.fetch(user.DiscordiD)) as GuildMember;
31+
32+
const Roles = DiscordUser
33+
? (DiscordUser.roles as GuildMemberRoleManager).cache.map(
34+
(role) => {
35+
return {
36+
id: role.id,
37+
name: role.name,
38+
position: role.position,
39+
rawPosition: role.rawPosition,
40+
icon: role.icon,
41+
iconUrl: role.iconURL(),
42+
};
43+
}
44+
)
45+
: [];
46+
47+
// console.log(Roles);
48+
49+
const update = await prisma.user.update({
50+
where: {
51+
DiscordiD: user.DiscordiD,
52+
},
53+
data: {
54+
Roles: Roles,
55+
},
56+
});
57+
58+
console.log(update);
59+
}
60+
} catch (e) {
61+
errorLog(e);
62+
}
63+
64+
if (
65+
user.Username === null ||
66+
user.Avatar === null ||
67+
user.Roles === null
68+
) {
69+
const DiscordUser = (await DClient.users.fetch(
70+
user.DiscordiD
71+
)) as any;
72+
73+
const Roles = DiscordUser.member
74+
? (
75+
DiscordUser.member.roles as GuildMemberRoleManager
76+
).cache.map((role) => {
77+
return {
78+
id: role.id,
79+
name: role.name,
80+
position: role.position,
81+
rawPosition: role.rawPosition,
82+
icon: role.icon,
83+
iconUrl: role.iconURL(),
84+
};
85+
})
86+
: [];
87+
88+
await prisma.user.update({
89+
where: {
90+
DiscordiD: user.DiscordiD,
91+
},
92+
data: {
93+
Username: DiscordUser.username,
94+
GlobalName: DiscordUser.globalname,
95+
Avatar: DiscordUser.avatar,
96+
Roles: JSON.stringify(Roles),
97+
},
98+
});
99+
}
100+
101+
if (user.GithubUsername && user.Github !== null) {
102+
const Github: any =
103+
(await GithubQuery(user.GithubUsername)) ?? undefined;
104+
105+
await prisma.user.update({
106+
where: {
107+
DiscordiD: user.DiscordiD,
108+
},
109+
data: {
110+
Github: Github,
111+
},
112+
});
113+
}
114+
115+
if (user.NexusModsUsername && user.NexusMods !== null) {
116+
const NexusMods: Record<string, any> | undefined =
117+
await NexusQuery(user.NexusModsUsername);
118+
119+
await prisma.user.update({
120+
where: {
121+
DiscordiD: user.DiscordiD,
122+
},
123+
data: {
124+
NexusMods: NexusMods,
125+
},
126+
});
127+
}
128+
});
129+
130+
return await prisma.user.findMany({
131+
orderBy: {
132+
GlobalId: "desc",
133+
},
134+
});
135+
} catch (e) {
136+
errorLog(e);
137+
return error;
138+
}
139+
})
140+
.post(
141+
"/import/users",
142+
async ({ body, error }) => {
143+
try {
144+
const NexusMods: Record<string, any> | undefined = await NexusQuery(
145+
body.NexusModsUsername
146+
);
147+
148+
const Github: any =
149+
(await GithubQuery(body.GithubUsername)) ?? undefined;
150+
151+
return await prisma.user.upsert({
152+
where: {
153+
DiscordiD: body.DiscordiD,
154+
},
155+
update: {
156+
Username: body.Username,
157+
GlobalName: body.GlobalName,
158+
Theme: body.Theme,
159+
Style: body.Style,
160+
Description: body.Description,
161+
GithubUsername: body.GithubUsername,
162+
NexusModsUsername: body.NexusModsUsername,
163+
NexusMods: NexusMods,
164+
Github: Github,
165+
Roles: body.Roles ?? [],
166+
},
167+
create: {
168+
Username: body.Username,
169+
GlobalName: body.GlobalName,
170+
Theme: body.Theme,
171+
Style: body.Style,
172+
Description: body.Description,
173+
DiscordiD: body.DiscordiD,
174+
GithubUsername: body.GithubUsername,
175+
NexusModsUsername: body.NexusModsUsername,
176+
NexusMods: NexusMods,
177+
Github: Github,
178+
Roles: body.Roles ?? [],
179+
},
180+
});
181+
} catch (e) {
182+
errorLog(e);
183+
return error;
184+
}
185+
},
186+
{
187+
body: t.Object({
188+
Username: t.Union([t.String(), t.Null()]),
189+
GlobalName: t.Union([t.String(), t.Null()]),
190+
DiscordiD: t.String(),
191+
Theme: t.String(),
192+
Style: t.String(),
193+
Description: t.Union([t.String(), t.Null()]),
194+
GithubUsername: t.Union([t.String(), t.Null()]),
195+
NexusModsUsername: t.Union([t.String(), t.Null()]),
196+
Roles: t.Union([t.Array(t.String()), t.Null()]),
197+
}),
198+
}
199+
)
200+
);

src/api/auth.ts

Lines changed: 77 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Elysia, t } from "elysia";
2-
import { prisma } from "../utils";
2+
import { errorLog, prisma } from "../utils";
33
import { jwt } from "@elysiajs/jwt";
44
import { defaultPermission, AdminPermission } from "../utils";
55

@@ -9,79 +9,88 @@ export const auth = new Elysia({ prefix: "/auth" }).use(
99
secret: process.env.JWT_SECRET!,
1010
}).get(
1111
"/redirect",
12-
async ({ jwt, query, cookie: { discord, auth }, redirect }) => {
13-
const Token = (await fetch("https://discord.com/api/oauth2/token", {
14-
method: "POST",
15-
headers: {
16-
"Content-Type": "application/x-www-form-urlencoded",
17-
},
18-
body: new URLSearchParams({
19-
client_id: "796283112199553044",
20-
client_secret: process.env.DISCORD_SECRET!,
21-
grant_type: "authorization_code",
22-
code: query.code,
23-
redirect_uri: `${process.env.LOCAL_URL!}/auth/redirect`,
24-
scope: "identify guilds user email",
25-
}),
26-
}).then((data) => data.json())) as Token;
12+
async ({ jwt, query, cookie: { discord, auth }, redirect, error }) => {
13+
try {
14+
const Token = (await fetch("https://discord.com/api/oauth2/token", {
15+
method: "POST",
16+
headers: {
17+
"Content-Type": "application/x-www-form-urlencoded",
18+
},
19+
body: new URLSearchParams({
20+
client_id: "796283112199553044",
21+
client_secret: process.env.DISCORD_SECRET!,
22+
grant_type: "authorization_code",
23+
code: query.code,
24+
redirect_uri: `${process.env.LOCAL_URL!}/auth/redirect`,
25+
scope: "identify guilds user email",
26+
}),
27+
}).then((data) => data.json())) as Token;
2728

28-
const DiscordUser = (await fetch("https://discord.com/api/users/@me", {
29-
headers: {
30-
Authorization: `Bearer ${Token.access_token}`,
31-
},
32-
}).then((data) => data.json())) as User;
29+
const DiscordUser = (await fetch("https://discord.com/api/users/@me", {
30+
headers: {
31+
Authorization: `Bearer ${Token.access_token}`,
32+
},
33+
}).then((data) => data.json())) as User;
3334

34-
const JWT = await jwt.sign({
35-
id: DiscordUser.id,
36-
username: DiscordUser.username,
37-
});
35+
const JWT = await jwt.sign({
36+
id: DiscordUser.id,
37+
username: DiscordUser.username,
38+
});
3839

39-
auth.set({
40-
value: JWT,
41-
maxAge: Token.expires_in,
42-
path: "/",
43-
});
40+
auth.set({
41+
value: JWT,
42+
maxAge: Token.expires_in,
43+
path: "/",
44+
});
4445

45-
await prisma.$transaction([
46-
prisma.auth.upsert({
47-
where: {
48-
DiscordId: DiscordUser.id,
49-
},
50-
update: {
51-
JWT: JWT,
52-
},
53-
create: {
54-
DiscordId: DiscordUser.id,
55-
JWT: JWT,
56-
Endpoints:
57-
DiscordUser.id === process.env.ADMIN_ID!
58-
? AdminPermission
59-
: defaultPermission,
60-
},
61-
}),
62-
prisma.moderation.upsert({
63-
where: {
64-
DiscordId: DiscordUser.id,
65-
},
66-
update: {
67-
DiscordId: DiscordUser.id,
68-
JWT: auth.value,
69-
},
70-
create: {
71-
DiscordId: DiscordUser.id,
72-
JWT: auth.value,
73-
Resolved: true,
74-
},
75-
}),
76-
]);
46+
await prisma.$transaction([
47+
prisma.auth.upsert({
48+
where: {
49+
DiscordId: DiscordUser.id,
50+
},
51+
update: {
52+
JWT: JWT,
53+
Endpoints:
54+
DiscordUser.id === process.env.ADMIN_ID!
55+
? AdminPermission
56+
: defaultPermission,
57+
},
58+
create: {
59+
DiscordId: DiscordUser.id,
60+
JWT: JWT,
61+
Endpoints:
62+
DiscordUser.id === process.env.ADMIN_ID!
63+
? AdminPermission
64+
: defaultPermission,
65+
},
66+
}),
67+
prisma.moderation.upsert({
68+
where: {
69+
DiscordId: DiscordUser.id,
70+
},
71+
update: {
72+
DiscordId: DiscordUser.id,
73+
JWT: auth.value,
74+
},
75+
create: {
76+
DiscordId: DiscordUser.id,
77+
JWT: auth.value,
78+
Resolved: true,
79+
},
80+
}),
81+
]);
7782

78-
discord.set({
79-
value: JSON.stringify(DiscordUser),
80-
path: "/",
81-
maxAge: Token.expires_in,
82-
});
83+
discord.set({
84+
value: JSON.stringify(DiscordUser),
85+
path: "/",
86+
maxAge: Token.expires_in,
87+
});
8388

84-
return redirect(process.env.REDIRECT!);
89+
return redirect(process.env.REDIRECT!);
90+
} catch (e) {
91+
errorLog(e);
92+
return error(500);
93+
}
8594
},
8695
{
8796
query: t.Object({

0 commit comments

Comments
 (0)