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', |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 10 | 'notice', |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 11 | 'sticker', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 12 | 'msg', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 13 | 'join', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 14 | 'leave', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 15 | 'invite', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 16 | 'disinvite', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 17 | 'kick', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 18 | 'ban', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 19 | 'unban', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 20 | 'powerlevel', |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 21 | 'mute', |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 22 | 'unmute', |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 23 | 'ignore', |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 24 | 'unignore', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 25 | 'delete', |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 26 | 'roomnick', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 27 | 'globalnick', |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 28 | 'roomavatar', |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 29 | 'globalavatar' |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 30 | ] as const; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 31 | export type CommandName = (typeof COMMANDS)[number]; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 32 | |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 33 | /** A leading `/word`, or null for plain text. `//text` sends `/text` as a message. */ |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 34 | export function parseCommand(text: string): ParsedCommand | null { |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 35 | const match = /^\/([a-z]+)(?:\s+([\s\S]*))?$/i.exec(text.trim()); |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 36 | if (!match) return null; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 37 | return { name: match[1].toLowerCase(), args: (match[2] ?? '').trim() }; |
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 function isKnownCommand(name: string): name is CommandName { |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 41 | return (COMMANDS as readonly string[]).includes(name); |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 42 | } |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 43 | |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 44 | export function unescapeCommand(text: string): string { |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 45 | return text.startsWith('//') ? text.slice(1) : text; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 46 | } |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 47 | |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 48 | export type Target = { user_id: string; rest: string } | { error: string }; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 49 | |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 50 | /** The user an argument string starts with, by matrix id or by a known display name. */ |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 51 | export function splitTarget(args: string, people: Person[]): Target { |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 52 | if (!args) return { error: 'Name someone' }; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 53 | if (args.startsWith('@')) { |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 54 | const [id, ...rest] = args.split(/\s+/); |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 55 | if (/^@[^:\s]+:\S+$/.test(id)) return { user_id: id, rest: rest.join(' ') }; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 56 | } |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 57 | const lower = args.toLowerCase(); |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 58 | let best: Person[] = []; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 59 | let bestLength = 0; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 60 | for (const person of people) { |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 61 | const name = person.name.toLowerCase(); |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 62 | if (!name || name.length < bestLength) continue; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 63 | if (lower !== name && !lower.startsWith(`${name} `)) continue; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 64 | if (name.length > bestLength) { |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 65 | best = [person]; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 66 | bestLength = name.length; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 67 | } else { |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 68 | best.push(person); |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 69 | } |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 70 | } |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 71 | const ids = [...new Set(best.map((p) => p.user_id))]; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 72 | if (ids.length === 0) return { error: `No one here is called ${args.split(/\s+/)[0]}` }; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 73 | if (ids.length > 1) return { error: `Several people are called ${best[0].name}, use their @id` }; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 74 | return { user_id: ids[0], rest: args.slice(bestLength).trim() }; |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 75 | } |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 76 | |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 77 | export const COMMAND_USAGE: Record<CommandName, string> = { |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 78 | me: '<action>', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 79 | notice: '<text>', |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 80 | sticker: '<sticker name>', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 81 | msg: '<user> [message]', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 82 | join: '<room>', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 83 | leave: '', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 84 | invite: '<user>', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 85 | disinvite: '<user> [reason]', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 86 | kick: '<user> [reason]', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 87 | ban: '<user> [reason]', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 88 | unban: '<user> [reason]', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 89 | powerlevel: '<user> <level>', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 90 | mute: '<user>', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 91 | unmute: '<user>', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 92 | ignore: '<user>', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 93 | unignore: '<user>', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 94 | delete: '[reason]', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 95 | roomnick: '[name]', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 96 | globalnick: '<name>', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 97 | roomavatar: '[mxc]', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 98 | globalavatar: '[mxc]' |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 99 | }; |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 100 | |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 101 | /** The command name being typed when the caret sits in a leading `/word`. */ |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 102 | export function commandQueryAt(text: string, caret: number): string | null { |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 103 | const match = /^\/([a-z]*)$/i.exec(text.slice(0, caret)); |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 104 | return match ? match[1].toLowerCase() : null; |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 105 | } |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 106 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 107 | export function matchCommands(query: string): CommandName[] { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 108 | const q = query.toLowerCase(); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 109 | return COMMANDS.flatMap((name, order) => { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 110 | if (name.startsWith(q)) return [{ name, score: -1, order }]; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 111 | let previous = -1; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 112 | let score = 0; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 113 | for (const letter of q) { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 114 | const index = name.indexOf(letter, previous + 1); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 115 | if (index < 0) return []; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 116 | score += index - previous - 1; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 117 | previous = index; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 118 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 119 | return [{ name, score, order }]; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 120 | }).sort((a, b) => a.score - b.score || a.order - b.order).map(({ name }) => name); |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 121 | } |