This commit is contained in:
Aslan 2025-12-28 17:57:58 +01:00
parent 0f6e387f30
commit 05875413f9
2 changed files with 48 additions and 12 deletions

View file

@ -3,6 +3,18 @@ import { state } from "./store/store.js";
import { type IUser, type TRole } from "./store/types.js"; import { type IUser, type TRole } from "./store/types.js";
import type { ILevel } from "./types.js"; import type { ILevel } from "./types.js";
const fixUserData = (user: IUser) => {
if (!user.experience) {
user.experience = 0;
}
if (!user.money) {
user.money = 0;
}
if (!user.aiCost) {
user.aiCost = 0;
}
};
const getUserById = (userId: string): IUser => { const getUserById = (userId: string): IUser => {
const user = state.users.find((user) => user.id === userId); const user = state.users.find((user) => user.id === userId);
if (!user) { if (!user) {
@ -18,15 +30,7 @@ const getUserById = (userId: string): IUser => {
}; };
} }
if (!user.experience) { fixUserData(user);
user.experience = 0;
}
if (!user.money) {
user.money = 0;
}
if (!user.aiCost) {
user.aiCost = 0;
}
return user; return user;
}; };
@ -65,4 +69,28 @@ const getUserName = (user: IUser): string => {
return match.replaceAll("@", ""); return match.replaceAll("@", "");
}; };
export { getUserById, checkRoles, getLevel, getUserName }; const getUserFromMention = (mention: string): IUser | 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;
};
export {
fixUserData,
getUserById,
checkRoles,
getLevel,
getUserName,
getUserFromMention,
};

View file

@ -1,7 +1,12 @@
import { MatrixClient } from "matrix-js-sdk"; import { MatrixClient } from "matrix-js-sdk";
import type { ICallbackStore } from "../types.js"; import type { ICallbackStore } from "../types.js";
import { config } from "../../config.js"; import { config } from "../../config.js";
import { getLevel, getUserById, getUserName } from "../../helpers.js"; import {
getLevel,
getUserById,
getUserFromMention,
getUserName,
} from "../../helpers.js";
import { state } from "../../store/store.js"; import { state } from "../../store/store.js";
import type { IUser } from "../../store/types.js"; import type { IUser } from "../../store/types.js";
let client: MatrixClient; let client: MatrixClient;
@ -29,7 +34,10 @@ const registerModuleUser = (
const onMe = (text: string, roomId: string, sender: string) => { const onMe = (text: string, roomId: string, sender: string) => {
const mention = text.split(" ")[1]; const mention = text.split(" ")[1];
const user = mention ? getUserById(mention) : getUserById(sender); const user = mention ? getUserFromMention(mention) : getUserById(sender);
if (!user) {
return;
}
const level = getLevel(user.experience); const level = getLevel(user.experience);
client.sendHtmlMessage( client.sendHtmlMessage(