Files
VoiceType/command_handler.py
2025-08-30 13:34:44 +03:00

60 lines
2.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import json
import os
from config import COMMANDS_FILE
class CommandHandler:
def __init__(self):
self.commands = {}
self.actions = {}
self.load_commands()
def load_commands(self):
"""Загрузка пользовательских команд из файла"""
if os.path.exists(COMMANDS_FILE):
try:
with open(COMMANDS_FILE, 'r', encoding='utf-8') as f:
data = json.load(f)
self.commands = data.get('commands', {})
self.actions = data.get('actions', {})
except Exception as e:
print(f"Ошибка при загрузке команд: {e}")
else:
print(f"Файл команд {COMMANDS_FILE} не найден")
def get_command_action(self, command_text, language):
"""Получение действия для команды"""
# Приводим текст к нижнему регистру для сравнения
command_text = command_text.lower().strip()
# Проверяем наличие команд для языка
if language in self.commands:
# Ищем команду
for cmd, action in self.commands[language].items():
if cmd.lower() == command_text:
# Возвращаем строку действия, а не код
return self.actions.get(action)
return None
def add_command(self, language, command_text, action_name):
"""Добавление новой команды"""
if language not in self.commands:
self.commands[language] = {}
self.commands[language][command_text] = action_name
# Сохраняем изменения
self.save_commands()
def save_commands(self):
"""Сохранение команд в файл"""
data = {
'commands': self.commands,
'actions': self.actions
}
try:
with open(COMMANDS_FILE, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)
except Exception as e:
print(f"Ошибка при сохранении команд: {e}")