Add new file
This commit is contained in:
63
core/commands/registry.py
Normal file
63
core/commands/registry.py
Normal file
@@ -0,0 +1,63 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Реестр команд – связывает имя команды с объектом-обработчиком.
|
||||
Поддерживает алиасы и подсказки при неизвестной команде.
|
||||
"""
|
||||
|
||||
import difflib
|
||||
from typing import Dict, List
|
||||
|
||||
# Импортируем все классы команд
|
||||
from core.commands.info import InfoCommand
|
||||
from core.commands.help import HelpCommand
|
||||
from core.commands.learn import LearnCommand, StopLearnCommand, GlobalLearnCommand
|
||||
from core.commands.kb import ClearCommand, GlobalClearCommand, KbCommand
|
||||
from core.commands.stats import StatsCommand, StatusCommand, CleanCommand
|
||||
from core.commands.expert import SummaryCommand, MetricsCommand
|
||||
from core.commands.other import ResetCommand
|
||||
from core.commands.global_remove import GlobalRemoveCommand
|
||||
from core.commands.template import TemplateSaveCommand, TemplateListCommand, TemplateDeleteCommand
|
||||
from core.commands.create import CreateCommand
|
||||
|
||||
|
||||
def register_commands(bot) -> Dict[str, object]:
|
||||
"""
|
||||
Создаёт и возвращает словарь: имя_команды -> объект команды.
|
||||
Включает основные имена и все алиасы.
|
||||
"""
|
||||
all_commands = [
|
||||
InfoCommand(bot),
|
||||
HelpCommand(bot),
|
||||
LearnCommand(bot),
|
||||
StopLearnCommand(bot),
|
||||
GlobalLearnCommand(bot),
|
||||
ClearCommand(bot),
|
||||
GlobalClearCommand(bot),
|
||||
KbCommand(bot),
|
||||
StatsCommand(bot),
|
||||
StatusCommand(bot),
|
||||
CleanCommand(bot),
|
||||
ResetCommand(bot),
|
||||
SummaryCommand(bot),
|
||||
MetricsCommand(bot),
|
||||
GlobalRemoveCommand(bot),
|
||||
TemplateSaveCommand(bot),
|
||||
TemplateListCommand(bot),
|
||||
TemplateDeleteCommand(bot),
|
||||
CreateCommand(bot),
|
||||
]
|
||||
cmd_map = {}
|
||||
for cmd in all_commands:
|
||||
if cmd.name:
|
||||
cmd_map[cmd.name.lower()] = cmd
|
||||
for alias in cmd.aliases:
|
||||
cmd_map[alias.lower()] = cmd
|
||||
# Сохраняем список основных имён для подсказок (используется в message_handler)
|
||||
bot._command_names = list(set(cmd.name for cmd in all_commands if cmd.name))
|
||||
return cmd_map
|
||||
|
||||
|
||||
def suggest_command(unknown_cmd: str, known_commands: List[str]) -> str:
|
||||
"""Возвращает ближайшее известное имя команды (или None), используя difflib."""
|
||||
matches = difflib.get_close_matches(unknown_cmd, known_commands, n=1, cutoff=0.6)
|
||||
return matches[0] if matches else None
|
||||
Reference in New Issue
Block a user