コード例 #1
0
    async def start(self):
        await super().start()

        me = await self.get_me()  # Get bot info from pyrogram client
        LOGGER.info("Starting bot...")

        """Redis Content Setup!"""
        await self.get_admins()  # Load admins in cache
        set_key("SUPPORT_STAFF", SUPPORT_STAFF)  # Load SUPPORT_STAFF in cache
        set_key("BOT_ID", int(me.id))  # Save Bot ID in Redis!
        """Redis Content Setup!"""

        # Show in Log that bot has started
        LOGGER.info(
            f"Pyrogram v{__version__}\n(Layer - {layer}) started on @{me.username}"
        )
        LOGGER.info(load_cmds(ALL_PLUGINS))
        LOGGER.info(f"Redis Keys Loaded: {allkeys()}")

        # Send a message to MESSAGE_DUMP telling that the bot has started and has loaded all plugins!
        await self.send_message(
            MESSAGE_DUMP,
            (
                f"<b><i>Bot started on Pyrogram v{__version__} (Layer - {layer})</i></b>\n\n"
                "<b>Loaded Plugins:</b>\n"
                f"<i>{list(HELP_COMMANDS.keys())}</i>\n"
                "<b>Redis Keys Loaded:</b>\n"
                f"<i>{allkeys()}</i>"
            ),
        )
コード例 #2
0
async def demote_usr(c: Alita, m: Message):

    _ = GetLang(m).strs

    res = await admin_check(c, m)
    if not res:
        return

    from_user = await m.chat.get_member(m.from_user.id)

    if len(m.text.split()) == 1 and not m.reply_to_message:
        await m.reply_text("Whom should I demote?\nSpecify a user first.")
        return

    # If user does not have permission to demote other users, return
    if from_user.can_promote_members or from_user.status == "creator":

        user_id, user_first_name = extract_user(m)
        try:
            await m.chat.promote_member(
                user_id=user_id,
                can_change_info=False,
                can_delete_messages=False,
                can_restrict_members=False,
                can_invite_users=False,
                can_pin_messages=False,
            )
            await m.reply_text(
                _("admin.demoted").format(
                    demoter=mention_html(m.from_user.first_name, m.from_user.id),
                    demoted=mention_html(user_first_name, user_id),
                    chat_title=m.chat.title,
                )
            )

            # ----- Add admin to redis cache! -----
            ADMINDICT = get_key("ADMINDICT")  # Load ADMINDICT from string
            adminlist = []
            async for i in m.chat.iter_members(filter="administrators"):
                adminlist.append(i.user.id)
            ADMINDICT[str(m.chat.id)] = adminlist
            set_key("ADMINDICT", ADMINDICT)

        except errors.ChatAdminRequired:
            await m.reply_text(_("admin.notadmin"))
        except errors.RightForbidden:
            await m.reply_text("I don't have enough rights to demote this user.")
        except Exception as ef:
            await m.reply_text(f"<code>{ef}</code>\nReport to @{SUPPORT_GROUP}")
            LOGGER.error(ef)

        return

    await m.reply_text(_("admin.nodemoteperm"))
    return
コード例 #3
0
async def promote_usr(c: Alita, m: Message):

    res = await admin_check(c, m)
    if not res:
        return

    from_user = await m.chat.get_member(m.from_user.id)

    # If user does not have permission to promote other users, return
    if from_user.can_promote_members or from_user.status == "creator":

        user_id, user_first_name = await extract_user(c, m)
        try:
            await m.chat.promote_member(
                user_id=user_id,
                can_change_info=False,
                can_delete_messages=True,
                can_restrict_members=True,
                can_invite_users=True,
                can_pin_messages=True,
            )
            await m.reply_text(
                _("admin.promoted").format(
                    promoter=mention_html(m.from_user.first_name,
                                          m.from_user.id),
                    promoted=mention_html(user_first_name, user_id),
                    chat_title=m.chat.title,
                ))

            # ----- Add admin to redis cache! -----
            ADMINDICT = get_key("ADMINDICT")  # Load ADMINDICT from string
            adminlist = []
            async for i in m.chat.iter_members(filter="administrators"):
                adminlist.append(i.user.id)
            ADMINDICT[str(m.chat.id)] = adminlist
            set_key("ADMINDICT", ADMINDICT)

        except errors.ChatAdminRequired:
            await m.reply_text(_("admin.notadmin"))
        except Exception as ef:
            await m.reply_text(_("admin.useadmincache"))
            LOGGER.error(ef)

        return

    await m.reply_text(_("admin.nopromoteperm"))
    return
コード例 #4
0
    async def get_admins(self):
        LOGGER.info("Begin caching admins...")
        begin = time.time()
        c = self

        # Flush Redis data
        try:
            flushredis()
        except Exception as ef:
            LOGGER.error(ef)

        # Get bot info
        me = await self.get_me()

        all_chats = userdb.get_all_chats() or []  # Get list of all chats
        LOGGER.info(f"{len(all_chats)} chats loaded.")
        ADMINDICT = {}
        for chat in all_chats:
            adminlist = []
            try:
                async for i in c.iter_chat_members(
                    chat_id=chat.chat_id, filter="administrators"
                ):
                    adminlist.append(i.user.id)

                ADMINDICT[str(chat.chat_id)] = adminlist  # Remove the last space

                LOGGER.info(
                    f"Set {len(adminlist)} admins for {chat.chat_id}\n{adminlist}"
                )
                del adminlist  # Delete list var
            except Exception as ef:
                LOGGER.error(ef)

        try:
            set_key("ADMINDICT", ADMINDICT)
            del ADMINDICT
            end = time.time()
            LOGGER.info(f"Set admin list cache!\nTime Taken: {round(end-begin, 2)}s")
        except Exception as ef:
            LOGGER.error(f"Could not set ADMINDICT!\n{ef}")
コード例 #5
0
async def reload_admins(c: Alita, m: Message):

    res = await admin_check(c, m)
    if not res:
        return

    ADMINDICT = get_key("ADMINDICT")  # Load ADMINDICT from string

    try:
        adminlist = []
        async for i in m.chat.iter_members(filter="administrators"):
            adminlist.append(i.user.id)
        ADMINDICT[str(m.chat.id)] = adminlist
        set_key("ADMINDICT", ADMINDICT)
        await m.reply_text(_("admin.reloadedadmins"))
        LOGGER.info(f"Reloaded admins for {m.chat.title}({m.chat.id})")
    except Exception as ef:
        await m.reply_text(_("admin.useadmincache"))
        LOGGER.error(ef)

    return