import { MatrixClient } from "matrix-js-sdk"; import type { ICallbackStore } from "../types.js"; import { config } from "../../config.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; const registerModuleUser = ( matrixClient: MatrixClient, callbackStore: ICallbackStore, ) => { client = matrixClient; callbackStore.messageCallbacks.push({ startConditions: [`${config.app.triggerPrefix}me`], callbackFunc: onMe, }); callbackStore.messageCallbacks.push({ startConditions: [`${config.app.triggerPrefix}myinfo `], callbackFunc: onMyInfo, }); callbackStore.messageCallbacks.push({ startConditions: [`${config.app.triggerPrefix}leaderboard`], callbackFunc: onLeaderboard, }); callbackStore.messageCallbacks.push({ startConditions: [`${config.app.triggerPrefix}aileaderboard`], callbackFunc: onAILeaderboard, }); }; const onMe = (text: string, roomId: string, sender: string) => { const mention = text.split(" ")[1]; const user = mention ? getUserFromMention(mention) : getUserById(sender); if (!user) { return; } const level = getLevel(user.experience); client.sendHtmlMessage( roomId, "", `
Your Role: ${user.role}
Your Money: ${user.money}
Your AI Cost: ${user.aiCost.toFixed(2)}$
Your Level: ${level.level}
Next level progress: ${level.experienceInLevel}/${level.expToNextLevel}xp`, ); }; const onMyInfo = (text: string, roomId: string, sender: string) => { const user = getUserById(sender); const newInformation = text.replace( `${config.app.triggerPrefix}myinfo `, "", ); if (!user || newInformation.length < 3) { return; } user.information = newInformation; client.sendTextMessage(roomId, "Information updated"); }; const onLeaderboard = (_text: string, roomId: string) => { const mapUsersToLeaderboard = (user: IUser): string => { const level = getLevel(user.experience); const username = getUserName(user); return `