From 0d48ea28aeb8a6290d2aeab202efa9c8d6e30fe3 Mon Sep 17 00:00:00 2001 From: Mikhail Kirillov Date: Wed, 9 Oct 2024 03:40:27 +0400 Subject: Add /set command; Add error_handler; --- commands/users.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'commands/users.py') diff --git a/commands/users.py b/commands/users.py index affdb53..413b681 100644 --- a/commands/users.py +++ b/commands/users.py @@ -13,11 +13,23 @@ from telegram import Update, error from telegram.ext import ContextTypes from collections import deque -from strings import USER_NOT_PROVIDED, USER_ADDED, USER_REMOVED, EXPECTED_ONE_USER +from strings import USER_NOT_PROVIDED, USERS_ADDED, USERS_REMOVED, \ + EXPECTED_ONE_USER, USER_SET, USER_ADD, USER_REMOVE from utils import context_init -from rich import inspect +async def set_users( + update: Update, + context: ContextTypes.DEFAULT_TYPE +) -> None: + context_init(context) + + if context.args == []: + raise error.TelegramError(USER_NOT_PROVIDED) + + context.chat_data["users"] = context.args + + await update.message.reply_text(USER_SET) async def add_users( update: Update, @@ -26,14 +38,13 @@ async def add_users( context_init(context) if context.args == []: - await update.message.reply_text(USER_NOT_PROVIDED) raise error.TelegramError(USER_NOT_PROVIDED) for user in context.args: context.chat_data["users"].append(user) - await update.message.reply_text(USER_ADDED.format(user=user)) + await update.message.reply_text(USER_ADD.format(user=user)) - await update.message.reply_text(context.chat_data["users"]) + await update.message.reply_text(USERS_ADDED) async def list_users( @@ -52,7 +63,6 @@ async def remove_users( context_init(context) if context.args == []: - await update.message.reply_text(USER_NOT_PROVIDED) raise error.TelegramError(USER_NOT_PROVIDED) for user in context.args: @@ -61,9 +71,9 @@ async def remove_users( break context.chat_data["users"].remove(user) - await update.message.reply_text(USER_REMOVED.format(user=user)) + await update.message.reply_text(USER_REMOVE.format(user=user)) - await update.message.reply_text(context.chat_data["users"]) + await update.message.reply_text(USERS_REMOVED) async def chooser_user( @@ -73,11 +83,9 @@ async def chooser_user( context_init(context) if context.args == []: - await update.message.reply_text(USER_NOT_PROVIDED) raise error.TelegramError(USER_NOT_PROVIDED) if len(context.args) > 1: - await update.message.reply_text(EXPECTED_ONE_USER) raise error.TelegramError(EXPECTED_ONE_USER) chooser = context.args[0] @@ -88,4 +96,4 @@ async def chooser_user( context.chat_data["users"] = list(users) - await update.message.reply_text(context.chat_data) + await update.message.reply_text(context.chat_data["users"]) -- cgit v1.2.3