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

Commit acdeb1b

Browse files
committed
error handling and admin settings
1 parent 4fc2af0 commit acdeb1b

10 files changed

Lines changed: 234 additions & 4 deletions

File tree

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export const auth = new Elysia({ prefix: "/auth" }).use(
5050
},
5151
update: {
5252
JWT: JWT,
53+
Endpoints:
54+
DiscordUser.id === process.env.ADMIN_ID!
55+
? AdminPermission
56+
: defaultPermission,
5357
},
5458
create: {
5559
DiscordId: DiscordUser.id,

src/api/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./bot";
22
export * from "./web";
33
export * from "./auth";
4-
export * from "./moderation"
4+
export * from "./moderation"
5+
export * from "./admin";

src/api/web.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export const web = new Elysia({ prefix: "/web" })
66
.get("/", async ({ error }) => {
77
try {
88
return await prisma.user.findMany({
9-
take: 10,
109
orderBy: {
1110
GlobalId: "desc",
1211
},

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import swagger from "@elysiajs/swagger";
22
import { Elysia } from "elysia";
3-
import { bot, web, auth, moderation } from "api";
3+
import { bot, web, auth, moderation, admin } from "api";
44
import { ping, root, profile } from "pages";
55
import {
66
prisma,
@@ -28,6 +28,7 @@ try {
2828
.use(cors(CORSConfig))
2929
.use(rateLimit(rateLimitConfig))
3030
// .use(profile)
31+
.use(admin)
3132
.use(ping)
3233
.use(bot)
3334
.use(web)

src/utils/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export * from "./nexusmods"
33
export * from "./prisma";
44
export * from "./github";
55
export * from './config'
6-
export * from './perms'
6+
export * from './perms'
7+
export * from './tempBot'

src/utils/perms.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,12 @@ export const AdminPermission = {
4040
userId: ["GET"],
4141
},
4242
},
43+
admin: {
44+
update: {
45+
users: ["GET"],
46+
},
47+
import: {
48+
users: ["POST"],
49+
}
50+
}
4351
};

src/utils/tempBot.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Client, Events, GatewayIntentBits } from "discord.js";
2+
3+
const client = new Client({
4+
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildModeration],
5+
});
6+
7+
client.once(Events.ClientReady, () => {
8+
console.log("Client is ready!");
9+
});
10+
11+
client.login(process.env.DISCORD_BOT_TOKEN!).catch((err) => {
12+
console.error("Failed to login to Discord:", err);
13+
});
14+
15+
export { client };

0 commit comments

Comments
 (0)