TgRouting/deamon/router/commandMatcher.js

29 lines
816 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// commandMatcher.js
/**
* Ищет команду в начале текста и возвращает объект с ботом и командой
* @param {string} text
* @param {Array<{ name: string, commands: Array<{ command: string }> }>} bots
* @returns {{ botName: string, commandName: string } | null}
*/
export function findCommandTargetBot(text, bots) {
if (!text) return null
// первое слово, без ведущего "/"
const [firstWord] = text.trim().split(/\s+/)
const normalized = firstWord.replace(/^\//, '').toLowerCase()
for (const b of bots) {
for (const cmd of b.commands || []) {
if (cmd.command.toLowerCase() === normalized) {
return {
botName: b.name,
commandName: cmd.command
}
}
}
}
return null
}