diff --git a/src/context.ts b/src/context.ts index 93ddbc4..f8ffd45 100644 --- a/src/context.ts +++ b/src/context.ts @@ -82,13 +82,18 @@ export function commands() { }; ctx.getNearestCommand = (commands, options) => { - if (ctx.msg?.text) { - commands = ensureArray(commands); - const results = commands + if (ctx.has(":text")) { + const results = ensureArray(commands) .map((commands) => { - const userInput = ctx.msg!.text!.substring(1); + const firstMatch = ctx.getCommandEntities( + commands, + )[0]; + const commandLike = firstMatch?.text.replace( + firstMatch.prefix, + "", + ) || ""; const result = fuzzyMatch( - userInput, + commandLike, commands, { ...options, @@ -135,34 +140,32 @@ export function commands() { const regexes = prefixes.map( (prefix) => new RegExp( - `(\?\<\!\\S)${ + `(\?\<\!\\S)(\?${ escapeSpecial( prefix, ) - }\\S+(\\s|$)`, + })\\S+(\\s|$)`, "g", ), ); - const allMatches = regexes.flatMap((regex) => { - let match; + const entities = regexes.flatMap((regex) => { + let match: RegExpExecArray | null; const matches = []; while ( (match = regex.exec(text)) !== null ) { + const text = match[0].trim(); matches.push({ - text: match[0].trim(), + text, offset: match.index, + prefix: match.groups!.prefix, + type: "bot_command", + length: text.length, }); } - return matches; + return matches as botCommandEntity[]; }); - const entities = allMatches.map((match) => ({ - ...match, - type: "bot_command", - length: match.text.length, - })) as botCommandEntity[]; - return entities; }; diff --git a/src/types.ts b/src/types.ts index ad372a0..84bee98 100644 --- a/src/types.ts +++ b/src/types.ts @@ -41,4 +41,5 @@ export interface CommandElementals { export interface botCommandEntity extends MessageEntity.CommonMessageEntity { type: "bot_command"; text: string; + prefix: string; } diff --git a/test/commands.test.ts b/test/commands.test.ts index 707d76e..db51f2e 100644 --- a/test/commands.test.ts +++ b/test/commands.test.ts @@ -415,18 +415,21 @@ describe("Commands", () => { offset: 0, type: "bot_command", length: 3, + prefix: "/", }, { text: "?momi", offset: 4, type: "bot_command", length: 5, + prefix: "?", }, { text: "abcdfghi", offset: 10, type: "bot_command", length: 8, + prefix: "abcd", }, ]); });