diff --git a/src/events/mineflayer/message.ts b/src/events/mineflayer/message.ts index 19f7489..fde24ec 100644 --- a/src/events/mineflayer/message.ts +++ b/src/events/mineflayer/message.ts @@ -37,9 +37,14 @@ function splitRightCarrotInFirstWord(words: string[]): string[] { * from the message and collapse spaces. */ function removePlayerFromMsg(fullMsg: string, player: string): string { - // allow optional spaces, optional < and >, optional trailing colon - const pattern = new RegExp(`\\s*?\\s*:?\\s*`, "g"); - return fullMsg.replace(pattern, " ").trim().replace(/\s+/g, " "); + // allow optional clan tag inside <>, optional trailing colon + const bracketPattern = new RegExp(`\\s*<\\s*[^>]*\\b${player}\\b\\s*>\\s*:?\\s*`, "g"); + const plainPattern = new RegExp(`\\s*?\\s*:?\\s*`, "g"); + return fullMsg + .replace(bracketPattern, " ") + .replace(plainPattern, " ") + .trim() + .replace(/\s+/g, " "); } /** diff --git a/src/events/mineflayer/messagestr.ts b/src/events/mineflayer/messagestr.ts index 1816703..d94ac51 100644 --- a/src/events/mineflayer/messagestr.ts +++ b/src/events/mineflayer/messagestr.ts @@ -211,6 +211,22 @@ export default { } // Fallback for simpler chat formats (e.g., message) + if (!username) { + const clanMatch = rawMessage.match(/^<([^>]+)>\s*(.+)$/); + if (clanMatch) { + const inside = clanMatch[1].trim(); + const msgContent = clanMatch[2]; + const parts = inside.split(/\s+/); + const nameToken = parts[parts.length - 1]; + const possibleUsername = parseUsername(nameToken, Bot.bot); + if (Bot.bot.players[possibleUsername]) { + username = possibleUsername; + uuid = Bot.bot.players[username].uuid; + message = msgContent.trim(); + } + } + } + if (!username) { const chatDividers = ["ยป", ">>", ">", ":"]; for (const divider of chatDividers) { @@ -257,4 +273,4 @@ export default { } } }, -}; \ No newline at end of file +}; diff --git a/src/structure/mineflayer/utils/parseUsername.ts b/src/structure/mineflayer/utils/parseUsername.ts index a0c8944..3808a11 100644 --- a/src/structure/mineflayer/utils/parseUsername.ts +++ b/src/structure/mineflayer/utils/parseUsername.ts @@ -1,17 +1,24 @@ import type { Bot } from "mineflayer"; export default function parseUsername(name: string, bot: Bot): string { + let cleanedName = name.trim(); + // If a clan/prefix is present, take the last token (usernames have no spaces) + if (cleanedName.includes(" ")) { + const parts = cleanedName.split(/\s+/); + cleanedName = parts[parts.length - 1]; + } + // remove everything except word chars, underscores, numbers - name = name.replace(/[^_\w\d]/g, ''); + cleanedName = cleanedName.replace(/[^_\w\d]/g, ''); // remove leading "<" if present - if (name.startsWith("<")) { - name = name.slice(1); + if (cleanedName.startsWith("<")) { + cleanedName = cleanedName.slice(1); } // if exact match, return early - if (bot.players[name] && name === bot.players[name].displayName.toString()) { - return name; + if (bot.players[cleanedName] && cleanedName === bot.players[cleanedName].displayName.toString()) { + return cleanedName; } // try to resolve real username @@ -25,11 +32,11 @@ export default function parseUsername(name: string, bot: Bot): string { } // strict equality check, not includes - if (name === displayName || name === user) { + if (cleanedName === displayName || cleanedName === user) { return user; } } // fallback - return name; + return cleanedName; }