From f4fa9f80d2b1c19260a9782076b189338f2c8b17 Mon Sep 17 00:00:00 2001 From: Artyom Isrofilov Date: Thu, 29 Aug 2024 16:51:43 +0300 Subject: [PATCH] refactoring --- WinControlBot.pyw | 99 ++++++++++++++++------------------------------- requirements.txt | 4 +- 2 files changed, 36 insertions(+), 67 deletions(-) diff --git a/WinControlBot.pyw b/WinControlBot.pyw index 2c8af1d..5e04db0 100644 --- a/WinControlBot.pyw +++ b/WinControlBot.pyw @@ -9,12 +9,7 @@ AUTHORIZED_USERS = [User1,User2,User3] translations = { 'en': { 'unauthorized': 'You do not have permission to execute this command.\nYour userId is {user_id}', - 'commands': ( - '/uptime - Get system uptime\n' - '/sleep - Put the system to sleep\n' - '/hibernate - Hibernate the system\n' - '/shutdown - Shut down the system' - ), + 'commands': '/uptime - Get system uptime\n/sleep - Put the system to sleep\n/hibernate - Hibernate the system\n/shutdown - Shut down the system', 'sleep': 'The computer will be put to sleep...', 'hibernate': 'The computer will be hibernated...', 'shutdown': 'Shutting down the computer...', @@ -22,12 +17,7 @@ translations = { }, 'ru': { 'unauthorized': 'У вас нет прав для выполнения этой команды.\nВаш userId {user_id}', - 'commands': ( - '/uptime - Получить время работы системы\n' - '/sleep - Перевести в режим сна\n' - '/hibernate - Перевести в режим гибернации\n' - '/shutdown - Выключить компьютер' - ), + 'commands': '/uptime - Получить время работы системы\n/sleep - Перевести в режим сна\n/hibernate - Перевести в режим гибернации\n/shutdown - Выключить компьютер', 'sleep': 'Компьютер будет отправлен в режим сна...', 'hibernate': 'Компьютер будет отправлен в режим гибернации...', 'shutdown': 'Выключаю компьютер...', @@ -36,82 +26,61 @@ translations = { } def get_translation(language_code, key, **kwargs): - if language_code not in translations: - language_code = 'en' - translations_dict = translations[language_code] - return translations_dict[key].format(**kwargs) + return translations.get(language_code, translations['en'])[key].format(**kwargs) - async def is_user_authorized(user_id: int) -> bool: return user_id in AUTHORIZED_USERS -async def send_unauthorized_message(update: Update, language_code: str) -> None: +async def handle_command(update: Update, message_key: str, action=None, **kwargs) -> None: user_id = update.effective_user.id - message = get_translation(language_code, 'unauthorized', user_id=user_id) - await update.message.reply_text(message) + language_code = update.effective_user.language_code + if await is_user_authorized(user_id): + await send_reply(update, message_key, language_code, **kwargs) + if action: + os.system(action) + else: + await send_reply(update, 'unauthorized', language_code, user_id=user_id) async def send_reply(update: Update, message_key: str, language_code: str, **kwargs) -> None: message = get_translation(language_code, message_key, **kwargs) await update.message.reply_text(message) async def start(update: Update, context) -> None: - language_code = update.effective_user.language_code - if await is_user_authorized(update.effective_user.id): - await send_reply(update, 'commands', language_code) - else: - await send_unauthorized_message(update, language_code) - + await handle_command(update, 'commands') + async def sleep(update: Update, context) -> None: - language_code = update.effective_user.language_code - if await is_user_authorized(update.effective_user.id): - await send_reply(update, 'sleep', language_code) - os.system("rundll32.exe powrprof.dll,SetSuspendState 0,1,0") - else: - await send_unauthorized_message(update, language_code) + await handle_command(update, 'sleep', action="rundll32.exe powrprof.dll,SetSuspendState 0,1,0") async def hibernate(update: Update, context) -> None: - language_code = update.effective_user.language_code - if await is_user_authorized(update.effective_user.id): - await send_reply(update, 'hibernate', language_code) - os.system("shutdown /h") - else: - await send_unauthorized_message(update, language_code) + await handle_command(update, 'hibernate', action="shutdown /h") async def shutdown(update: Update, context) -> None: - language_code = update.effective_user.language_code - if await is_user_authorized(update.effective_user.id): - await send_reply(update, 'shutdown', language_code) - os.system("shutdown /s /t 0") - else: - await send_unauthorized_message(update, language_code) + await handle_command(update, 'shutdown', action="shutdown /s /t 0") async def get_uptime(update: Update, context) -> None: - language_code = update.effective_user.language_code - if await is_user_authorized(update.effective_user.id): - uptime_seconds = int(uptime.uptime()) - uptime_days = int(uptime_seconds // (3600 * 24)) - uptime_hours = int((uptime_seconds % (3600 * 24)) // 3600) - uptime_minutes = int((uptime_seconds % 3600) // 60) - remaining_seconds = int(uptime_seconds % 60) - await send_reply(update, 'uptime', language_code, - days=uptime_days, - hours=uptime_hours, - minutes=uptime_minutes, - seconds=remaining_seconds) - else: - await send_unauthorized_message(update, language_code) - + uptime_seconds = int(uptime.uptime()) + await handle_command(update, 'uptime', + days=uptime_seconds // (3600 * 24), + hours=(uptime_seconds % (3600 * 24)) // 3600, + minutes=(uptime_seconds % 3600) // 60, + seconds=uptime_seconds % 60) def main() -> None: application = Application.builder().token(TOKEN).build() - application.add_handler(CommandHandler('start', start)) - application.add_handler(CommandHandler('uptime', get_uptime)) - application.add_handler(CommandHandler('sleep', sleep)) - application.add_handler(CommandHandler('hibernate', hibernate)) - application.add_handler(CommandHandler('shutdown', shutdown)) + command_handlers = { + 'start': start, + 'uptime': get_uptime, + 'sleep': sleep, + 'hibernate': hibernate, + 'shutdown': shutdown + } + + for command, handler in command_handlers.items(): + application.add_handler(CommandHandler(command, handler)) + application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, start)) application.run_polling() if __name__ == "__main__": - main() + main() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 07f33c5..2313521 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -python-telegram-bot==20.7 -requests==2.32.0 +python-telegram-bot==21.4 +requests==2.32.3 uptime==3.0.1