feat: команда /version из хэша коммита, значки в мобильной навигации, версия в сайдбаре
| | |
| | | import logging |
| | | import os |
| | | import pathlib |
| | | from typing import Optional, Set |
| | | |
| | | from aiogram import Bot, Dispatcher |
| | |
| | | |
| | | load_dotenv() |
| | | |
| | | |
| | | def resolve_version() -> str: |
| | | # Версия = короткий хэш коммита. Приоритет: env BOT_VERSION, затем .git, |
| | | # затем fallback. Читаем .git напрямую, т.к. git в образе не установлен. |
| | | env = os.getenv("BOT_VERSION") |
| | | if env: |
| | | return env |
| | | git = pathlib.Path(__file__).resolve().parent / ".git" |
| | | try: |
| | | head = (git / "HEAD").read_text().strip() |
| | | commit = head |
| | | if head.startswith("ref:"): |
| | | ref = head.split(" ", 1)[1].strip() |
| | | loose = git / ref |
| | | if loose.exists(): |
| | | commit = loose.read_text().strip() |
| | | else: |
| | | commit = "" |
| | | packed = git / "packed-refs" |
| | | if packed.exists(): |
| | | for line in packed.read_text().splitlines(): |
| | | parts = line.split() |
| | | if len(parts) == 2 and parts[1] == ref: |
| | | commit = parts[0] |
| | | break |
| | | return commit[:7] if commit else "unknown" |
| | | except Exception: |
| | | return "unknown" |
| | | |
| | | |
| | | VERSION: str = resolve_version() |
| | | |
| | | TOKEN: Optional[str] = os.getenv("BOT_TOKEN") |
| | | # Владелец бота: полный доступ везде, ограничить его нельзя. |
| | | OWNER_ID: int = int(os.getenv("OWNER_ID") or 0) |
| | |
| | | from aiogram.filters import Command, CommandObject |
| | | |
| | | import db |
| | | import cfg |
| | | import perms as pm |
| | | from cfg import bot, dp |
| | | from cfg import VERSION, bot, dp |
| | | from perms import P_CFG, P_PERMS, PERM_LABELS, check_perm, member_status |
| | | from util import mention, msg_target, now_ts, resolve_usr |
| | | |
| | | HELP_TEXT = ( |
| | | "Команды бота:\n" |
| | | "/help - этот список и текущие настройки\n" |
| | | "/version - версия бота\n" |
| | | "/config - настройки капчи\n" |
| | | "/config <кнопки> <секунды> <попытки> - изменить капчу\n" |
| | | "/warncfg <лимит> <mute|ban> <время> - настроить систему варнов\n" |
| | |
| | | await message.answer(HELP_TEXT + "\n\n" + _cfg_text(cfg)) |
| | | |
| | | |
| | | @dp.message(Command("version")) |
| | | async def cmd_version(message: types.Message) -> None: |
| | | if message.from_user is None: |
| | | return |
| | | if not await _is_staff(message.chat.id, message.from_user.id): |
| | | return |
| | | username = f"@{cfg.BOT_USERNAME}" if cfg.BOT_USERNAME else "неизвестен" |
| | | await message.answer( |
| | | f"Amadeus\nВерсия: {VERSION}\nБот: {username}\nID: {cfg.BOT_ID}" |
| | | ) |
| | | |
| | | |
| | | @dp.message(Command("config")) |
| | | async def cmd_cfg(message: types.Message, command: CommandObject) -> None: |
| | | if message.from_user is None: |
| | |
| | | import jinja2 |
| | | from aiohttp import web |
| | | |
| | | import cfg |
| | | |
| | | BASE = pathlib.Path(__file__).resolve().parent |
| | | |
| | | ENV = jinja2.Environment( |
| | |
| | | |
| | | |
| | | def render(request, name: str, active: str = "", **ctx) -> web.Response: |
| | | ctx.update(request=request, user=request.get("user"), active=active) |
| | | ctx.update(request=request, user=request.get("user"), active=active, version=cfg.VERSION) |
| | | html = ENV.get_template(name).render(**ctx) |
| | | return web.Response(text=html, content_type="text/html") |
| | |
| | | </div> |
| | | </div> |
| | | <a href="/logout" class="mt-3 block rounded-lg border border-gray-200 px-3 py-1.5 text-center text-sm hover:bg-gray-100 dark:border-gray-700 dark:hover:bg-gray-800">Выйти</a> |
| | | <div class="mt-2 text-center text-[11px] text-gray-400">Amadeus v{{ version }}</div> |
| | | </div> |
| | | </aside> |
| | | |
| | |
| | | </div> |
| | | </div> |
| | | |
| | | <!-- Нижняя навигация: только на мобильных --> |
| | | <nav class="fixed inset-x-0 bottom-0 z-30 grid grid-cols-5 border-t border-gray-200 bg-white/95 backdrop-blur dark:border-gray-800 dark:bg-gray-900/95 md:hidden"> |
| | | <!-- Нижняя навигация: только на мобильных, со значками --> |
| | | <nav class="fixed inset-x-0 bottom-0 z-30 grid grid-cols-5 border-t border-gray-200 bg-white/95 pb-[env(safe-area-inset-bottom)] backdrop-blur dark:border-gray-800 dark:bg-gray-900/95 md:hidden"> |
| | | {% set nav = [ |
| | | ("dash", "/", "Главная"), |
| | | ("chats", "/chats", "Чаты"), |
| | | ("users", "/users", "Люди"), |
| | | ("captcha", "/captcha", "Капчи"), |
| | | ("log", "/log", "Журнал") |
| | | ("dash", "/", "Главная", "M2.25 12l8.954-8.955c.44-.439 1.152-.439 1.591 0L21.75 12M4.5 9.75v10.125A1.125 1.125 0 005.625 21H9.75v-4.875c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125V21h4.125a1.125 1.125 0 001.125-1.125V9.75"), |
| | | ("chats", "/chats", "Чаты", "M20.25 8.511c.884.284 1.5 1.128 1.5 2.097v4.286c0 1.136-.847 2.1-1.98 2.193-.34.027-.68.052-1.02.072v3.091l-3-3c-1.354 0-2.694-.055-4.02-.163a2.115 2.115 0 01-.825-.242m9.345-8.334a2.126 2.126 0 00-.476-.095 48.64 48.64 0 00-8.048 0c-1.131.094-1.976 1.057-1.976 2.192v4.286c0 .837.46 1.58 1.155 1.951m9.345-8.334V6.637c0-1.621-1.152-3.026-2.76-3.235A48.455 48.455 0 0011.25 3c-2.115 0-4.198.137-6.24.402-1.608.209-2.76 1.614-2.76 3.235v6.226c0 1.621 1.152 3.026 2.76 3.235.577.075 1.157.14 1.74.194V21l4.155-4.155"), |
| | | ("users", "/users", "Люди", "M15 19.128a9.38 9.38 0 002.625.372 9.337 9.337 0 004.121-.952 4.125 4.125 0 00-7.533-2.493M15 19.128v-.003c0-1.113-.285-2.16-.786-3.07M15 19.128v.106A12.318 12.318 0 018.624 21c-2.331 0-4.512-.645-6.374-1.766l-.001-.109a6.375 6.375 0 0111.964-3.07M12 6.375a3.375 3.375 0 11-6.75 0 3.375 3.375 0 016.75 0zm8.25 2.25a2.625 2.625 0 11-5.25 0 2.625 2.625 0 015.25 0z"), |
| | | ("captcha", "/captcha", "Капчи", "M9 12.75L11.25 15 15 9.75M21 12c0 1.268-.63 2.39-1.593 3.068a3.745 3.745 0 01-1.043 3.296 3.745 3.745 0 01-3.296 1.043A3.745 3.745 0 0112 21c-1.268 0-2.39-.63-3.068-1.593a3.746 3.746 0 01-3.296-1.043 3.745 3.745 0 01-1.043-3.296A3.745 3.745 0 013 12c0-1.268.63-2.39 1.593-3.068a3.745 3.745 0 011.043-3.296 3.746 3.746 0 013.296-1.043A3.746 3.746 0 0112 3c1.268 0 2.39.63 3.068 1.593a3.746 3.746 0 013.296 1.043 3.746 3.746 0 011.043 3.296A3.745 3.745 0 0121 12z"), |
| | | ("log", "/log", "Журнал", "M8.25 6.75h7.5M8.25 12h7.5m-7.5 5.25h4.5M3.75 6.75h.008v.008H3.75V6.75zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zM3.75 12h.008v.008H3.75V12zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zm-.375 5.25h.008v.008H3.75v-.008zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0z") |
| | | ] %} |
| | | {% for key, href, label in nav %} |
| | | <a href="{{ href }}" class="flex flex-col items-center justify-center py-2.5 text-[11px] leading-tight |
| | | {% if active == key %}text-brand-600 dark:text-brand-400 font-medium{% else %}text-gray-500 dark:text-gray-400{% endif %}"> |
| | | {{ label }} |
| | | {% for key, href, label, icon in nav %} |
| | | <a href="{{ href }}" class="flex flex-col items-center justify-center gap-0.5 py-2 text-[10px] |
| | | {% if active == key %}text-brand-600 dark:text-brand-400{% else %}text-gray-500 dark:text-gray-400{% endif %}"> |
| | | <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"> |
| | | <path stroke-linecap="round" stroke-linejoin="round" d="{{ icon }}"/> |
| | | </svg> |
| | | <span class="{% if active == key %}font-medium{% endif %}">{{ label }}</span> |
| | | </a> |
| | | {% endfor %} |
| | | </nav> |