Prechádzať zdrojové kódy

Use log.info instead of print on some places

TheHamkerCat 4 rokov pred
rodič
commit
c675b90f7b
4 zmenil súbory, kde vykonal 59 pridanie a 60 odobranie
  1. 27 27
      wbb/__init__.py
  2. 22 23
      wbb/__main__.py
  3. 8 8
      wbb/modules/purge_me.py
  4. 2 2
      wbb/modules/rss.py

+ 27 - 27
wbb/__init__.py

@@ -51,15 +51,36 @@ MOD_NOLOAD = []
 SUDOERS = filters.user()
 bot_start_time = time.time()
 
+
+class Log:
+    def __init__(self, save_to_file=False, file_name="wbb.log"):
+        self.save_to_file = save_to_file
+        self.file_name = file_name
+
+    def info(self, msg):
+        print(f"[+]: {msg}")
+        if self.save_to_file:
+            with open(self.file_name, "a") as f:
+                f.write(f"[INFO]({time.ctime(time.time())}): {msg}\n")
+
+    def error(self, msg):
+        print(f"[-]: {msg}")
+        if self.save_to_file:
+            with open(self.file_name, "a") as f:
+                f.write(f"[ERROR]({time.ctime(time.time())}): {msg}\n")
+
+
+log = Log(True, "bot.log")
+
 # MongoDB client
-print("[INFO]: INITIALIZING DATABASE")
+log.info("Initializing MongoDB client")
 mongo_client = MongoClient(MONGO_URL)
 db = mongo_client.wbb
 
 
 async def load_sudoers():
     global SUDOERS
-    print("[INFO]: LOADING SUDOERS")
+    log.info("Loading sudoers")
     sudoersdb = db.sudoers
     sudoers = await sudoersdb.find_one({"sudo": "sudo"})
     sudoers = [] if not sudoers else sudoers["sudoers"]
@@ -75,7 +96,6 @@ async def load_sudoers():
     if sudoers:
         for user_id in sudoers:
             SUDOERS.add(user_id)
-    print("[INFO]: LOADED SUDOERS")
 
 
 loop = asyncio.get_event_loop()
@@ -97,12 +117,12 @@ arq = ARQ(ARQ_API_URL, ARQ_API_KEY, aiohttpsession)
 
 app = Client("wbb", bot_token=BOT_TOKEN, api_id=API_ID, api_hash=API_HASH)
 
-print("[INFO]: STARTING BOT CLIENT")
+log.info("Starting bot client")
 app.start()
-print("[INFO]: STARTING USERBOT CLIENT")
+log.info("Starting userbot client")
 app2.start()
 
-print("[INFO]: GATHERING PROFILE INFO")
+log.info("Gathering profile info")
 x = app.get_me()
 y = app2.get_me()
 
@@ -121,6 +141,7 @@ USERBOT_DC_ID = y.dc_id
 if USERBOT_ID not in SUDOERS:
     SUDOERS.add(USERBOT_ID)
 
+log.info("Initializing Telegraph client")
 telegraph = Telegraph()
 telegraph.create_account(short_name=BOT_USERNAME)
 
@@ -133,24 +154,3 @@ async def eor(msg: Message, **kwargs):
     )
     spec = getfullargspec(func.__wrapped__).args
     return await func(**{k: v for k, v in kwargs.items() if k in spec})
-
-
-class Log:
-    def __init__(self, save_to_file=False, file_name="wbb.log"):
-        self.save_to_file = save_to_file
-        self.file_name = file_name
-
-    def info(self, msg):
-        print(f"[+]: {msg}")
-        if self.save_to_file:
-            with open(self.file_name, "a") as f:
-                f.write(f"[INFO]({time.ctime(time.time())}): {msg}\n")
-
-    def error(self, msg):
-        print(f"[-]: {msg}")
-        if self.save_to_file:
-            with open(self.file_name, "a") as f:
-                f.write(f"[ERROR]({time.ctime(time.time())}): {msg}\n")
-
-
-log = Log(True, "bot.log")

+ 22 - 23
wbb/__main__.py

@@ -30,7 +30,7 @@ from pyrogram import filters, idle
 from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
 
 from wbb import (BOT_NAME, BOT_USERNAME, LOG_GROUP_ID, USERBOT_NAME,
-                 aiohttpsession, app)
+                 aiohttpsession, app, log)
 from wbb.modules import ALL_MODULES
 from wbb.modules.sudoers import bot_sys_stats
 from wbb.utils import paginate_modules
@@ -48,13 +48,13 @@ async def start_bot():
     for module in ALL_MODULES:
         imported_module = importlib.import_module("wbb.modules." + module)
         if (
-            hasattr(imported_module, "__MODULE__")
-            and imported_module.__MODULE__
+                hasattr(imported_module, "__MODULE__")
+                and imported_module.__MODULE__
         ):
             imported_module.__MODULE__ = imported_module.__MODULE__
             if (
-                hasattr(imported_module, "__HELP__")
-                and imported_module.__HELP__
+                    hasattr(imported_module, "__HELP__")
+                    and imported_module.__HELP__
             ):
                 HELPABLE[
                     imported_module.__MODULE__.replace(" ", "_").lower()
@@ -73,13 +73,13 @@ async def start_bot():
     print("+===============+===============+===============+===============+")
     print(bot_modules)
     print("+===============+===============+===============+===============+")
-    print(f"[INFO]: BOT STARTED AS {BOT_NAME}!")
-    print(f"[INFO]: USERBOT STARTED AS {USERBOT_NAME}!")
+    log.info(f"BOT STARTED AS {BOT_NAME}!")
+    log.info(f"USERBOT STARTED AS {USERBOT_NAME}!")
 
     restart_data = await clean_restart_stage()
 
     try:
-        print("[INFO]: SENDING ONLINE STATUS")
+        log.info("Sending online status")
         if restart_data:
             await app.edit_message_text(
                 restart_data["chat_id"],
@@ -95,12 +95,12 @@ async def start_bot():
     await idle()
 
     await aiohttpsession.close()
-    print("[INFO]: CLOSING AIOHTTP SESSION AND STOPPING BOT")
+    log.info("Stopping clients")
     await app.stop()
-    print("[INFO]: Bye!")
+    log.info("Cancelling asyncio tasks")
     for task in asyncio.all_tasks():
         task.cancel()
-    print("[INFO]: Turned off!")
+    log.info("Dead!")
 
 
 home_keyboard_pm = InlineKeyboardMarkup(
@@ -133,12 +133,11 @@ home_keyboard_pm = InlineKeyboardMarkup(
 )
 
 home_text_pm = (
-    f"Hey there! My name is {BOT_NAME}. I can manage your "
-    + "group with lots of useful features, feel free to "
-    + "add me to your group."
+        f"Hey there! My name is {BOT_NAME}. I can manage your "
+        + "group with lots of useful features, feel free to "
+        + "add me to your group."
 )
 
-
 keyboard = InlineKeyboardMarkup(
     [
         [
@@ -177,8 +176,8 @@ async def start(_, message):
         elif "_" in name:
             module = name.split("_", 1)[1]
             text = (
-                f"Here is the help for **{HELPABLE[module].__MODULE__}**:\n"
-                + HELPABLE[module].__HELP__
+                    f"Here is the help for **{HELPABLE[module].__MODULE__}**:\n"
+                    + HELPABLE[module].__HELP__
             )
             await message.reply(text, disable_web_page_preview=True)
         elif name == "help":
@@ -228,8 +227,8 @@ async def help_command(_, message):
             name = (message.text.split(None, 1)[1]).replace(" ", "_").lower()
             if str(name) in HELPABLE:
                 text = (
-                    f"Here is the help for **{HELPABLE[name].__MODULE__}**:\n"
-                    + HELPABLE[name].__HELP__
+                        f"Here is the help for **{HELPABLE[name].__MODULE__}**:\n"
+                        + HELPABLE[name].__HELP__
                 )
                 await message.reply(text, disable_web_page_preview=True)
             else:
@@ -306,10 +305,10 @@ General command are:
     if mod_match:
         module = (mod_match.group(1)).replace(" ", "_")
         text = (
-            "{} **{}**:\n".format(
-                "Here is the help for", HELPABLE[module].__MODULE__
-            )
-            + HELPABLE[module].__HELP__
+                "{} **{}**:\n".format(
+                    "Here is the help for", HELPABLE[module].__MODULE__
+                )
+                + HELPABLE[module].__HELP__
         )
 
         await query.message.edit(

+ 8 - 8
wbb/modules/purge_me.py

@@ -25,7 +25,7 @@ SOFTWARE.
 from pyrogram import filters
 from pyrogram.types import Message
 
-from wbb import USERBOT_ID, USERBOT_PREFIX, app2, eor, telegraph
+from wbb import USERBOT_ID, USERBOT_PREFIX, app2, eor, telegraph, log
 
 __MODULE__ = "Userbot"
 TEXT = """
@@ -79,14 +79,14 @@ TEXT = """
 
 <code>dice</code> → Roll a dice.<br>
 """
-print("[INFO]: Pasting userbot commands on telegraph")
+log.info("Pasting userbot commands on telegraph")
 
 __HELP__ = f"""**Commands:** {telegraph.create_page(
-        "Userbot Commands",
-        html_content=TEXT,
-    )['url']}"""
+    "Userbot Commands",
+    html_content=TEXT,
+)['url']}"""
 
-print("[INFO]: Done pasting userbot commands on telegraph")
+log.info("Done pasting userbot commands on telegraph")
 
 
 @app2.on_message(
@@ -101,7 +101,7 @@ async def get_help(_, message: Message):
 
 
 @app2.on_message(
-    filters.command("purgeme", prefixes=USERBOT_PREFIX)
+    filters.command(["purgeme", "purge_me"], prefixes=USERBOT_PREFIX)
     & filters.user(USERBOT_ID)
 )
 async def purge_me_func(_, message: Message):
@@ -136,7 +136,7 @@ async def purge_me_func(_, message: Message):
     # we have to do it in chunks of 100, i'll choose 99 just
     # to be safe.
     to_delete = [
-        message_ids[i : i + 99] for i in range(0, len(message_ids), 99)
+        message_ids[i: i + 99] for i in range(0, len(message_ids), 99)
     ]
 
     for hundred_messages_or_less in to_delete:

+ 2 - 2
wbb/modules/rss.py

@@ -18,14 +18,14 @@ __HELP__ = f"""
 /rm_feed - Remove feed from chat
 
 **Note:**
-    - This will check for updates every {RSS_DELAY//60} minutes.
+    - This will check for updates every {RSS_DELAY // 60} minutes.
     - You can only add one feed per chat.
     - Currently RSS and ATOM feeds are supported.
 """
 
 
 async def rss_worker():
-    print("[INFO]: RSS WORKER STARTED")
+    log.info("RSS Worker started")
     while True:
         feeds = await get_rss_feeds()
         if not feeds: