From 05875413f9c25ae89b3568ad6cde7f6b2015f26c1ddc2f00fe96f51aee72c229 Mon Sep 17 00:00:00 2001 From: aslan Date: Sun, 28 Dec 2025 17:57:58 +0100 Subject: [PATCH] Fix me --- src/helpers.ts | 48 +++++++++++++++++++++++++++++++--------- src/modules/user/user.ts | 12 ++++++++-- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index c396e7b..d09d7f6 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -3,6 +3,18 @@ import { state } from "./store/store.js"; import { type IUser, type TRole } from "./store/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 user = state.users.find((user) => user.id === userId); if (!user) { @@ -18,15 +30,7 @@ const getUserById = (userId: string): IUser => { }; } - if (!user.experience) { - user.experience = 0; - } - if (!user.money) { - user.money = 0; - } - if (!user.aiCost) { - user.aiCost = 0; - } + fixUserData(user); return user; }; @@ -65,4 +69,28 @@ const getUserName = (user: IUser): string => { 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, +}; diff --git a/src/modules/user/user.ts b/src/modules/user/user.ts index 65825bb..1ad7770 100644 --- a/src/modules/user/user.ts +++ b/src/modules/user/user.ts @@ -1,7 +1,12 @@ import { MatrixClient } from "matrix-js-sdk"; import type { ICallbackStore } from "../types.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 type { IUser } from "../../store/types.js"; let client: MatrixClient; @@ -29,7 +34,10 @@ const registerModuleUser = ( const onMe = (text: string, roomId: string, sender: string) => { 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); client.sendHtmlMessage(