discordBot/commands/status.py
deadzilla f2844f0453 fix: uptime читается с bot._start_time вместо Cog
Проблема: _start_time хранился в Status Cog, но консольная
команда искала его на объекте bot — всегда получала fallback
time.time() и показывала 0с.

Решение: _start_time устанавливается в BotRunner.__init__ на
объекте bot. Cog и console_command читают getattr(bot, '_start_time').

Изменено: bot.py, commands/status.py, console_commands/status.py,
tests/test_commands_status.py
2026-06-12 16:32:55 +05:00

46 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import time
import discord
from discord.ext import commands
class Status(commands.Cog):
"""Команда !status — статус бота, пинг, uptime"""
@commands.command(name="status")
async def status(self, ctx):
"""Статус бота: пинг к Discord gateway и время работы"""
latency_ms = round(ctx.bot.latency * 1000, 1)
start_time = getattr(ctx.bot, "_start_time", time.time())
uptime_seconds = time.time() - start_time
uptime_str = self._format_uptime(uptime_seconds)
embed = discord.Embed(
title="Статус бота",
colour=discord.Color.green(),
)
embed.add_field(name="Пинг", value=f"{latency_ms} мс", inline=True)
embed.add_field(name="Uptime", value=uptime_str, inline=True)
embed.add_field(name="Статус", value="Online", inline=True)
await ctx.send(embed=embed)
@staticmethod
def _format_uptime(total_seconds: float) -> str:
"""Форматировать секунды в человекочитаемый вид."""
days = int(total_seconds // 86400)
hours = int((total_seconds % 86400) // 3600)
minutes = int((total_seconds % 3600) // 60)
seconds = int(total_seconds % 60)
parts = []
if days:
parts.append(f"{days}д")
if hours:
parts.append(f"{hours}ч")
if minutes:
parts.append(f"{minutes}м")
parts.append(f"{seconds}с")
return " ".join(parts)