Exemple #1
0
def chats(update: Update, context: CallbackContext):
    all_chats = sql.get_all_chats() or []
    chatfile = 'List of chats.\n0. Chat name | Chat ID | Members count | Invitelink\n'
    P = 1
    for chat in all_chats:
        try:
            curr_chat = bot.getChat(chat.chat_id)
            bot_member = curr_chat.get_member(bot.id)
            chat_members = curr_chat.get_members_count(bot.id)
            if bot_member.can_invite_users:
                invitelink = bot.exportChatInviteLink(chat.chat_id)
            else:
                invitelink = "0"
            chatfile += "{}. {} | {} | {} | {}\n".format(
                P, chat.chat_name, chat.chat_id, chat_members, invitelink)
            P = P + 1
        except:
            pass

    with BytesIO(str.encode(chatfile)) as output:
        output.name = "chatlist.txt"
        update.effective_message.reply_document(
            document=output,
            filename="chatlist.txt",
            caption="Here is the list of chats in my database.")
def get_muted_chats(bot: Bot, update: Update, leave: bool = False):
    chat_id = update.effective_chat.id
    chats = user_sql.get_all_chats()
    muted_chats, progress = 0, 0
    chat_list = []
    progress_message = None

    for chat in chats:

        if ((100 * chats.index(chat)) / len(chats)) > progress:
            progress_bar = f"{progress}% Completed In Getting Muted Chats."
            if progress_message:
                try:
                    bot.editMessageText(
                        progress_bar, chat_id, progress_message.message_id
                    )
                except:
                    pass
            else:
                progress_message = bot.sendMessage(chat_id, progress_bar)
            progress += 5

        cid = chat.chat_id
        sleep(0.1)

        try:
            bot.send_chat_action(cid, "TYPING", timeout=120)
        except (BadRequest, Unauthorized):
            muted_chats += +1
            chat_list.append(cid)
        except:
            pass

    try:
        progress_message.delete()
    except:
        pass

    if not leave:
        return muted_chats
    else:
        for muted_chat in chat_list:
            sleep(0.1)
            try:
                bot.leaveChat(muted_chat, timeout=120)
            except:
                pass
            user_sql.rem_chat(muted_chat)
        return muted_chats
Exemple #3
0
def broadcast(update: Update, context: CallbackContext):
    to_send = update.effective_message.text.split(None, 1)

    if len(to_send) >= 2:
        to_group = False
        to_user = False
        if to_send[0] == '/broadcastgroups':
            to_group = True
        if to_send[0] == '/broadcastusers':
            to_user = True
        else:
            to_group = to_user = True
        chats = sql.get_all_chats() or []
        users = get_all_users()
        failed = 0
        failed_user = 0
        if to_group:
            for chat in chats:
                try:
                    context.bot.sendMessage(int(chat.chat_id),
                                            to_send[1],
                                            parse_mode="MARKDOWN",
                                            disable_web_page_preview=True)
                    sleep(0.1)
                except TelegramError:
                    failed += 1
        if to_user:
            for user in users:
                try:
                    context.bot.sendMessage(int(user.user_id),
                                            to_send[1],
                                            parse_mode="MARKDOWN",
                                            disable_web_page_preview=True)
                    sleep(0.1)
                except TelegramError:
                    failed_user += 1
        update.effective_message.reply_text(
            f"Broadcast complete.\nGroups failed: {failed}.\nUsers failed: {failed_user}."
        )
def gkick(update, context):
    message = update.effective_message
    args = context.args
    bot = context.bot
    user_id = extract_user(message, args)
    try:
        user_chat = context.bot.get_chat(user_id)
    except BadRequest as excp:
        if excp.message in GKICK_ERRORS:
            pass
        else:
            message.reply_text("Unexpected Error!")
            context.bot.send_message(
                ERROR_DUMP,
                "User cannot be Globally kicked because: {}".format(
                    excp.message))
            return
    except TelegramError:
        pass

    if not user_id or int(user_id) == 777000:
        message.reply_text("You don't seem to be referring to a user.")
        return

    if int(user_id) in OFFICERS:
        message.reply_text("Error! (^_-)")
        return

    if user_id == context.bot.id:
        message.reply_text("Error! (¯―¯٥)")
        return

    chats = get_all_chats()
    banner = update.effective_user  # type: Optional[User]

    message.reply_text("Globally kicking user @{}".format(user_chat.username))
    sql.gkick_user(user_id, user_chat.username, 1)
    for chat in chats:
        try:
            member = context.bot.get_chat_member(chat.chat_id, user_id)
            if member.can_send_messages is False:
                context.bot.unban_chat_member(
                    chat.chat_id, user_id)  # Unban_member = kick (and not ban)
                context.bot.restrict_chat_member(
                    chat.chat_id,
                    user_id,
                    permissions=ChatPermissions(can_send_messages=False))
            else:
                context.bot.unban_chat_member(chat.chat_id, user_id)
        except BadRequest as excp:
            if excp.message in GKICK_ERRORS:
                pass
            else:
                message.reply_text("Unexpected Error!")
                context.bot.send_message(
                    ERROR_DUMP,
                    "User cannot be Globally kicked because: {}".format(
                        excp.message))
                return
        except TelegramError:
            pass
Exemple #5
0
def gmute(update, context):
    message = update.effective_message  # type: Optional[Message]
    chat = update.effective_chat
    args = context.args
    user_id, reason = extract_user_and_text(message, args)

    if not user_id:
        message.reply_text("You don't seem to be referring to a user.")
        return

    if int(user_id) in OFFICERS:
        message.reply_text("I Can't Gmute My Officer.")
        return

    if user_id == context.bot.id:
        message.reply_text("I can't gmute myself.")
        return

    if not reason:
        message.reply_text(
            "Please give a reason why are you want to gmute this user!")
        return

    try:
        user_chat = context.bot.get_chat(user_id)
    except BadRequest as excp:
        message.reply_text(excp.message)
        return

    if user_chat.type != 'private':
        message.reply_text("That's not a user!")
        return

    if sql.is_user_gmuted(user_id):
        if not reason:
            message.reply_text(
                "This user is already gmuted; I'd change the reason, but you haven't given me one..."
            )
            return

        success = sql.update_gmute_reason(
            user_id, user_chat.username or user_chat.first_name, reason)
        if success:
            message.reply_text(
                "This user is already gmuted; I've gone and updated the gmute reason though!"
            )
        else:
            message.reply_text("I thought this person was gmuted.")

        return

    message.reply_text("Gets duct tape ready 😉")

    muter = update.effective_user  # type: Optional[User]

    sql.gmute_user(user_id, user_chat.username or user_chat.first_name, reason)

    chats = get_all_chats()
    for chat in chats:
        chat_id = chat.chat_id

        # Check if this group has disabled gmutes
        if not sql.does_chat_gmute(chat_id):
            continue

        try:
            context.bot.restrict_chat_member(
                chat_id,
                user_id,
                permissions=ChatPermissions(can_send_messages=False))
        except BadRequest as excp:
            if excp.message == "User is an administrator of the chat":
                pass
            elif excp.message == "Chat not found":
                pass
            elif excp.message == "Not enough rights to restrict/unrestrict chat member":
                pass
            elif excp.message == "User_not_participant":
                pass
            elif excp.message == "Peer_id_invalid":  # Suspect this happens when a group is suspended by telegram.
                pass
            elif excp.message == "Group chat was deactivated":
                pass
            elif excp.message == "Need to be inviter of a user to kick it from a basic group":
                pass
            elif excp.message == "Chat_admin_required":
                pass
            elif excp.message == "Only the creator of a basic group can kick group administrators":
                pass
            elif excp.message == "Method is available only for supergroups":
                pass
            elif excp.message == "Can't demote chat creator":
                pass
            else:
                message.reply_text("Unexpected Error!")
                context.bot.send_message(
                    ERROR_DUMP,
                    "Could not gmute due to: {}".format(excp.message))
                sql.ungmute_user(user_id)
                return
        except TelegramError:
            pass

    message.reply_text("They won't be talking again anytime soon.")
Exemple #6
0
def ungmute(update, context):
    message = update.effective_message  # type: Optional[Message]
    bot = context.bot
    args = context.args
    user_id = extract_user(message, args)
    if not user_id:
        message.reply_text("You don't seem to be referring to a user.")
        return

    user_chat = bot.get_chat(user_id)
    if user_chat.type != 'private':
        message.reply_text("That's not a user!")
        return

    if not sql.is_user_gmuted(user_id):
        message.reply_text("This user is not gmuted!")
        return

    muter = update.effective_user  # type: Optional[User]

    message.reply_text("I'll let {} speak again, globally.".format(
        user_chat.first_name))

    chats = get_all_chats()
    for chat in chats:
        chat_id = chat.chat_id

        # Check if this group has disabled gmutes
        if not sql.does_chat_gmute(chat_id):
            continue

        try:
            member = context.bot.get_chat_member(chat_id, user_id)
            if member.status == 'restricted':
                context.bot.restrict_chat_member(
                    chat_id,
                    int(user_id),
                    permissions=ChatPermissions(
                        can_send_messages=True,
                        can_invite_users=True,
                        can_pin_messages=True,
                        can_send_polls=True,
                        can_change_info=True,
                        can_send_media_messages=True,
                        can_send_other_messages=True,
                        can_add_web_page_previews=True,
                    ))

        except BadRequest as excp:
            if excp.message == "User is an administrator of the chat":
                pass
            elif excp.message == "Chat not found":
                pass
            elif excp.message == "Not enough rights to restrict/unrestrict chat member":
                pass
            elif excp.message == "User_not_participant":
                pass
            elif excp.message == "Method is available for supergroup and channel chats only":
                pass
            elif excp.message == "Not in the chat":
                pass
            elif excp.message == "Channel_private":
                pass
            elif excp.message == "Chat_admin_required":
                pass
            else:
                message.reply_text("Unexpected Error!")
                bot.send_message(
                    ERROR_DUMP,
                    "Could not un-gmute due to: {}".format(excp.message))
                return
        except TelegramError:
            pass

    sql.ungmute_user(user_id)

    message.reply_text("Person has been un-gmuted.")