Exemplo n.º 1
0
def leave_fed(bot: Bot, update: Update, args: List[str]):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    fed_id = sql.get_fed_id(chat.id)

    administrators = chat.get_administrators()

    if user.id in SUDO_USERS:
        pass
    else:
        for admin in administrators:
            status = admin.status
            if status == "creator":
                print(admin)
                if str(admin.user.id) == str(user.id):
                    pass
                else:
                    update.effective_message.reply_text(
                        chat.id, "Only group creator can do it!")
                    return

    if sql.chat_leave_fed(chat.id) == True:
        update.effective_message.reply_text(chat.id, "Leaved from fed!")
    else:
        update.effective_message.reply_text(chat.id, "Error!")
Exemplo n.º 2
0
def set_frules(bot: Bot, update: Update, args: List[str]):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    fed_id = sql.get_fed_id(chat.id)

    if is_user_fed_admin(fed_id, user.id) == False:
        update.effective_message.reply_text(chat.id,
                                            "Only fed admins can do this!")
        return

    if len(args) >= 1:
        msg = update.effective_message  # type: Optional[Message]
        raw_text = msg.text
        args = raw_text.split(
            None, 1)  # use python's maxsplit to separate cmd and args
        if len(args) == 2:
            txt = args[1]
            offset = len(txt) - len(
                raw_text)  # set correct offset relative to command
            markdown_rules = markdown_parser(txt,
                                             entities=msg.parse_entities(),
                                             offset=offset)
        sql.set_frules(fed_id, markdown_rules)
        update.effective_message.reply_text(chat.id,
                                            "Rules setuped for this fed!")
    else:
        update.effective_message.reply_text(chat.id, "Please write rules!")
Exemplo n.º 3
0
def unfban(bot: Bot, update: Update, args: List[str]):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    message = update.effective_message  # type: Optional[Message]
    fed_id = sql.get_fed_id(chat.id)

    if is_user_fed_admin(fed_id, user.id) == False:
        update.effective_message.reply_text(chat.id,
                                            "Only fed admins can do this!")
        return

    user_id = extract_user(message, args)
    if not user_id:
        message.reply_text(chat.id,
                           "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(chat.id, "That's not a user!")
        return

    if sql.get_fban_user(fed_id, user_id) == False:
        message.reply_text(chat.id, "This user is not gbanned!")
        return

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

    message.reply_text(
        chat.id, "I'll give {} a second chance in this federation.").format(
            user_chat.first_name)

    h = sql.all_fed_chats(fed_id)

    for O in h:
        try:
            member = bot.get_chat_member(O, user_id)
            if member.status == 'kicked':
                bot.unban_chat_member(O, user_id)

        except BadRequest as excp:

            if excp.message in UNFBAN_ERRORS:
                pass
            else:
                message.reply_text(chat.id,
                                   "Could not un-fban due to: {}").format(
                                       excp.message)
                return

        except TelegramError:
            pass

        try:
            sql.un_fban_user(fed_id, user_id)
        except:
            pass

    message.reply_text(chat.id, "Person has been un-fbanned.")
Exemplo n.º 4
0
def fed_ban(bot: Bot, update: Update, args: List[str]):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    fed_id = sql.get_fed_id(chat.id)

    if is_user_fed_admin(fed_id, user.id) == False:
        update.effective_message.reply_text(chat.id,
                                            "Only fed admins can do this!")
        return

    message = update.effective_message  # type: Optional[Message]

    user_id, reason = extract_user_and_text(message, args)

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

    if user_id == bot.id:
        message.reply_text(
            chat.id,
            "You can't fban me, better hit your head against the wall, it's more fun."
        )
        return

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

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

    message.reply_text(chat.id, "Start fbanning!")

    if reason == "":
        reason = "no reason"

    sql.fban_user(fed_id, user_id, reason)

    h = sql.all_fed_chats(fed_id)
    for O in h:
        try:
            bot.kick_chat_member(O, user_id)
            #text = (chat.id, "I should gban {}, but it's only test fban, right? So i let him live.").format(O)
            text = "Fbanning {}".format(user_id)
            #message.reply_text(text)
        except BadRequest as excp:
            if excp.message in FBAN_ERRORS:
                pass
            else:
                message.reply_text(
                    chat.id, "Could not fban due to: {}").format(excp.message)
                return
        except TelegramError:
            pass
Exemplo n.º 5
0
def get_frules(bot: Bot, update: Update, args: List[str]):
    chat = update.effective_chat  # type: Optional[Chat]
    fed_id = sql.get_fed_id(chat.id)
    rules = sql.get_frules(fed_id).rules
    print(rules)
    text = "*Rules in this fed:*\n"
    text += rules
    update.effective_message.reply_text(chat.id,
                                        text,
                                        parse_mode=ParseMode.MARKDOWN)
Exemplo n.º 6
0
def fed_info(bot: Bot, update: Update, args: List[str]):

    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    fed_id = sql.get_fed_id(chat.id)

    if not fed_id:
        update.effective_message.reply_text(
            chat.id, "This group not in any federation!")
        return

    if is_user_fed_admin(fed_id, user.id) == False:
        update.effective_message.reply_text(chat.id,
                                            "Only fed admins can do this!")
        return

    print(fed_id)
    user = update.effective_user  # type: Optional[Chat]
    chat = update.effective_chat  # type: Optional[Chat]
    info = sql.get_fed_info(fed_id)

    text = "<b>Federation INFO:</b>"
    text += "\nName: <code>{}</code>".format(info.fed_name)
    text += "\nID: <code>{}</code>".format(fed_id)

    R = 0
    for O in sql.get_all_fban_users(fed_id):
        R = R + 1

    text += "\nBanned: <code>{}</code>".format(R)
    text += "\n\n<b>Chats:</b>"
    h = sql.all_fed_chats(fed_id)
    for O in h:
        cht = bot.get_chat(O)
        text += "\n• {} (<code>{}</code>)".format(cht.title, O)

    text += "\n\n<b>Admins:</b>"
    user = bot.get_chat(info.owner_id)
    text += "\n• {} - <code>{}</code> (Creator)".format(
        user.first_name, user.id)

    h = sql.all_fed_users(fed_id)
    for O in h:
        user = bot.get_chat(O)
        text += "\n• {} - <code>{}</code>".format(user.first_name, user.id, O)

    # Chance 1/5 to add this string to /fedinfo
    # You can remove this or reduce the percentage, but if you really like my work leave this.
    num = random.randint(1, 5)
    print("random ", num)
    if num == 3:
        text += "\n\nstella federation"

    update.effective_message.reply_text(text, parse_mode=ParseMode.HTML)
Exemplo n.º 7
0
def __user_info__(user_id, chat_id):
    fed_id = sql.get_fed_id(chat_id)
    if fed_id:
        fban = sql.get_fban_user(fed_id, user_id)
        if not fban == False:
            text = "Banned in current federation - <b>Yes</b>"
            text += "\n<b>Reason:</b> {}".format(fban)
        else:
            text = "Banned in current federation - <b>No</b>"
    else:
        text = ""
    return text
Exemplo n.º 8
0
def welcome_fed(bot, update):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]

    fed_id = sql.get_fed_id(chat.id)
    fban = fban = sql.get_fban_user(fed_id, user.id)
    if not fban == False:
        update.effective_message.reply_text(
            chat.id,
            "This user if banned in current federation! I will remove him.")
        bot.kick_chat_member(chat.id, user.id)
        return True
    else:
        return False
Exemplo n.º 9
0
def del_fed(bot: Bot, update: Update, args: List[str]):

    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    fed_id = sql.get_fed_id(chat.id)

    if is_user_fed_owner(fed_id, user.id) == False:
        update.effective_message.reply_text(chat.id,
                                            "Only fed owner can do this.")
        return

    if len(args) >= 1:
        fed_id = args[0]
        sql.del_fed(fed_id, chat.id)
        update.effective_message.reply_text(chat.id, "Deleted!")
    else:
        update.effective_message.reply_text(
            chat.id, "Please write federation id to remove!")
Exemplo n.º 10
0
def user_demote_fed(bot: Bot, update: Update, args: List[str]):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    fed_id = sql.get_fed_id(chat.id)

    if is_user_fed_owner(fed_id, user.id) == False:
        update.effective_message.reply_text(chat.id,
                                            "Only fed owner can do this!")
        return

    msg = update.effective_message  # type: Optional[Message]
    user_id = extract_user(msg, args)
    if user_id:
        user = bot.get_chat(user_id)

    elif not msg.reply_to_message and not args:
        user = msg.from_user

    elif not msg.reply_to_message and (
            not args or
        (len(args) >= 1 and not args[0].startswith("@")
         and not args[0].isdigit()
         and not msg.parse_entities([MessageEntity.TEXT_MENTION]))):
        msg.reply_text(chat.id, "I can't extract a user from this.")
        return

    else:
        return

    if sql.search_user_in_fed(fed_id, user_id) == False:
        update.effective_message.reply_text(
            chat.id,
            "I can't demote user which not a fed admin! If you wanna bring him to tears, promote him and demote."
        )
        return

    res = sql.user_demote_fed(fed_id, user_id)
    if res == True:
        update.effective_message.reply_text(chat.id, "Get out of here!")
    else:
        update.effective_message.reply_text(
            chat.id, "I can not remove him, I am powerless!")
Exemplo n.º 11
0
def broadcast(bot: Bot, update: Update, args: List[str]):
    to_send = update.effective_message.text.split(None, 1)
    if len(to_send) >= 2:
        chat = update.effective_chat  # type: Optional[Chat]
        fed_id = sql.get_fed_id(chat.id)
        chats = sql.all_fed_chats(fed_id)
        failed = 0
        for Q in chats:
            try:
                bot.sendMessage(Q, to_send[1])
                sleep(0.1)
            except TelegramError:
                failed += 1
                LOGGER.warning("Couldn't send broadcast to %s, group name %s",
                               str(chat.chat_id), str(chat.chat_name))

        update.effective_message.reply_text(
            chat.id,
            "Federations Broadcast complete. {} groups failed to receive the message, probably "
            "due to left federation.").format(failed)
Exemplo n.º 12
0
def user_join_fed(bot: Bot, update: Update, args: List[str]):

    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    fed_id = sql.get_fed_id(chat.id)

    if is_user_fed_owner(fed_id, user.id) == False:
        update.effective_message.reply_text(chat.id,
                                            "Only fed owner can do this!")
        return

    msg = update.effective_message  # type: Optional[Message]
    user_id = extract_user(msg, args)
    if user_id:
        user = bot.get_chat(user_id)

    elif not msg.reply_to_message and not args:
        user = msg.from_user

    elif not msg.reply_to_message and (
            not args or
        (len(args) >= 1 and not args[0].startswith("@")
         and not args[0].isdigit()
         and not msg.parse_entities([MessageEntity.TEXT_MENTION]))):
        msg.reply_text(chat.id, "I can't extract a user from this.")
        return

    else:
        return

    print(sql.search_user_in_fed(fed_id, user_id))

    if not sql.search_user_in_fed(fed_id, user_id) == False:
        update.effective_message.reply_text(
            chat.id,
            "I can't promote user which already a fed admin! But I can demote him."
        )
        return

    res = sql.user_join_fed(fed_id, user_id)
    update.effective_message.reply_text(chat.id, "Promoted to federation!")