update polish again

This commit is contained in:
esef committed 2026-07-05 20:53:22 +03:00
1 parent 10f372173f
commit f88fd7989a
1 file changed
+40 -81
+40 -81
View File
@@ -1,6 +1,5 @@
import asyncio
import os
from datetime import datetime, timezone
import discord
from discord.ext import commands
@@ -14,11 +13,9 @@ SERVICE_NAME = "yuuka"
GIT_TIMEOUT = 30
SYSTEMD_TIMEOUT = 15
# ── colors ───────────────────────────────────────────────────────────────────
C_OK = 0x50FA7B
C_ERR = 0xFF5555
C_WARN = 0xFFB86C
C_INFO = 0x8BE9FD
# ── subprocess helpers ────────────────────────────────────────────────────────
@@ -52,10 +49,7 @@ async def run_git_pull() -> tuple[int, str]:
async def run_git_log(n: int = 5) -> tuple[int, str]:
return await _run(
"git", "log", f"--oneline", f"-{n}",
cwd=BOT_DIR, timeout=GIT_TIMEOUT,
)
return await _run("git", "log", "--oneline", f"-{n}", cwd=BOT_DIR, timeout=GIT_TIMEOUT)
async def run_sudo_systemctl(action: str) -> tuple[int, str]:
@@ -68,8 +62,7 @@ async def run_sudo_systemctl(action: str) -> tuple[int, str]:
async def run_journalctl(n: int = 50) -> tuple[int, str]:
return await _run(
"sudo", "-S", "journalctl", "-u", SERVICE_NAME,
"-n", str(n), "--no-pager",
"sudo", "-S", "journalctl", "-u", SERVICE_NAME, "-n", str(n), "--no-pager",
input_data=f"{SUDO_PASSWORD}\n".encode() if SUDO_PASSWORD else None,
timeout=SYSTEMD_TIMEOUT,
)
@@ -77,19 +70,10 @@ async def run_journalctl(n: int = 50) -> tuple[int, str]:
def truncate(text: str, limit: int = 1900) -> str:
if len(text) > limit:
return text[:limit] + "\n… (truncated)"
return text[:limit] + "\n... (truncated)"
return text
def _embed(title: str, description: str, color: int) -> discord.Embed:
return discord.Embed(
title=title,
description=description,
color=color,
timestamp=datetime.now(timezone.utc),
)
# ── cog ──────────────────────────────────────────────────────────────────────
class GitPull(commands.Cog):
def __init__(self, bot: commands.Bot):
@@ -99,11 +83,7 @@ class GitPull(commands.Cog):
return ctx.author.id == OWNER_ID
async def _do_restart(self, ctx: commands.Context):
await ctx.reply(embed=_embed(
"⟳ Restarting",
f"Restarting `{SERVICE_NAME}` — see you on the other side.",
C_WARN,
))
await ctx.reply(embed=discord.Embed(description="Restarting service...", color=C_WARN))
await asyncio.sleep(0.5)
proc = await asyncio.create_subprocess_exec(
"sudo", "-S", "systemctl", "restart", SERVICE_NAME,
@@ -119,11 +99,8 @@ class GitPull(commands.Cog):
except Exception:
pass
# ── !gitpull ──────────────────────────────────────────────────────────────
@commands.command(name="gitpull")
async def gitpull(self, ctx: commands.Context):
"""Pull latest changes from git without restarting."""
if not self._is_owner(ctx):
return
@@ -131,30 +108,27 @@ class GitPull(commands.Cog):
code, output = await run_git_pull()
if code != 0:
return await ctx.reply(embed=_embed(
"✗ Pull Failed",
f"```\n{truncate(output)}\n```",
C_ERR,
return await ctx.reply(embed=discord.Embed(
title="Git Pull",
description=f"```\n{truncate(output)}\n```",
color=C_ERR,
))
if "Already up to date" in output:
return await ctx.reply(embed=_embed(
"✓ Already Up to Date",
"Nothing new to pull.",
C_WARN,
return await ctx.reply(embed=discord.Embed(
title="Git Pull",
description="Already up to date.",
color=C_WARN,
))
await ctx.reply(embed=_embed(
"✓ Pulled",
f"```\n{truncate(output)}\n```",
C_OK,
await ctx.reply(embed=discord.Embed(
title="Git Pull",
description=f"```\n{truncate(output)}\n```",
color=C_OK,
))
# ── !gitlog ───────────────────────────────────────────────────────────────
@commands.command(name="gitlog")
async def gitlog(self, ctx: commands.Context, n: int = 5):
"""Show the last N commits. Default: 5."""
if not self._is_owner(ctx):
return
n = max(1, min(n, 20))
@@ -162,18 +136,14 @@ class GitPull(commands.Cog):
async with ctx.typing():
code, output = await run_git_log(n)
color = C_OK if code == 0 else C_ERR
await ctx.reply(embed=_embed(
f"📋 Last {n} Commits",
f"```\n{truncate(output)}\n```",
color,
await ctx.reply(embed=discord.Embed(
title=f"Last {n} commits",
description=f"```\n{truncate(output)}\n```",
color=C_OK if code == 0 else C_ERR,
))
# ── !logs ─────────────────────────────────────────────────────────────────
@commands.command(name="logs")
async def logs(self, ctx: commands.Context, n: int = 40):
"""Show the last N lines of the service journal. Default: 40."""
if not self._is_owner(ctx):
return
n = max(1, min(n, 100))
@@ -181,45 +151,34 @@ class GitPull(commands.Cog):
async with ctx.typing():
code, output = await run_journalctl(n)
color = C_OK if code == 0 else C_ERR
await ctx.reply(embed=_embed(
f"📄 Logs — {SERVICE_NAME} (last {n} lines)",
f"```\n{truncate(output)}\n```",
color,
await ctx.reply(embed=discord.Embed(
title=f"Logs ({n} lines)",
description=f"```\n{truncate(output)}\n```",
color=C_OK if code == 0 else C_ERR,
))
# ── !status ───────────────────────────────────────────────────────────────
@commands.command(name="status")
async def status(self, ctx: commands.Context):
"""Show systemd service status."""
if not self._is_owner(ctx):
return
async with ctx.typing():
code, output = await run_sudo_systemctl("status")
color = C_OK if code == 0 else C_ERR
await ctx.reply(embed=_embed(
f"{'✓' if code == 0 else '✗'} Status — {SERVICE_NAME}",
f"```\n{truncate(output)}\n```",
color,
await ctx.reply(embed=discord.Embed(
title=f"Service Status: {SERVICE_NAME}",
description=f"```\n{truncate(output)}\n```",
color=C_OK if code == 0 else C_ERR,
))
# ── !restart ──────────────────────────────────────────────────────────────
@commands.command(name="restart")
async def restart(self, ctx: commands.Context):
"""Restart the bot service."""
if not self._is_owner(ctx):
return
await self._do_restart(ctx)
# ── !update ───────────────────────────────────────────────────────────────
@commands.command(name="update")
async def update(self, ctx: commands.Context):
"""Pull latest changes and restart if anything changed."""
if not self._is_owner(ctx):
return
@@ -227,23 +186,23 @@ class GitPull(commands.Cog):
code, output = await run_git_pull()
if code != 0:
return await ctx.reply(embed=_embed(
"✗ Update Failed",
f"Pull failed — restart skipped.\n```\n{truncate(output)}\n```",
C_ERR,
return await ctx.reply(embed=discord.Embed(
title="Update Failed",
description=f"git pull failed, restart skipped.\n```\n{truncate(output)}\n```",
color=C_ERR,
))
if "Already up to date" in output:
return await ctx.reply(embed=_embed(
"✓ Already Up to Date",
"Nothing new to pull. Restart skipped.",
C_WARN,
return await ctx.reply(embed=discord.Embed(
title="Update",
description="No changes to pull. Restart skipped.",
color=C_WARN,
))
await ctx.reply(embed=_embed(
"✓ Updated",
f"```\n{truncate(output)}\n```",
C_OK,
await ctx.reply(embed=discord.Embed(
title="Update",
description=f"Pulled changes:\n```\n{truncate(output)}\n```",
color=C_OK,
))
await self._do_restart(ctx)