S
S
szjyakgf2021-12-05 13:12:58
Python
szjyakgf, 2021-12-05 13:12:58

How to avoid errors with the /ban command?

I wrote the code to remove users from the chat:

@dp.message_handler(commands=['ban'])
async def ban(message: types.Message):
    if not message.reply_to_message:
        pass
    else:
        await message.bot.ban_chat_member(message.chat.id, message.reply_to_message.from_user.id)

But I get 3 errors:
1, if the bot does not have permission to delete users
2, if the admin sends a command to the admin
3, if the command is not used by the admin
Is there any way to avoid them? For example, how I put a check on the replay

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
inworkl, 2021-12-05
@szjyakgf

As an option to wrap in try except

@dp.message_handler(commands=['ban'])
async def ban(message: types.Message):
    try:
        await message.bot.ban_chat_member(message.chat.id, message.reply_to_message.from_user.id)
    except:
        pass

But it's good to have handlers for each error, and restructuring the code so that these errors do not occur in principle.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question