2

How can I make it so that the administrator can indicate the reason for the ban and then the bot indicated this in the chat? Example: "!ban @NAMEorID Spam" From bot to chat: "User NAME banned. Reason - REASON"

@dp.message_handler(AdminFilter(is_chat_admin=True), IsReplyFilter(is_reply=True), commands=['ban'],
                    commands_prefix='!', chat_type=[types.ChatType.SUPERGROUP, types.ChatType.GROUP])
async def ban(message: types.Message):
    replied_user = message.reply_to_message.from_user.id
    admin_id = message.from_user.id
    await bot.kick_chat_member(chat_id=message.chat.id, user_id=replied_user)
    await bot.delete_message(chat_id=message.chat.id, message_id=message.message_id)
    await bot.send_message(chat_id=message.chat.id, text=f"[{message.reply_to_message.from_user.full_name}]"
                                                         f"(tg://user?id={replied_user})"
                                                         f" was banned ",
                           parse_mode=types.ParseMode.MARKDOWN)

2 Answers2

1

If you want to reply on message with command "!ban reason", try this:

@dp.message_handler(AdminFilter(is_chat_admin=True), IsReplyFilter(is_reply=True), commands=['ban'],
                    commands_prefix='!', chat_type=[types.ChatType.SUPERGROUP, types.ChatType.GROUP])
async def ban(message: types.Message):
    command, reason = message.text.split()
    user_chat: types.Chat = await bot.get_chat(message.reply_to_message.from_user.id)
    await message.chat.kick(user_chat.id)
    await message.delete()
    await message.answer(f"User @{user_chat.username} banned. Reason - {reason}")
say8hi
  • 48
  • 4
  • I noticed a significant problem.. The !ban command only works if you write the reason for one problem. For example: !ban Spam And if you write several reasons, then the bot did not react at all. For example: !ban Insults, spam How can I make it possible to specify an unlimited number of reasons? – ForeverOnline Mar 22 '22 at 16:38
  • 1
    `command, reason = message.text.split(" ", 1)`. Try to learn python basics – say8hi Mar 23 '22 at 06:31
0

you can use command, username, reason = message.text.split() to get args from command !ban @NAMEorID Spam then you'll have this args command = '!ban', username = '@NAMEorID', reason = 'Spam'

@dp.message_handler(AdminFilter(is_chat_admin=True), IsReplyFilter(is_reply=True), commands=['ban'],
                    commands_prefix='!', chat_type=[types.ChatType.SUPERGROUP, types.ChatType.GROUP])
async def ban(message: types.Message):
    command, username, reason = message.text.split()
    replied_user = message.reply_to_message.from_user.id
    admin_id = message.from_user.id
    await bot.kick_chat_member(chat_id=message.chat.id, user_id=replied_user)
    await bot.delete_message(chat_id=message.chat.id, message_id=message.message_id)
    await bot.send_message(chat_id=message.chat.id, text=f"User {username} banned. Reason - {reason}")

say8hi
  • 48
  • 4
  • I tried this code, but it doesn't work quite right. When I write the !ban command, the bot sends a message like "User !ban is banned. Reason - spam" That is, it does not send the @tag of the blocked user, but the admin command – ForeverOnline Mar 20 '22 at 08:03
  • You want to reply to the message of user who you want to ban with command "!ban reason"? Or you want to send command "!ban user_id reason" without replying? First method is better in my opinion. – say8hi Mar 21 '22 at 09:08