Esempio n. 1
0
def info(update: Update, context: CallbackContext):
    """
    Function to get user details
    :param update: object representing the incoming update.
    :param context: object containing data about the command call.
    """
    # get user to display info of
    try:
        user_id, _ = get_user_from_message(update.effective_message)
        user = update.effective_chat.get_member(user_id).user
    except UserError:
        user = update.effective_user
    except UserRecordError as e:
        update.effective_message.reply_text(e.message)
        return

    # make info string
    reply = f"*ID*: `{user.id}`\n"
    if user.first_name:
        reply += f"*First Name*: `{user.first_name}`\n"
    if user.last_name:
        reply += f"*Last Name*: `{user.last_name}`\n"
    if user.username:
        reply += f"*Username*: @{escape_markdown(user.username)}\n\n"
    reply += user.mention_markdown(name="Click here to properly check this kitten out")

    # send user info
    update.effective_message.reply_markdown(reply)
Esempio n. 2
0
def unmute(update: Update, context: CallbackContext):
    """
    Unmute a muted user
    :param update: object representing the incoming update.
    :param context: object containing data about the command call.
    """
    # kwargs to pass to the restrict_chat_member function call
    kwargs = {'chat_id': update.effective_chat.id}

    # get user to un mute
    try:
        user_id, username = get_user_from_message(update.effective_message)
    except UserError:
        update.effective_message.reply_text(
            "Reply to a message by the user or give username of user you want to unmute..."
        )
        return
    except UserRecordError as e:
        update.effective_message.reply_text(e.message)
        return

    # set default permissions
    kwargs['permissions'] = context.bot.get_chat(
        update.effective_chat.id).permissions

    # unmute member
    if remove_muted_member(chat=kwargs['chat_id'], user=kwargs['user_id']):
        context.bot.restrict_chat_member(**kwargs)
        update.effective_message.reply_text(
            f"@{username} can now go nyan nyan")
    else:
        update.effective_message.reply_text(
            "Couldn't remove record from db....")
Esempio n. 3
0
def demote(update: Update, context: CallbackContext):
    """
    Demote a user to remove admin rights
    :param update: object representing the incoming update.
    :param context: object containing data about the command call.
    """
    # check if user has enough perms
    if update.effective_chat.id not in get_command_exception_chats("admin"):
        user = update.effective_chat.get_member(update.effective_user.id)
        if not user.can_promote_members and user.status != "creator":
            update.effective_message.reply_markdown(
                "Ask your sugar daddy to give you perms required to use the method `CanPromoteMembers`."
            )
            return

    # get bot member object to check it's perms
    bot: ChatMember = update.effective_chat.get_member(context.bot.id)

    # check if bot can demote users
    if not bot.can_promote_members:
        update.effective_message.reply_markdown(
            "Ask your sugar daddy to give me perms required to use the method `CanPromoteMembers`."
        )
        return

    # get member to demote
    try:
        user_id, username = get_user_from_message(update.effective_message)
    except UserError:
        update.effective_message.reply_text(
            "Reply to a message by the user or give username of user you want to demote..."
        )
        return
    except UserRecordError as e:
        update.effective_message.reply_text(e.message)
        return

    # demote member
    if update.effective_chat.get_member(user_id).status != "administrator":
        update.effective_message.reply_text(
            "What are you gonna demote a pleb to?")
        return

    try:
        context.bot.promote_chat_member(
            chat_id=update.effective_chat.id,
            user_id=user_id,
            can_change_info=False,
            can_post_messages=False,
            can_edit_messages=False,
            can_delete_messages=False,
            can_invite_users=False,
            can_restrict_members=False,
            can_pin_messages=False,
            can_promote_members=False,
        )

        update.effective_message.reply_markdown(
            emojize(
                f"{mention_markdown(user_id, username)} has been thrown down from the council of admins to rot with the"
                f" rest of you plebs :grinning_cat_face_with_smiling_eyes:"))

    except BadRequest:
        update.effective_message.reply_text(
            "This cat can't meow at it's superiors.... and neither can I change their perms or title."
        )
Esempio n. 4
0
def promote(update: Update, context: CallbackContext):
    """
    Promote a user to give him admin rights
    :param update: object representing the incoming update.
    :param context: object containing data about the command call.
    """
    # check if user has enough perms
    if update.effective_chat.id not in get_command_exception_chats("admin"):
        user = update.effective_chat.get_member(update.effective_user.id)
        if not user.can_promote_members and user.status != "creator":
            update.effective_message.reply_markdown(
                "Ask your sugar daddy to give you perms required to use the method `CanPromoteMembers`."
            )
            return

    # get bot member object to check it's perms
    bot: ChatMember = update.effective_chat.get_member(context.bot.id)

    # check if bot can promote users
    if not bot.can_promote_members:
        update.effective_message.reply_markdown(
            "Ask your sugar daddy to give me perms required to use the method `CanPromoteMembers`."
        )
        return

    # get member to promote
    try:
        user_id, username = get_user_from_message(update.effective_message)
    except UserError:
        update.effective_message.reply_text(
            "Reply to a message by the user or give username of user you want to promote..."
        )
        return
    except UserRecordError as e:
        update.effective_message.reply_text(e.message)
        return

    # promote member
    reply = ""
    try:
        context.bot.promote_chat_member(
            chat_id=update.effective_chat.id,
            user_id=user_id,
            can_change_info=bot.can_change_info,
            can_post_messages=bot.can_post_messages,
            can_edit_messages=bot.can_edit_messages,
            can_delete_messages=bot.can_delete_messages,
            can_invite_users=bot.can_invite_users,
            can_restrict_members=bot.can_restrict_members,
            can_pin_messages=bot.can_pin_messages,
            can_promote_members=bot.can_promote_members,
        )
        reply += f"Everyone say NyaHello to {mention_markdown(user_id, username)}, our new admin!"

        # get all args other than the username
        useful_args = []
        for arg in context.args:
            if username not in arg:
                useful_args.append(arg)

        if useful_args:
            title = " ".join(useful_args)
            context.bot.set_chat_administrator_custom_title(
                update.effective_chat.id, user_id, title)
            reply += f"\nThey have been granted the title of `{title}`."

        update.effective_message.reply_markdown(reply.strip())

    except BadRequest:
        update.effective_message.reply_text(
            "This cat can't meow at it's superiors.... and neither can I change their perms or title."
        )
Esempio n. 5
0
def ban_kick(update: Update, context: CallbackContext):
    """
    ban or kick a user from a chat
    :param update: object representing the incoming update.
    :param context: object containing data about the command call.
    """
    action = "ban" if update.effective_message.text.split(
        None, 1)[0] == "/ban" else "kick"

    # kwargs to pass to the ban_chat_member function call
    kwargs = {'chat_id': update.effective_chat.id}

    # get user to ban
    try:
        user_id, username = get_user_from_message(update.effective_message)
    except UserError:
        update.effective_message.reply_text(
            f"Reply to a message by the user or give username of user you want to {action}..."
        )
        return
    except UserRecordError as e:
        update.effective_message.reply_text(e.message)
        return

    # check if user is trying to ban the bot
    if kwargs['user_id'] == context.bot.id:
        update.effective_message.reply_markdown(
            f"Try to {action} me again, I'll meow meow your buttocks.")
        return

    # check if user is trying to ban an admin
    user = update.effective_chat.get_member(kwargs['user_id'])
    if user.status in ('administrator', 'creator'):
        update.effective_message.reply_markdown(
            f"Try to {action} an admin again, I might just {action} __you__.")
        return

    # get datetime till when we have to mute user
    try:
        kwargs['until_date'] = get_datetime_form_args(context.args, username)
    except TimeFormatException:
        update.effective_message.reply_markdown(
            "Please give the unit of time as one of the following\n\n`m` = minutes\n`h` = hours\n`d` = days"
        )
        return
    except ValueError:
        update.effective_message.reply_text("Time needs to be a number, baka!")
        return

    # ban user
    context.bot.kick_chat_member(**kwargs)
    if action == "kick":
        context.bot.unban_chat_member(kwargs['chat_id'], kwargs['user_id'])

    # announce ban
    reply = (f"{'Banned' if action == 'ban' else 'Kicked'} "
             f"@{escape_markdown(username)} to the litter "
             f":smiling_face_with_horns:")
    if action == "ban":
        reply += "\nIf you want to be added again, bribe an admin with some catnip to add you..."
    if kwargs['until_date']:
        reply += f"\n\nBanned till `{kwargs['until_date'].strftime('%c')} UTC`"

    update.effective_message.reply_markdown(emojize(reply))
Esempio n. 6
0
def mute(update: Update, context: CallbackContext):
    """
    Mute a user, temporarily or permanently
    :param update: object representing the incoming update.
    :param context: object containing data about the command call.
    """
    # kwargs to pass to the restrict_chat_member function call
    kwargs = {'chat_id': update.effective_chat.id}

    # get user to mute
    try:
        user_id, username = get_user_from_message(update.effective_message)
    except UserError:
        update.effective_message.reply_text(
            "Reply to a message by the user or give username of user you want to mute..."
        )
        return
    except UserRecordError as e:
        update.effective_message.reply_text(e.message)
        return

    # check if user is trying to mute an admin
    user = update.effective_chat.get_member(kwargs['user_id'])
    if user.status in ('administrator', 'creator'):
        update.effective_message.reply_text("I can't mute an admin, baka!")
        return

    # set muted permissions
    kwargs['permissions'] = ChatPermissions(
        can_send_messages=False,
        can_send_media_messages=False,
        can_send_other_messages=False,
        can_send_polls=False,
        can_add_web_page_previews=False,
        can_change_info=user.can_change_info,
        can_invite_users=user.can_invite_users,
        can_pin_messages=user.can_pin_messages,
    )

    # get datetime till when we have to mute user
    try:
        kwargs['until_date'] = get_datetime_form_args(context.args, username)
    except TimeFormatException:
        update.effective_message.reply_markdown(
            "Please give the unit of time as one of the following\n\n`m` = minutes\n`h` = hours\n`d` = days"
        )
        return
    except ValueError:
        update.effective_message.reply_text("Time needs to be a number, baka!")
        return

    # add muted member in db
    if add_muted_member(
            chat=kwargs['chat_id'],
            user=kwargs['user_id'],
            username=username,
            until_date=kwargs['until_date']
            if 'until_date' in kwargs.keys() else None,
    ):
        # mute member
        context.bot.restrict_chat_member(**kwargs)
        reply = (
            f"Sewed up @{escape_markdown(username)}'s mouth :smiling_face_with_horns:\nIf you want to be un-muted, "
            f"bribe an admin with some catnip to do it for you...")
        if kwargs['until_date']:
            reply += f" or wait till `{kwargs['until_date'].strftime('%c')} UTC`"

        update.effective_message.reply_markdown(emojize(reply))
    else:
        update.effective_message.reply_text("Couldn't save record in db....")