Changeset 0d48ea2


Ignore:
Timestamp:
Oct 9, 2024, 3:40:27 AM (5 weeks ago)
Author:
Mikhail Kirillov <w96k@…>
Branches:
master
Children:
6623428
Parents:
cc34991
Message:

Add /set command; Add error_handler;

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • commands/__init__.py

    rcc34991 r0d48ea2  
    1111
    1212from .meta import about
    13 from .users import add_users, list_users, remove_users, chooser_user
     13from .users import set_users, add_users, list_users, remove_users, chooser_user
  • commands/users.py

    rcc34991 r0d48ea2  
    1414from collections import deque
    1515
    16 from strings import USER_NOT_PROVIDED, USER_ADDED, USER_REMOVED, EXPECTED_ONE_USER
     16from strings import USER_NOT_PROVIDED, USERS_ADDED, USERS_REMOVED, \
     17    EXPECTED_ONE_USER, USER_SET, USER_ADD, USER_REMOVE
    1718from utils import context_init
    1819
    19 from rich import inspect
    2020
     21async def set_users(
     22        update: Update,
     23        context: ContextTypes.DEFAULT_TYPE
     24) -> None:
     25    context_init(context)
     26
     27    if context.args == []:
     28        raise error.TelegramError(USER_NOT_PROVIDED)
     29
     30    context.chat_data["users"] = context.args
     31
     32    await update.message.reply_text(USER_SET)
    2133
    2234async def add_users(
     
    2739
    2840    if context.args == []:
    29         await update.message.reply_text(USER_NOT_PROVIDED)
    3041        raise error.TelegramError(USER_NOT_PROVIDED)
    3142
    3243    for user in context.args:
    3344        context.chat_data["users"].append(user)
    34         await update.message.reply_text(USER_ADDED.format(user=user))
     45        await update.message.reply_text(USER_ADD.format(user=user))
    3546
    36     await update.message.reply_text(context.chat_data["users"])
     47    await update.message.reply_text(USERS_ADDED)
    3748
    3849
     
    5364
    5465    if context.args == []:
    55         await update.message.reply_text(USER_NOT_PROVIDED)
    5666        raise error.TelegramError(USER_NOT_PROVIDED)
    5767
     
    6272
    6373        context.chat_data["users"].remove(user)
    64         await update.message.reply_text(USER_REMOVED.format(user=user))
     74        await update.message.reply_text(USER_REMOVE.format(user=user))
    6575
    66     await update.message.reply_text(context.chat_data["users"])
     76    await update.message.reply_text(USERS_REMOVED)
    6777
    6878
     
    7484
    7585    if context.args == []:
    76         await update.message.reply_text(USER_NOT_PROVIDED)
    7786        raise error.TelegramError(USER_NOT_PROVIDED)
    7887
    7988    if len(context.args) > 1:
    80         await update.message.reply_text(EXPECTED_ONE_USER)
    8189        raise error.TelegramError(EXPECTED_ONE_USER)
    8290
     
    8997    context.chat_data["users"] = list(users)
    9098
    91     await update.message.reply_text(context.chat_data)
     99    await update.message.reply_text(context.chat_data["users"])
  • main.py

    rcc34991 r0d48ea2  
    3636    )
    3737
     38async def error_handler(update: object, context: ContextTypes.DEFAULT_TYPE) -> None:
     39    logging.error("Exception:", exc_info=context.error)
     40
     41    await context.bot.send_message(
     42        chat_id=update.effective_chat.id,
     43        text=context.error.message
     44    )
    3845
    3946if __name__ == "__main__":
     
    4249    ).persistence(Persistence).build()
    4350
     51    # Meta commands
    4452    app.add_handler(CommandHandler("about", commands.about))
     53
     54    # Movie commands
     55    app.add_handler(CommandHandler("set", commands.set_users))
    4556    app.add_handler(CommandHandler("add", commands.add_users))
    4657    app.add_handler(CommandHandler("list", commands.list_users))
     
    4859    app.add_handler(CommandHandler("chooser", commands.chooser_user))
    4960
     61    app.add_handler(MessageHandler(filters.COMMAND, unknown))
     62    app.add_error_handler(error_handler)
    5063
    51     app.add_handler(MessageHandler(filters.COMMAND, unknown))
    5264
    5365    app.run_polling()
     66
  • strings.py

    rcc34991 r0d48ea2  
    1010# <http://creativecommons.org/publicdomain/zero/1.0/>
    1111
    12 INVALID_COMMAND = "Invalid command. Available commands: /add"
     12INVALID_COMMAND = "Invalid command. Available commands: /add /list /remove /chooser /about"
    1313USER_NOT_PROVIDED = "User(s) is not provided"
    1414EXPECTED_ONE_USER = "Expected only one user"
    15 USER_ADDED = "User {user} has been added"
    16 USER_REMOVED = "User {user} has been removed"
     15USER_ADD = "User {user} has been added"
     16USERS_ADDED = "Users have been added successfully. Use /list to view."
     17USER_REMOVE = "User {user} has been removed. Use /list to view."
     18USERS_REMOVED = "Users has been removed"
     19USER_SET = "Users have been set successfully. Use /list to view."
     20
Note: See TracChangeset for help on using the changeset viewer.