aslobot-matrix/src/helpers.ts
2026-01-23 09:26:48 -05:00

150 lines
3.6 KiB
TypeScript

import type { MatrixClient } from "matrix-js-sdk";
import { config } from "./config.js";
import { state } from "./store/store.js";
import { type IUser, type TRole } from "./store/types.js";
import type { ILevel } from "./types.js";
import { appendFileSync } from "fs";
const fixUserData = (user: IUser) => {
if (!user.experience) {
user.experience = 0;
}
if (!user.money) {
user.money = 0;
}
if (!user.aiCost) {
user.aiCost = 0;
}
if (!user.lastExperienceGainTimestamp) {
user.lastExperienceGainTimestamp = 0;
}
if (!user.lastMoneyGainTimestamp) {
user.lastMoneyGainTimestamp = 0;
}
};
const getUserById = (userId: string): IUser => {
const user = state.users.find((user) => user.id === userId);
if (!user) {
log(`Tried to get a non existing user ${userId}}`);
return {
id: ":",
role: "NONE",
experience: 0,
money: 0,
aiCost: 0,
lastMessageTimestamp: 0,
lastExperienceGainTimestamp: 0,
lastMoneyGainTimestamp: 0,
};
}
fixUserData(user);
return user;
};
const checkRoles = (roles: TRole[], userId: string) => {
const user = getUserById(userId);
return roles.includes(user.role);
};
const getLevel = (experience: number): ILevel => {
let tmpExperience = experience;
let expToNextLevel = config.app.experience.startingRequirement;
let level = 0;
while (tmpExperience >= expToNextLevel) {
level++;
tmpExperience -= expToNextLevel;
expToNextLevel = expToNextLevel *= config.app.experience.multiplier;
}
return {
level: level,
experience: Math.floor(experience),
experienceInLevel: Math.floor(tmpExperience),
expToNextLevel: Math.floor(expToNextLevel),
};
};
const getUserName = (user: IUser): string => {
return getUserNameById(user.id);
};
const getUserNameById = (userId: string): string => {
const userPattern = /@[a-zA-Z0-9]*/;
const match = userId.match(userPattern)?.at(0);
if (!match) {
return "";
}
let username = match.replaceAll("@", "");
username = username.charAt(0).toUpperCase() + username.slice(1);
return username;
};
const getUserFromMention = (mention: string | undefined): IUser | undefined => {
if (!mention) {
return undefined;
}
const regex = /\[\@\S*:aslan2142.space\]/;
const match = mention
.match(regex)
?.at(0)
?.replaceAll("[", "")
.replaceAll("]", "");
const user = state.users.find((user) => user.id === match);
if (!user) {
return undefined;
}
fixUserData(user);
return user;
};
const changePersonality = (
client: MatrixClient,
personalityName: string,
): boolean => {
const personalityIndex = config.app.ai.personalities.findIndex(
(personality) =>
personality.personalityName.toLowerCase() ===
personalityName.toLowerCase(),
);
if (personalityIndex === -1) {
return false;
}
const name =
personalityName.charAt(0).toUpperCase() + personalityName.slice(1);
state.personality = {
index: personalityIndex,
startTime: Date.now(),
};
client.setDisplayName(name);
return true;
};
const log = (logMessage: string) => {
appendFileSync(
config.logPath,
`[${new Date().toLocaleString()}] ${logMessage}\n`,
);
};
export {
fixUserData,
getUserById,
checkRoles,
getLevel,
getUserName,
getUserNameById,
getUserFromMention,
changePersonality,
log,
};