discordBot/commands/status.py

50 lines
1.7 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 logging
import time
import discord
from discord.ext import commands
logger = logging.getLogger(__name__)
class Status(commands.Cog):
"""Команда !status — статус бота, пинг, uptime"""
@commands.command(name="status")
async def status(self, ctx: commands.Context) -> None:
"""Статус бота: пинг к 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)
logger.info("%s: !status выполнена", ctx.author)
@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)