Please Subscribe To My YouTube Channel Subscribe Now!

Add Maintenance feature in any repository

Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated

Maintenance Feature

Maintenance helps you to enable or disable maintenance mode in your Telegram bot. It allows you to pause bot functions for updates, testing, or troubleshooting. The mode status is saved in MongoDB so it stays active even after a restart. Only the admin can use this command to manage bot availability.

Follow the below steps to maintenance feature in any Repo.

1. Create a new file name 'maintenance.py'.

from pyrogram import Client, filters
from pyrogram.types import Message
from config import ADMIN, DB_URI, DB_NAME
from motor.motor_asyncio import AsyncIOMotorClient

# MongoDB setup
mongo_client = AsyncIOMotorClient(DB_URI)
db = mongo_client[DB_NAME]
settings_col = db["settings"]

# --- DB Helpers ---
async def get_maintenance() -> bool:
    data = await settings_col.find_one({"_id": "maintenance"})
    return data.get("status", False) if data else False

async def set_maintenance(status: bool):
    await settings_col.update_one(
        {"_id": "maintenance"},
        {"$set": {"status": status}},
        upsert=True
    )

# --- Command ---
@Client.on_message(filters.command("maintenance") & filters.user(ADMIN))
async def maintenance_cmd(_, m: Message):
    args = m.text.split(maxsplit=1)
    if len(args) < 2:
        return await m.reply("Usage: /maintenance [on/off]")
    status = args[1].lower()
    if status == "on":
        if await get_maintenance():
            return await m.reply("⚠️ Maintenance mode is already enabled.")
        await set_maintenance(True)
        return await m.reply("✅ Maintenance mode **enabled**.")
    elif status == "off":
        if not await get_maintenance():
            return await m.reply("⚠️ Maintenance mode is already disabled.")
        await set_maintenance(False)
        return await m.reply("❌ Maintenance mode **disabled**.")
    else:
        await m.reply("Invalid status. Use 'on' or 'off'.")

2. Now add this code in your main function of bot & don't forget to import necessary modules.

    if await get_maintenance() and message.from_user.id != ADMIN:
        await message.delete()
        return await message.reply_text("**🛠️ Bot is Under Maintenance**", reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Support", user_id=int(ADMIN))]]))

Note: Change functions names according to your repository.

Important

If you still face any issues, then click on the below button.

Copying the post and using it without permission is strictly prohibited.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.