main
 1#!/usr/bin/env python
 2# -*- coding: utf-8 -*-
 3import contextlib
 4import importlib.metadata
 5import os
 6import platform
 7
 8from ffmpeg.asyncio import FFmpeg
 9from pyrogram.client import Client
10from pyrogram.types import Message
11
12from config import PREFIX
13from messages.sender import send2tg
14from messages.utils import blockquote, startswith_prefix
15
16
17async def get_bot_version(client: Client, message: Message, **kwargs):
18    """Get detail version of this bot."""
19    if not startswith_prefix(message.content, prefix=PREFIX.VERSION):
20        return
21    commit_sha = os.getenv("COMMIT_SHA", "")
22    commit_sha = f"{commit_sha[:7]}" if commit_sha else ""
23    commit_date = os.getenv("COMMIT_DATE", "")
24    python_version = platform.python_version()
25    ffmpeg_version = await get_ffmpeg_version()
26    python_packages = {x.metadata["Name"]: x.version for x in importlib.metadata.distributions()}
27
28    header = f"**Bot**: {commit_sha} ({commit_date})\n"
29    if ffmpeg_version:
30        header += f"**FFmpeg**: {ffmpeg_version}\n"
31    header += f"**Python**: {python_version}\n**Packages**:\n"
32    pkgs = ""
33    for name, version in sorted(python_packages.items(), key=lambda x: x[0].lower()):
34        pkgs += f"{name}=={version}\n"
35    await send2tg(client, message, texts=f"{header}{blockquote(pkgs)}", **kwargs)
36
37
38async def get_ffmpeg_version() -> str:
39    ffmpeg = FFmpeg().option("version")
40    with contextlib.suppress(Exception):
41        res = await ffmpeg.execute()
42        if isinstance(res, bytes):
43            lines = res.decode("utf-8").splitlines()
44            return lines[0].split(" ")[2]
45    return ""