e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 1 | import type { Person } from './types'; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 2 | |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 3 | export interface ParsedCommand { |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 4 | name: string; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 5 | args: string; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 6 | } |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 7 | |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 8 | export const COMMANDS = [ |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 9 | 'me', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 10 | 'msg', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 11 | 'join', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 12 | 'leave', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 13 | 'invite', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 14 | 'disinvite', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 15 | 'kick', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 16 | 'ban', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 17 | 'unban', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 18 | 'powerlevel', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 19 | 'delete', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 20 | 'globalnick', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 21 | 'globalavatar' |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 22 | ] as const; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 23 | export type CommandName = (typeof COMMANDS)[number]; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 24 | |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 25 | /** A leading `/word`, or null for plain text. `//text` sends `/text` as a message. */ |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 26 | export function parseCommand(text: string): ParsedCommand | null { |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 27 | const match = /^\/([a-z]+)(?:\s+([\s\S]*))?$/i.exec(text.trim()); |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 28 | if (!match) return null; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 29 | return { name: match[1].toLowerCase(), args: (match[2] ?? '').trim() }; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 30 | } |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 31 | |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 32 | export function isKnownCommand(name: string): name is CommandName { |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 33 | return (COMMANDS as readonly string[]).includes(name); |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 34 | } |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 35 | |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 36 | export function unescapeCommand(text: string): string { |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 37 | return text.startsWith('//') ? text.slice(1) : text; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 38 | } |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 39 | |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 40 | export type Target = { user_id: string; rest: string } | { error: string }; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 41 | |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 42 | /** The user an argument string starts with, by matrix id or by a known display name. */ |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 43 | export function splitTarget(args: string, people: Person[]): Target { |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 44 | if (!args) return { error: 'Name someone' }; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 45 | if (args.startsWith('@')) { |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 46 | const [id, ...rest] = args.split(/\s+/); |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 47 | if (/^@[^:\s]+:\S+$/.test(id)) return { user_id: id, rest: rest.join(' ') }; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 48 | } |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 49 | const lower = args.toLowerCase(); |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 50 | let best: Person[] = []; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 51 | let bestLength = 0; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 52 | for (const person of people) { |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 53 | const name = person.name.toLowerCase(); |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 54 | if (!name || name.length < bestLength) continue; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 55 | if (lower !== name && !lower.startsWith(`${name} `)) continue; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 56 | if (name.length > bestLength) { |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 57 | best = [person]; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 58 | bestLength = name.length; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 59 | } else { |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 60 | best.push(person); |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 61 | } |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 62 | } |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 63 | const ids = [...new Set(best.map((p) => p.user_id))]; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 64 | if (ids.length === 0) return { error: `No one here is called ${args.split(/\s+/)[0]}` }; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 65 | if (ids.length > 1) return { error: `Several people are called ${best[0].name}, use their @id` }; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 66 | return { user_id: ids[0], rest: args.slice(bestLength).trim() }; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 67 | } |