Added /status command

This commit is contained in:
2025-04-28 13:06:55 +02:00
parent c96cb167b5
commit c4ebd1b891
6 changed files with 85 additions and 2 deletions

View File

@@ -20,6 +20,9 @@ logger: Logger = get_logger(__name__)
class PycordBot(LibPycordBot):
__version__ = "0.1.0"
started: datetime
cache: CacheMemcached | CacheRedis | None = None
def __init__(self, *args, **kwargs) -> None:
@@ -48,6 +51,9 @@ class PycordBot(LibPycordBot):
@override
async def start(self, *args: Any, **kwargs: Any) -> None:
await self._schedule_tasks()
self.started = datetime.now(tz=ZoneInfo("UTC"))
await super().start(*args, **kwargs)
@override

42
cogs/cog_status.py Normal file
View File

@@ -0,0 +1,42 @@
from discord import ApplicationContext, Cog, slash_command
from libbot.i18n import _, in_every_locale
from classes.pycord_bot import PycordBot
from modules.utils import get_current_commit, get_unix_timestamp
class CogStatus(Cog):
"""Cog with the status command."""
def __init__(self, bot: PycordBot):
self.bot: PycordBot = bot
@slash_command(
name="status",
description=_("description", "commands", "status"),
description_localizations=in_every_locale("description", "commands", "status"),
)
async def command_status(self, ctx: ApplicationContext) -> None:
current_commit: str | None = await get_current_commit()
if current_commit is None:
await ctx.respond(
self.bot._("status", "messages", locale=ctx.locale).format(
version=self.bot.__version__,
start_time=get_unix_timestamp(self.bot.started),
)
)
return
await ctx.respond(
self.bot._("status_git", "messages", locale=ctx.locale).format(
version=self.bot.__version__,
commit=current_commit if len(current_commit) < 10 else current_commit[:10],
start_time=get_unix_timestamp(self.bot.started),
)
)
def setup(bot: PycordBot) -> None:
bot.add_cog(CogStatus(bot))

View File

@@ -6,7 +6,9 @@
"timezone_invalid": "Timezone **{timezone}** was not found. Please, select one of the timezones provided by the autocompletion.",
"config_set": "Configuration has been updated. You can review it anytime using `/config show`.",
"config_reset": "Configuration has been reset. You can update it using `/config set`, otherwise no events can be held.",
"config_show": "**Guild config**\n\nChannel: <#{channel_id}>\nCategory: <#{category_id}>\nTimezone: {timezone}"
"config_show": "**Guild config**\n\nChannel: <#{channel_id}>\nCategory: <#{category_id}>\nTimezone: {timezone}",
"status": "**QuizBot** v{version}\n\nUptime: since <t:{start_time}>",
"status_git": "**QuizBot** v{version} (`{commit}`)\n\nUptime: since <t:{start_time}>"
},
"commands": {
"config": {
@@ -172,6 +174,9 @@
}
}
},
"status": {
"description": "Get status of the bot"
},
"unregister": {
"description": "Leave the selected event",
"options": {

View File

@@ -1,3 +1,5 @@
"""Main module with entry point that must be executed for the bot to start"""
import contextlib
import logging.config
from argparse import ArgumentParser
@@ -39,7 +41,7 @@ with contextlib.suppress(ImportError):
uvloop.install()
def main():
def main() -> None:
# Perform migration if command line argument was provided
if args.migrate:
migrate_database()

View File

@@ -9,5 +9,6 @@ from .autocomplete_utils import (
from .cache_utils import restore_from_cache
from .datetime_utils import get_unix_timestamp
from .event_utils import validate_event_validity
from .git_utils import get_current_commit
from .logging_utils import get_logger, get_logging_config
from .validation_utils import is_event_status_valid, is_operation_confirmed

View File

@@ -0,0 +1,27 @@
from pathlib import Path
import aiofiles
# TODO Add documentation
async def get_current_commit() -> str | None:
head_path: Path = Path(".git/HEAD")
if not head_path.exists():
return None
async with aiofiles.open(head_path, "r", encoding="utf-8") as head_file:
head_content: str = (await head_file.read()).strip()
if not head_content.startswith("ref:"):
return head_content
head_ref_path: Path = Path(".git/").joinpath(" ".join(head_content.split(" ")[1:]))
if not head_ref_path.exists():
return None
async with aiofiles.open(head_ref_path, "r", encoding="utf-8") as head_ref_file:
head_ref_content: str = (await head_ref_file.read()).strip()
return head_ref_content