From 8834c7bc0f1b624bade8195e7eb6ca2366a6649f Mon Sep 17 00:00:00 2001 From: Eduard Kuzmenko Date: Fri, 4 Mar 2022 14:59:25 +0200 Subject: [PATCH] Fix bot commands search order --- src/components/chat/commandsHelper.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/components/chat/commandsHelper.ts b/src/components/chat/commandsHelper.ts index a8a27eeb..8d4c225b 100644 --- a/src/components/chat/commandsHelper.ts +++ b/src/components/chat/commandsHelper.ts @@ -22,14 +22,17 @@ export function processPeerFullForCommands(full: ChatFull.chatFull | ChatFull.ch }); } - const commands: Map = new Map(); + type T = {peerId: PeerId, name: string, description: string, index: number, command: string}; + const commands: Map = new Map(); botInfos.forEach(botInfo => { - botInfo.commands.forEach(botCommand => { + botInfo.commands.forEach((botCommand, idx) => { const c = '/' + botCommand.command; commands.set(botCommand.command, { peerId: botInfo.user_id.toPeerId(false), + command: botCommand.command, name: c, - description: botCommand.description + description: botCommand.description, + index: idx }); if(index) { @@ -38,13 +41,17 @@ export function processPeerFullForCommands(full: ChatFull.chatFull | ChatFull.ch }); }); + let out: T[]; if(!index) { - return [...commands.values()]; + out = [...commands.values()]; + } else { + const found = index.search(query); + out = Array.from(found).map(command => commands.get(command)); } - const found = index.search(query); - const filtered = Array.from(found).map(command => commands.get(command)); - return filtered; + out = out.sort((a, b) => commands.get(a.command).index - commands.get(b.command).index); + + return out; } export default class CommandsHelper extends AutocompletePeerHelper {