diff options
-rw-r--r-- | commands/__init__.py | 2 | ||||
-rw-r--r-- | commands/users.py | 30 | ||||
-rw-r--r-- | main.py | 15 | ||||
-rw-r--r-- | strings.py | 10 |
4 files changed, 41 insertions, 16 deletions
diff --git a/commands/__init__.py b/commands/__init__.py index e2fd670..b494867 100644 --- a/commands/__init__.py +++ b/commands/__init__.py @@ -10,4 +10,4 @@ # <http://creativecommons.org/publicdomain/zero/1.0/> from .meta import about -from .users import add_users, list_users, remove_users, chooser_user +from .users import set_users, add_users, list_users, remove_users, chooser_user 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"]) @@ -35,19 +35,32 @@ async def unknown(update: Update, context: ContextTypes.DEFAULT_TYPE): text=INVALID_COMMAND ) +async def error_handler(update: object, context: ContextTypes.DEFAULT_TYPE) -> None: + logging.error("Exception:", exc_info=context.error) + + await context.bot.send_message( + chat_id=update.effective_chat.id, + text=context.error.message + ) if __name__ == "__main__": app = ApplicationBuilder().token( os.environ.get("TELEGRAM_TOKEN") ).persistence(Persistence).build() + # Meta commands app.add_handler(CommandHandler("about", commands.about)) + + # Movie commands + app.add_handler(CommandHandler("set", commands.set_users)) app.add_handler(CommandHandler("add", commands.add_users)) app.add_handler(CommandHandler("list", commands.list_users)) app.add_handler(CommandHandler("remove", commands.remove_users)) app.add_handler(CommandHandler("chooser", commands.chooser_user)) - app.add_handler(MessageHandler(filters.COMMAND, unknown)) + app.add_error_handler(error_handler) + app.run_polling() + @@ -9,8 +9,12 @@ # with this software. If not, see: # <http://creativecommons.org/publicdomain/zero/1.0/> -INVALID_COMMAND = "Invalid command. Available commands: /add" +INVALID_COMMAND = "Invalid command. Available commands: /add /list /remove /chooser /about" USER_NOT_PROVIDED = "User(s) is not provided" EXPECTED_ONE_USER = "Expected only one user" -USER_ADDED = "User {user} has been added" -USER_REMOVED = "User {user} has been removed" +USER_ADD = "User {user} has been added" +USERS_ADDED = "Users have been added successfully. Use /list to view." +USER_REMOVE = "User {user} has been removed. Use /list to view." +USERS_REMOVED = "Users has been removed" +USER_SET = "Users have been set successfully. Use /list to view." + |