import re

filepath = "/root/api-one/Telegram-Bot/index.js"
with open(filepath, "r") as f:
    content = f.read()

bot_catch_orig = """bot.catch((err, ctx) => {
    const errMsg = err.message || "";
    // Ignore minor/harmless Telegram UI errors to prevent log cluttering
    if (
        errMsg.includes("message is not modified") ||
        errMsg.includes("message to edit not found") ||
        errMsg.includes("query is too old") ||
        errMsg.includes("bot was blocked by the user") ||
        errMsg.includes("user is deactivated")
    ) {
        return; // Silent ignore
    }"""

bot_catch_new = """bot.catch((err, ctx) => {
    const errMsg = (err.message || "").toLowerCase() + (err.stack || "").toLowerCase();
    // Ignore minor/harmless Telegram UI errors to prevent log cluttering
    if (
        errMsg.includes("message is not modified") ||
        errMsg.includes("message to edit not found") ||
        errMsg.includes("query is too old") ||
        errMsg.includes("bot was blocked by the user") ||
        errMsg.includes("user is deactivated") ||
        errMsg.includes("econnreset") ||
        errMsg.includes("etimedout") ||
        errMsg.includes("socket hang up") ||
        errMsg.includes("fetcherror")
    ) {
        return; // Silent ignore
    }"""

unhandled_orig = """process.on('unhandledRejection', (reason, promise) => {
    const errMsg = reason ? (reason.message || reason.description || reason.stack || reason.toString()) : "";
    if (
        errMsg.includes("message is not modified") ||
        errMsg.includes("message to edit not found") ||
        errMsg.includes("query is too old") ||
        errMsg.includes("bot was blocked by the user") ||
        errMsg.includes("user is deactivated") ||
        errMsg.includes("bot was kicked")
    ) {
        return; // Silent ignore
    }"""

unhandled_new = """process.on('unhandledRejection', (reason, promise) => {
    const errMsg = reason ? (String(reason.message) + String(reason.description) + String(reason.stack) + String(reason)).toLowerCase() : "";
    if (
        errMsg.includes("message is not modified") ||
        errMsg.includes("message to edit not found") ||
        errMsg.includes("query is too old") ||
        errMsg.includes("bot was blocked by the user") ||
        errMsg.includes("user is deactivated") ||
        errMsg.includes("bot was kicked") ||
        errMsg.includes("econnreset") ||
        errMsg.includes("etimedout") ||
        errMsg.includes("socket hang up") ||
        errMsg.includes("fetcherror")
    ) {
        return; // Silent ignore
    }"""

uncaught_orig = """process.on('uncaughtException', (error) => {
    const errMsg = error ? (error.message || error.stack || error.toString()) : "";
    if (
        errMsg.includes("message is not modified") ||
        errMsg.includes("message to edit not found") ||
        errMsg.includes("query is too old")
    ) {
        return; // Silent ignore
    }"""

uncaught_new = """process.on('uncaughtException', (error) => {
    const errMsg = error ? (String(error.message) + String(error.stack) + String(error)).toLowerCase() : "";
    if (
        errMsg.includes("message is not modified") ||
        errMsg.includes("message to edit not found") ||
        errMsg.includes("query is too old") ||
        errMsg.includes("econnreset") ||
        errMsg.includes("etimedout") ||
        errMsg.includes("socket hang up") ||
        errMsg.includes("fetcherror")
    ) {
        return; // Silent ignore
    }"""

content = content.replace(bot_catch_orig, bot_catch_new)
content = content.replace(unhandled_orig, unhandled_new)
content = content.replace(uncaught_orig, uncaught_new)

with open(filepath, "w") as f:
    f.write(content)

print("Error suppression updated.")
