Initial code

This commit is contained in:
Aslan 2025-12-23 07:18:10 -05:00
commit 1f20a611da
26 changed files with 1050 additions and 0 deletions

51
src/helpers.ts Normal file
View file

@ -0,0 +1,51 @@
import { config } from "./config.js";
import { state } from "./store/store.js";
import { type IUser, type TRole } from "./store/types.js";
import type { IRank } from "./types.js";
const getUserById = (userId: string): IUser => {
return (
state.users.find((user) => user.id === userId) ?? {
id: ":",
role: "NONE",
experience: 0,
lastMessageTimestamp: 0,
}
);
};
const checkRoles = (roles: TRole[], userId: string) => {
const user = getUserById(userId);
return roles.includes(user.role);
};
const getRank = (experience: number): IRank => {
let tmpExperience = experience;
let expToNextRank = config.app.experience.startingRequirement;
let rank = 0;
while (tmpExperience >= expToNextRank) {
rank++;
tmpExperience -= expToNextRank;
expToNextRank = expToNextRank *= 1.2;
}
return {
rank: rank,
experience: Math.floor(experience),
experienceInRank: Math.floor(tmpExperience),
expToNextRank: Math.floor(expToNextRank),
};
};
const getUserName = (user: IUser): string => {
const userPattern = /@[a-zA-Z0-9]*/;
const match = user.id.match(userPattern)?.at(0);
if (!match) {
return "";
}
return match.replaceAll("@", "");
};
export { getUserById, checkRoles, getRank, getUserName };