Exemplo n.º 1
0
async def handler(event):
    # TODO: exempt admins from locks
    # check for "lock" "bots"
    if is_locked(event.chat_id, "bots"):
        # bots are limited Telegram accounts,
        # and cannot join by themselves
        if event.user_added:
            users_added_by = event.action_message.from_id
            is_ban_able = False
            rights = types.ChatBannedRights(until_date=None,
                                            view_messages=True)
            added_users = event.action_message.action.users
            for user_id in added_users:
                user_obj = await client.get_entity(user_id)
                if user_obj.bot:
                    is_ban_able = True
                    try:
                        await client(
                            functions.channels.EditBannedRequest(
                                event.chat_id, user_obj, rights))
                    except Exception as e:
                        await event.reply(
                            "I don't seem to have ADMIN permission here. \n`{}`"
                            .format(str(e)))
                        update_lock(event.chat_id, "bots", False)
                        break
Exemplo n.º 2
0
async def handler(event):
    # Space weirdness in regex required because argument is optional and other
    # commands start with ".lock"
    if event.fwd_from:
        return
    input_str = event.pattern_match.group("target")
    peer_id = event.chat_id
    if input_str in (("bots", "commands", "email", "forward", "url")):
        update_lock(peer_id, input_str, True)
        await event.edit("Locked {}".format(input_str))
    else:
        msg = None
        media = None
        sticker = None
        gif = None
        gamee = None
        ainline = None
        gpoll = None
        adduser = None
        cpin = None
        changeinfo = None
        if input_str:
            if "msg" in input_str:
                msg = True
            if "media" in input_str:
                media = True
            if "sticker" in input_str:
                sticker = True
            if "gif" in input_str:
                gif = True
            if "gamee" in input_str:
                gamee = True
            if "ainline" in input_str:
                ainline = True
            if "gpoll" in input_str:
                gpoll = True
            if "adduser" in input_str:
                adduser = True
            if "cpin" in input_str:
                cpin = True
            if "changeinfo" in input_str:
                changeinfo = True
        banned_rights = types.ChatBannedRights(
            until_date=None,
            # view_messages=None,
            send_messages=msg,
            send_media=media,
            send_stickers=sticker,
            send_gifs=gif,
            send_games=gamee,
            send_inline=ainline,
            send_polls=gpoll,
            invite_users=adduser,
            pin_messages=cpin,
            change_info=changeinfo,
        )
        try:
            result = await client(  # pylint:disable=E0602
                functions.messages.EditChatDefaultBannedRightsRequest(
                    peer=peer_id, banned_rights=banned_rights))
        except Exception as e:  # pylint:disable=C0103,W0703
            await event.edit(str(e))
        else:
            await event.edit(
                "Current Chat Default Permissions Changed Successfully, in API"
            )
Exemplo n.º 3
0
def new_edit_permissions(bot, entity, user, until_date, permissions: ChatPermissionsModel):
    try:
        result = await bot.edit_permissions(
            entity=entity,
            user=user,  # is chat default
            until_date=None,  # is chat default
            view_messages = True,
            send_messages = permissions.can_send_messages,
            send_media = permissions.can_send_media_messages,
            send_stickers = permissions.can_send_other_messages,
            send_gifs = permissions.can_send_other_messages,
            send_games = permissions.can_send_other_messages,
            send_inline = permissions.can_send_other_messages,
            send_polls = permissions.can_send_polls,
            change_info = permissions.can_change_info,
            invite_users = permissions.can_invite_users,
            pin_messages = permissions.can_pin_messages,
            embed_links = permissions.can_add_web_page_previews,
        )
        logger.warning('It worked, so telethon was updated? Maintainer, remove compatibility workaround, pls.')
    except TypeError:

        from telethon import helpers
        from telethon.tl import types
        from telethon.tl import functions

        entity = entity
        view_messages = True
        send_messages = permissions.can_send_messages
        send_media = permissions.can_send_media_messages
        send_stickers = permissions.can_send_other_messages
        send_gifs = permissions.can_send_other_messages
        send_games = permissions.can_send_other_messages
        send_inline = permissions.can_send_other_messages
        send_polls = permissions.can_send_polls
        change_info = permissions.can_change_info
        invite_users = permissions.can_invite_users
        pin_messages = permissions.can_pin_messages
        embed_links = permissions.can_add_web_page_previews

        entity=await bot.get_input_entity(entity)
        ty = helpers._entity_type(entity)
        if ty != helpers._EntityType.CHANNEL:
            raise ValueError('You must pass either a channel or a supergroup')

        rights = types.ChatBannedRights(
            until_date=until_date,
            view_messages=not view_messages,
            send_messages=not send_messages,
            send_media=not send_media,
            send_stickers=not send_stickers,
            send_gifs=not send_gifs,
            send_games=not send_games,
            send_inline=not send_inline,
            send_polls=not send_polls,
            change_info=not change_info,
            invite_users=not invite_users,
            pin_messages=not pin_messages,
            embed_links=not embed_links,
        )

        if user is None:
            return await bot(functions.messages.EditChatDefaultBannedRightsRequest(
                peer=entity,
                banned_rights=rights
            ))

        user = await bot.get_input_entity(user)
        ty = helpers._entity_type(user)
        if ty != helpers._EntityType.USER:
            raise ValueError('You must pass a user entity')

        if isinstance(user, types.InputPeerSelf):
            raise ValueError('You cannot restrict yourself')

        result = await bot(functions.channels.EditBannedRequest(
            channel=entity,
            user_id=user,
            banned_rights=rights
        ))