Skip to content

Commit 00b0e12

Browse files
committed
Fixed a bit more
1 parent 79b9f6a commit 00b0e12

10 files changed

Lines changed: 427 additions & 270 deletions

File tree

.dockerignore

Lines changed: 0 additions & 24 deletions
This file was deleted.

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,6 @@ dist
130130
.pnp.*
131131

132132
/cache
133-
\logs*
133+
\logs*
134+
135+
oldData/

package-lock.json

Lines changed: 270 additions & 221 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dataMover.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import fs from "fs";
2+
import { createUser } from "./models/userSchema";
3+
import path from "path";
4+
5+
interface oldDataFormat {
6+
xp: number;
7+
userID: string;
8+
serverID: string;
9+
lastMessageTimestamp: number | null;
10+
xpTimeoutUntil: number | null;
11+
level: number | null;
12+
colorHexCode: string | null;
13+
reminders: [] | null;
14+
profileFrame: string | null;
15+
exclusiveFrames: [] | null;
16+
xpboost: object | null;
17+
other: object | null;
18+
privateVoiceID: string | null;
19+
privateVoiceThreadID: string | null;
20+
modLogs: [] | null;
21+
discordAuthToken: string | null;
22+
saveToken: string | null;
23+
hasGotMessage: boolean | null;
24+
loveMessage: boolean | null;
25+
hasCheckedInSverok: boolean | null;
26+
paryBotBeta: boolean | null;
27+
paryBotBetaKey: string | null;
28+
minecraftWhiteList: boolean | null;
29+
codeCount: number | null;
30+
votedResult: string | null;
31+
old_messages: [] | null;
32+
extraObjects: object | null;
33+
hashed_email: string | null;
34+
minecraftWhiteListConfirm: boolean | null;
35+
minecraftSecretCode: string | null;
36+
minecraftUsername: string | null;
37+
minecraftUuid: string | null;
38+
}
39+
40+
export const moveData = () => {
41+
const oldDataPath = path.resolve("./oldData/xpsystem.profilemodels.json");
42+
const rawOldData = fs.readFileSync(oldDataPath, "utf-8");
43+
const oldData = JSON.parse(rawOldData) as oldDataFormat[];
44+
let count = 0;
45+
oldData.forEach(async (oldUser) => {
46+
count++;
47+
console.log(`Processing user ${count}/${oldData.length}`);
48+
const userData = await createUser(oldUser.userID);
49+
if (!userData) return;
50+
console.log(`Migrating data for userID: ${oldUser.userID}`);
51+
userData.levelSystem.xp = oldUser.xp || 0;
52+
userData.levelSystem.level = oldUser.level || 0;
53+
userData.levelSystem.lastMessageTimestamp =
54+
oldUser.lastMessageTimestamp || Date.now();
55+
userData.levelSystem.xpTimeoutUntil =
56+
oldUser.xpTimeoutUntil || Date.now();
57+
58+
userData.frameData.frameColorHexCode =
59+
oldUser.colorHexCode || "#787C75";
60+
userData.frameData.selectedFrame = oldUser.profileFrame
61+
? parseInt(oldUser.profileFrame)
62+
: 0;
63+
64+
oldUser.exclusiveFrames = oldUser.exclusiveFrames || [];
65+
userData.frameData.frames =
66+
oldUser.exclusiveFrames && oldUser.exclusiveFrames.length > 0
67+
? userData.frameData.frames.concat(
68+
oldUser.exclusiveFrames as string[],
69+
)
70+
: userData.frameData.frames;
71+
userData.voiceData.voiceChannelId = oldUser.privateVoiceID || null;
72+
userData.voiceData.voiceChannelThreadId =
73+
oldUser.privateVoiceThreadID || null;
74+
75+
if (oldUser.modLogs && oldUser.modLogs.length > 0) {
76+
//eslint-disable-next-line @typescript-eslint/no-explicit-any
77+
const modLogs = oldUser.modLogs.map((log: any) => {
78+
const newLog = {
79+
type: log.type || "unknown",
80+
userId: log.userID || "unknown",
81+
username: log.userName || "unknown",
82+
reason: log.Reason || "No reason provided",
83+
timestamp: new Date(log.date).getTime() || Date.now(),
84+
length: log.length || "permanent",
85+
authorId: log.authorID || "unknown",
86+
};
87+
return newLog;
88+
});
89+
userData.modLogs.push(...modLogs);
90+
}
91+
92+
userData.minecraftData.uuid = oldUser.minecraftUuid || null;
93+
userData.minecraftData.username = oldUser.minecraftUsername || null;
94+
95+
userData.hashedEmail = oldUser.hashed_email || null;
96+
97+
await userData.save();
98+
});
99+
};

src/helpers/generateFrame.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createCanvas, Image, loadImage, registerFont } from "canvas";
22
import path from "path";
33
import { fetchGuildConfig } from "../models/guildSchema";
4+
import fs from "fs";
45

56
export const generateFrame = async (
67
name: string,
@@ -20,16 +21,17 @@ export const generateFrame = async (
2021

2122
if (!frameData) return null;
2223

23-
const framePath = path.resolve("./" + frameData.path);
24-
if (framePath == undefined) return null;
24+
const framePath = path.resolve("./" + frameData.path) || null;
25+
if (!framePath) return null;
2526
const foregroundFramePath = frameData.foregroundPath || null;
2627

2728
const width = 500;
2829
const height = 800;
2930

3031
//INFO: Don't work on windows
31-
registerFont(path.resolve("./graphics/fonts/Sansumu02-Regular.ttf"), {
32-
family: "Sansumu 02",
32+
const fontPath = path.resolve("./graphics/fonts/Sansumu02-Regular.ttf");
33+
registerFont(fontPath, {
34+
family: "Sansumu",
3335
});
3436

3537
const canvas = createCanvas(width, height);
@@ -39,26 +41,30 @@ export const generateFrame = async (
3941
ctx.fillStyle = hexColor;
4042
ctx.fillRect(0, 0, width, height);
4143

44+
const fileBuffer = fs.readFileSync(framePath);
45+
if (!fileBuffer) return null;
46+
4247
//Loads frame
43-
await loadImage(framePath).then((img: Image) =>
48+
await loadImage(fileBuffer).then((img: Image) =>
4449
ctx.drawImage(img, 0, 0, width, height),
4550
);
4651

4752
//loads avatar
4853
if (memberAvatar) {
49-
await loadImage(memberAvatar).then((img: Image) =>
54+
const pngAvatar = memberAvatar.replace(/\.webp(\?.*)?$/, ".png$1");
55+
await loadImage(pngAvatar).then((img: Image) =>
5056
ctx.drawImage(img, width / 2 - 125, 80, 250, 250),
5157
);
5258
}
5359

5460
//writes name
55-
ctx.font = "50pt Sansumu 02";
61+
ctx.font = "50pt Sansumu";
5662
ctx.textAlign = "center";
5763
ctx.fillStyle = "#FFFFFF";
5864
ctx.fillText(name, width / 2, 400);
5965

6066
//writes level
61-
ctx.font = "40pt Sansumu 02";
67+
ctx.font = "40pt Sansumu";
6268
ctx.fillText(`Level: ${level}`, width / 2, 470);
6369

6470
//renders xp bar
@@ -71,7 +77,7 @@ export const generateFrame = async (
7177
roundRect(ctx, 65, 500, bar, 40, 20, true, false);
7278

7379
//writes xp amount
74-
ctx.font = "40pt Sansumu 02";
80+
ctx.font = "40pt Sansumu";
7581
ctx.fillText(`${xpPercentage}%`, width / 2, 600);
7682

7783
//loads foreground frame if there is one

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ app.get("/", (req: Request, res: Response) => {
4848

4949
app.listen(port, async () => {
5050
await startMongoConnection();
51+
//moveData();
5152
logger.info(`Listening on port ${port}`);
5253
});

src/models/configSchema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const levelSystemSchema = new Schema(
2121
const configSchema = new Schema({
2222
id: { type: String, required: true, unique: true },
2323
debug: { type: Boolean, default: false },
24-
levelSystem: { type: levelSystemSchema },
24+
levelSystem: { type: levelSystemSchema, default: () => ({}) },
2525
debugGuildId: { type: String },
2626
extraObjects: {
2727
type: Map,

src/models/guildSchema.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import { toDotNotation } from "../utils/toDoNotation";
44

55
const voiceChannelDataSchema = new Schema(
66
{
7-
voiceChannelId: { type: String },
8-
infoChatId: { type: String },
7+
voiceChannelId: { type: String, default: null },
8+
infoChatId: { type: String, default: null },
99
},
1010
{ _id: false },
1111
);
1212

1313
const ticketDataSchema = new Schema(
1414
{
15-
ticketCategoryId: { type: String },
16-
archivedTicketCategoryId: { type: String },
15+
ticketCategoryId: { type: String, default: null },
16+
archivedTicketCategoryId: { type: String, default: null },
1717
},
1818
{ _id: false },
1919
);
@@ -42,9 +42,9 @@ const frameSchema = new Schema(
4242

4343
const guildConfigSchema = new Schema({
4444
guildId: { type: String, required: true, unique: true },
45-
voiceChannelData: { type: voiceChannelDataSchema },
46-
ticketData: { type: ticketDataSchema },
47-
autoModeration: { type: autoModerationSchema },
45+
voiceChannelData: { type: voiceChannelDataSchema, default: () => ({}) },
46+
ticketData: { type: ticketDataSchema, default: () => ({}) },
47+
autoModeration: { type: autoModerationSchema, default: () => ({}) },
4848
topics: { type: [String] },
4949
noXpChannels: { type: [String], default: [] },
5050
frames: { type: [frameSchema], default: [] },

src/models/userSchema.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,30 @@ const remiderSchema = new Schema(
7070

7171
const userSchema = new Schema({
7272
userId: { type: String, require: true, unique: true },
73-
levelSystem: { type: levelSystemSchema },
74-
frameData: { type: frameDataSchema },
75-
voiceData: { type: voiceDataSchema },
73+
levelSystem: {
74+
type: levelSystemSchema,
75+
default: () => ({
76+
level: 0,
77+
xp: 0,
78+
xpTimeoutUntil: Date.now(),
79+
lastMessageTimestamp: Date.now(),
80+
oldMessages: [],
81+
}),
82+
},
83+
frameData: {
84+
type: frameDataSchema,
85+
default: () => ({
86+
frameColorHexCode: "#787C75",
87+
selectedFrame: 0,
88+
frames: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
89+
}),
90+
},
91+
voiceData: {
92+
type: voiceDataSchema,
93+
default: () => ({}),
94+
},
7695
modLogs: { type: [modLogSchema], default: [] },
77-
minecraftData: { type: minecraftDataSchema },
96+
minecraftData: { type: minecraftDataSchema, default: () => ({}) },
7897
hashedEmail: { type: String },
7998
reminders: { type: [remiderSchema], default: [] },
8099
extraObjects: {

src/public_api/frame.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,13 @@ publicFrameRouter.get("/config", async (req: Request, res: Response) => {
6262
const guildConfig = await fetchGuildConfig("516605157795037185");
6363
if (!guildConfig)
6464
return res.status(500).json({ error: "No guild config found" });
65+
const frames = guildConfig.frames.map((frame) => ({
66+
name: frame.name,
67+
id: frame.id,
68+
frameLink: `https://api.sgc.se/public_api/frame/${frame.id}`,
69+
}));
6570
const frameConfig = {
66-
frames: guildConfig.frames,
71+
frames: frames,
6772
};
6873
return res.json(frameConfig);
6974
});
@@ -81,9 +86,9 @@ publicFrameRouter.get("/:frameId", async (req: Request, res: Response) => {
8186
return res.status(500).json({ error: "No guild config found" });
8287
if (frameId > guildConfig.frames.length - 1)
8388
return res.status(400).json({ error: "Invalid frame ID" });
84-
const frame = guildConfig.frames[frameId];
89+
const frame = guildConfig.frames.find((f) => f.id === frameId);
8590
if (!frame) return res.status(400).json({ error: "No frame with that ID" });
86-
const filePath = path.resolve("./") + frame.path;
91+
const filePath = path.resolve(`./${frame.path}`);
8792
if (!fs.existsSync(filePath))
8893
return res.status(400).json({ error: "No frame with that ID" });
8994
res.download(filePath);

0 commit comments

Comments
 (0)