0

Я использую pytelegrambotapi и Python 3.7.

Я бы хотел, чтобы мой бот проверял у себя наличие админских прав на: удаление сообщений, бан пользователей.

Спасибо заранее!

1 ответ 1

1

Я взял из своего кода. Но думаю суть разберёшь:

def tlg_kick_user(bot, chat_id, user_id):
    '''Telegram Kick (no ban) a user of an specified chat'''
    return_code = 0
    try:
        user_data = bot.getChatMember(chat_id, user_id)
        if (user_data['status'] != "left") and (user_data['status'] != "kicked"):
            bot.kickChatMember(chat_id, user_id)
            bot.unbanChatMember(chat_id, user_id)
            return_code = 1
        else:
            return_code = -1
    except Exception as e:
        printts("[{}] {}".format(chat_id, str(e)))
        if str(e) == "Not enough rights to restrict/unrestrict chat member":
            return_code = -2
        elif str(e) == "User is an administrator of the chat":
            return_code = -3
    return return_code

if new_user["join_retries"] < 5:
                printts("[{}] Captcha not solved, kicking {} ({})...".format(chat_id,
                        new_user["user_name"], new_user["user_id"]))
                # Try to kick the user
                kick_result = tlg_kick_user(bot, new_user["chat_id"], new_user["user_id"])
                if kick_result == 1:
                    # Kick success
                    bot_msg = TEXT[lang]["NEW_USER_KICK"].format(new_user["user_name"])
                    # Increase join retries
                    new_user["join_retries"] = new_user["join_retries"] + 1
                    printts("[{}] Increased join_retries to {}".format(chat_id,
                            new_user["join_retries"]))
                    # Set to auto-remove the kick message too, after a while
                    tlg_send_selfdestruct_msg(bot, chat_id, bot_msg)
                else:
                    # Kick fail
                    printts("[{}] Unable to kick".format(chat_id))
                    if kick_result == -1:
                        # The user is not in the chat
                        bot_msg = ['Юзер не в чате'].format(
                                new_user["user_name"])
                        # Set to auto-remove the kick message too, after a while
                        tlg_send_selfdestruct_msg(bot, chat_id, bot_msg)
                    elif kick_result == -2:
                        # Bot has no privileges to ban
                        bot_msg = ['Нет прав'].format(
                                new_user["user_name"])
                        # Send no rights for kick message without auto-remove
                        try:
                            bot.send_message(chat_id, bot_msg)
                        except Exception as e:
                            printts("[{}] {}".format(chat_id, str(e)))
                    else:
                        # For other reason, the Bot can't ban
                        bot_msg = TEXT[lang]['не могу кикнуть'].format(new_user["user_name"])
                        # Set to auto-remove the kick message too, after a while
                        tlg_send_selfdestruct_msg(bot, chat_id, bot_msg)

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.