Commit 0a457af

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-10-27 05:57:16
feat(version): add `/version` to show bot version
1 parent 8643f36
Changed files (4)
src/others/version.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+import contextlib
+import importlib.metadata
+import os
+import platform
+
+from ffmpeg.asyncio import FFmpeg
+from pyrogram.client import Client
+from pyrogram.types import Message
+
+from config import PREFIX
+from messages.sender import send2tg
+from messages.utils import blockquote, startswith_prefix
+
+
+async def get_bot_version(client: Client, message: Message, **kwargs):
+    """Get detail version of this bot."""
+    if not startswith_prefix(message.content, prefix=PREFIX.VERSION):
+        return
+    commit_sha = os.getenv("COMMIT_SHA", "")
+    commit_sha = f"{commit_sha[:7]}" if commit_sha else ""
+    commit_date = os.getenv("COMMIT_DATE", "")
+    python_version = platform.python_version()
+    ffmpeg_version = await get_ffmpeg_version()
+    python_packages = {x.metadata["Name"]: x.version for x in importlib.metadata.distributions()}
+
+    header = f"**Bot**: {commit_sha} ({commit_date})\n"
+    if ffmpeg_version:
+        header += f"**FFmpeg**: {ffmpeg_version}\n"
+    header += f"**Python**: {python_version}\n**Packages**:\n"
+    pkgs = ""
+    for name, version in sorted(python_packages.items(), key=lambda x: x[0].lower()):
+        pkgs += f"{name}=={version}\n"
+    await send2tg(client, message, texts=f"{header}{blockquote(pkgs)}", **kwargs)
+
+
+async def get_ffmpeg_version() -> str:
+    ffmpeg = FFmpeg().option("version")
+    with contextlib.suppress(Exception):
+        res = await ffmpeg.execute()
+        if isinstance(res, bytes):
+            lines = res.decode("utf-8").splitlines()
+            return lines[0].split(" ")[2]
+    return ""
src/config.py
@@ -75,6 +75,7 @@ class ENABLE:  # see fine-grained permission in `src/permission.py`
     TMDB = os.getenv("ENABLE_TMDB", "1").lower() in ["1", "y", "yes", "t", "true", "on"]
     FFMPEG = os.getenv("ENABLE_FFMPEG", "1").lower() in ["1", "y", "yes", "t", "true", "on"]
     WATERMARK = os.getenv("ENABLE_WATERMARK", "1").lower() in ["1", "y", "yes", "t", "true", "on"]
+    VERSION = os.getenv("ENABLE_VERSION", "1").lower() in ["1", "y", "yes", "t", "true", "on"]
 
 
 class PREFIX:
@@ -106,6 +107,7 @@ class PREFIX:
     FFMPEG_CUT = os.getenv("PREFIX_FFMPEG_CUT", "/cut").lower()
     FFMPEG_H264 = os.getenv("PREFIX_FFMPEG_H264", "/h264").lower()
     WATERMARK = os.getenv("PREFIX_WATERMARK", "/wm, /watermark").lower()
+    VERSION = os.getenv("PREFIX_VERSION", "/version").lower()
 
 
 class API:
src/handler.py
@@ -28,6 +28,7 @@ from others.raw_img_file import convert_raw_img_file
 from others.search_google import search_google
 from others.search_ytb import search_youtube
 from others.tmdb import search_tmdb
+from others.version import get_bot_version
 from others.watermark import add_watermark
 from permission import check_service
 from preview.bilibili import preview_bilibili
@@ -76,6 +77,7 @@ async def handle_utilities(
     tmdb: bool = True,
     ffmpeg: bool = True,
     watermark: bool = True,
+    version: bool = True,
     raw_img: bool = True,
     show_progress: bool = True,
     detail_progress: bool = False,
@@ -158,6 +160,8 @@ async def handle_utilities(
         await ffmpeg_h264(client, message, **kwargs)
     if watermark:
         await add_watermark(client, message, **kwargs)
+    if version:
+        await get_bot_version(client, message, **kwargs)
 
 
 async def handle_social_media(
src/permission.py
@@ -219,6 +219,8 @@ def check_service(cid: int | str, ctype: str) -> dict:
         permission["ffmpeg"] = False
     if not ENABLE.WATERMARK:
         permission["watermark"] = False
+    if not ENABLE.VERSION:
+        permission["version"] = False
 
     """
     Set for specific chat