F
F
f1awe2021-12-14 22:51:02
Python
f1awe, 2021-12-14 22:51:02

How to continue receiving aiogram messages?

When blocking a user, I want the bot to completely ignore them. I thought a good solution would be the code

@dp.message_handler(content_types=["text"])
async def check_ban(message: types.Message):
    if is_banned(message.from_user.id):
        return


But now, even if is_banned = False, the message does not go further, is not accepted by other handlers, what should I do?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
shurshur, 2021-12-14
@f1awe

It is a bad idea! The library selects the first handler that matches the condition (in this case, content_types=["text"]) and does not use the rest at all.
I recommend either inserting a check into each handler, or making it a condition in the decorator:

@dp.message_handler(lambda message: not is_banned(message.from_user.id)

You can also write your own decorator to just write @check_user_banned. But this is optional.

S
soremix, 2021-12-14
@SoreMix

What does "no further" mean? Is this handler at the very top of the code? If so, it will catch all text messages, including messages that are just commands. So any text processors below it will not work.
If you want to do such a check, I would make my own filter, which I put in the handler. In the filter already check the user

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question