skip to Main Content

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

Answers


  1. 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}")
    
    
    Login or Signup to reply.
  2. 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}")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search