Dough Git

commit

feat: add /minecraft skin and serverstatus commands

commit
8e1529465b3e7b5069c026d7369d6843694ff6ce
author
Clove Twilight <[email protected]>
date
2026-10-31 08:57

diff --git a/commands/cogs/lookups/minecraft.py b/commands/cogs/lookups/minecraft.py
new file mode 100644
index 0000000..7fe95e0
--- /dev/null
+++ b/commands/cogs/lookups/minecraft.py
@@ -0,0 +1,332 @@
+import aiohttp
+import base64
+import binascii
+import discord
+from discord import app_commands
+from discord.ext import commands
+import json
+import re
+from sandrone import checks, config, mood
+from typing import Any, Literal
+import urllib.parse
+from utils import components
+
+userAgent = (
+    f"Sandrone-{config.version} "
+    "(https://github.com/doughmination/sandrone)"
+)
+
+mojangProfileUrl = "https://api.mojang.com/users/profiles/minecraft/{}"
+sessionProfileUrl = (
+    "https://sessionserver.mojang.com/session/minecraft/profile/{}"
+)
+renderBase = "https://mc-heads.net"
+statusBase = "https://api.mcsrvstat.us"
+
+maxListedPlayers = 10
+
+usernamePattern = re.compile(r"^[A-Za-z0-9_]{1,16}$")
+uuidPattern = re.compile(r"^[0-9a-fA-F]{32}$")
+addressPattern = re.compile(r"^[A-Za-z0-9.\-_\[\]:]{1,255}$")
+formattingCode = re.compile(r"§.?")
+
+type Edition = Literal["java", "bedrock"]
+
+
+class Textures:
+    def __init__(
+        self, skin: str | None, slim: bool, cape: str | None
+    ) -> None:
+        self.skin = skin
+        self.slim = slim
+        self.cape = cape
+
+
+def normaliseName(query: str) -> str | None:
+    """A username or (dashed or not) UUID, or None if it's neither."""
+    text = query.strip().removeprefix("@")
+    undashed = text.replace("-", "")
+    if uuidPattern.match(undashed):
+        return undashed.lower()
+    if usernamePattern.match(text):
+        return text
+    return None
+
+
+def normaliseAddress(address: str) -> str | None:
+    text = address.strip().lower()
+    text = text.removeprefix("https://").removeprefix("http://")
+    text = text.removesuffix("/")
+    if not text or not addressPattern.match(text):
+        return None
+    return text
+
+
+def httpsUrl(url: str) -> str:
+    return "https://" + url.removeprefix("http://").removeprefix(
+        "https://"
+    )
+
+
+def parseTextures(profile: dict[str, Any]) -> Textures:
+    """Pull the skin/cape out of a session server profile."""
+    for prop in profile.get("properties") or []:
+        if prop.get("name") != "textures":
+            continue
+        try:
+            decoded = json.loads(base64.b64decode(prop.get("value", "")))
+        except binascii.Error, ValueError:
+            break
+        textures = decoded.get("textures") or {}
+        skin = textures.get("SKIN") or {}
+        cape = textures.get("CAPE") or {}
+        model = (skin.get("metadata") or {}).get("model")
+        return Textures(
+            skin=httpsUrl(skin["url"]) if skin.get("url") else None,
+            slim=model == "slim",
+            cape=httpsUrl(cape["url"]) if cape.get("url") else None,
+        )
+    return Textures(skin=None, slim=False, cape=None)
+
+
+def dashedUuid(uuid: str) -> str:
+    return (
+        f"{uuid[:8]}-{uuid[8:12]}-{uuid[12:16]}-{uuid[16:20]}-{uuid[20:]}"
+    )
+
+
+def stripFormatting(text: str) -> str:
+    return formattingCode.sub("", text)
+
+
+def skinPanel(
+    name: str, uuid: str, textures: Textures
+) -> components.Panel:
+    model = "Slim (Alex)" if textures.slim else "Classic (Steve)"
+    lines = [
+        f"> **Model**: {model}",
+        f"> **Cape**: {'Yes' if textures.cape else 'No'}",
+    ]
+    if textures.skin is None:
+        lines.append(
+            "\nThis player has no custom skin, so they get a default one."
+        )
+
+    buttons = [
+        components.linkButton(
+            "NameMC", f"https://namemc.com/profile/{uuid}"
+        )
+    ]
+    if textures.skin:
+        buttons.insert(
+            0, components.linkButton("Download skin", textures.skin)
+        )
+
+    return components.panel(
+        title=f"{name}'s skin",
+        body="\n".join(lines),
+        thumbnail=f"{renderBase}/avatar/{uuid}",
+        images=[
+            components.image(
+                f"{renderBase}/body/{uuid}/right", alt=f"{name}'s skin"
+            )
+        ],
+        buttons=buttons,
+        footer=f"UUID: {dashedUuid(uuid)} · Minecraft",
+    )
+
+
+def statusPanel(
+    data: dict[str, Any], address: str, edition: Edition
+) -> components.Panel:
+    label = "Bedrock" if edition == "bedrock" else "Java"
+    footer = f"{label} Edition · mcsrvstat.us"
+
+    if not data.get("online"):
+        return components.panel(
+            title=f"🔴 {address} is offline",
+            body=(
+                "Couldn't get a response from that server. It may be "
+                "down, or it may not exist at all."
+            ),
+            footer=footer,
+            color=components.RED,
+        )
+
+    motd = "\n".join(
+        line.strip()
+        for line in (data.get("motd") or {}).get("clean") or []
+        if line.strip()
+    )
+    players = data.get("players") or {}
+    host = data.get("ip")
+    port = data.get("port")
+
+    fields: list[components.Field] = [
+        (
+            "Players",
+            f"{players.get('online', 0)} / {players.get('max', 0)}",
+        ),
+    ]
+    if data.get("version"):
+        fields.append(
+            (
+                "Version",
+                components.escapeMarkdown(
+                    stripFormatting(str(data["version"]))
+                ),
+            )
+        )
+    if host:
+        fields.append(
+            ("Address", f"`{host}:{port}`" if port else f"`{host}`")
+        )
+    if data.get("software"):
+        fields.append(("Software", str(data["software"])))
+    if data.get("gamemode"):
+        fields.append(("Game mode", str(data["gamemode"])))
+
+    sample = [
+        player.get("name")
+        for player in players.get("list") or []
+        if player.get("name")
+    ]
+    if sample:
+        names = ", ".join(
+            components.escapeMarkdown(name)
+            for name in sample[:maxListedPlayers]
+        )
+        extra = len(sample) - maxListedPlayers
+        if extra > 0:
+            names += f" and {extra} more"
+        fields.append(("Online now", names))
+
+    return components.panel(
+        title=f"🟢 {address} is online",
+        body=components.codeBlock(motd) if motd else None,
+        fields=fields,
+        thumbnail=(
+            f"{statusBase}/icon/{urllib.parse.quote(address)}"
+            if edition == "java"
+            else None
+        ),
+        footer=footer,
+    )
+
+
+class Minecraft(
+    commands.GroupCog,
+    group_name="minecraft",
+    group_description="Minecraft skins and server status",
+):
+    def __init__(self, bot: commands.Bot) -> None:
+        self.bot = bot
+        self.session = aiohttp.ClientSession(
+            headers={"User-Agent": userAgent},
+            timeout=aiohttp.ClientTimeout(total=10),
+        )
+
+    async def cog_unload(self) -> None:
+        await self.session.close()
+
+    @app_commands.command(
+        name="skin", description="Show a Minecraft player's skin"
+    )
+    @app_commands.describe(player="The player's username or UUID")
+    @checks.hasPermissions(embed_links=True)
+    @mood.sassy
+    async def skin(
+        self, interaction: discord.Interaction, player: str
+    ) -> None:
+        await interaction.response.defer()
+        await interaction.followup.send(
+            view=await self.fetchSkinPanel(player)
+        )
+
+    async def fetchSkinPanel(self, player: str) -> components.Panel:
+        query = normaliseName(player)
+        if query is None:
+            return components.error(
+                "That isn't a valid Minecraft username or UUID."
+            )
+
+        try:
+            uuid = query if uuidPattern.match(query) else None
+            if uuid is None:
+                async with self.session.get(
+                    mojangProfileUrl.format(query)
+                ) as resp:
+                    if resp.status in (204, 404):
+                        return components.error(
+                            "That Minecraft account does not exist."
+                        )
+                    resp.raise_for_status()
+                    uuid = (await resp.json(content_type=None))["id"]
+
+            async with self.session.get(
+                sessionProfileUrl.format(uuid)
+            ) as resp:
+                if resp.status in (204, 404):
+                    return components.error(
+                        "That Minecraft account does not exist."
+                    )
+                resp.raise_for_status()
+                profile = await resp.json(content_type=None)
+        except aiohttp.ClientError, TimeoutError, ValueError, KeyError:
+            return components.error(
+                "Couldn't reach Mojang — try again in a moment."
+            )
+
+        return skinPanel(
+            profile.get("name") or query, uuid, parseTextures(profile)
+        )
+
+    @app_commands.command(
+        name="serverstatus",
+        description="Check whether a Minecraft server is online",
+    )
+    @app_commands.describe(
+        address="The server address, e.g. play.example.com:25565",
+        edition="Java or Bedrock (defaults to Java)",
+    )
+    @checks.hasPermissions(embed_links=True)
+    @mood.sassy
+    async def serverstatus(
+        self,
+        interaction: discord.Interaction,
+        address: str,
+        edition: Edition = "java",
+    ) -> None:
+        await interaction.response.defer()
+        await interaction.followup.send(
+            view=await self.fetchStatusPanel(address, edition)
+        )
+
+    async def fetchStatusPanel(
+        self, address: str, edition: Edition
+    ) -> components.Panel:
+        target = normaliseAddress(address)
+        if target is None:
+            return components.error("That isn't a valid server address.")
+
+        path = "bedrock/3" if edition == "bedrock" else "3"
+        url = f"{statusBase}/{path}/{urllib.parse.quote(target)}"
+        try:
+            async with self.session.get(url) as resp:
+                resp.raise_for_status()
+                data = await resp.json(content_type=None)
+        except aiohttp.ClientError, TimeoutError, ValueError:
+            return components.error(
+                "Couldn't reach the status service — try again in a "
+                "moment."
+            )
+
+        if not isinstance(data, dict):
+            return components.error(
+                "The status service sent back nonsense."
+            )
+        return statusPanel(data, target, edition)
+
+
+async def setup(bot: commands.Bot) -> None:
+    await bot.add_cog(Minecraft(bot))
diff --git a/tests/test_minecraft.py b/tests/test_minecraft.py
new file mode 100644
index 0000000..c3dd93a
--- /dev/null
+++ b/tests/test_minecraft.py
@@ -0,0 +1,99 @@
+import base64
+from commands.cogs.lookups import minecraft
+from discord import ui
+import json
+
+
+def textOf(view: ui.LayoutView) -> str:
+    return "\n".join(
+        item.content
+        for item in view.walk_children()
+        if isinstance(item, ui.TextDisplay)
+    )
+
+
+def profileWith(textures: dict) -> dict:
+    value = base64.b64encode(
+        json.dumps({"textures": textures}).encode()
+    ).decode()
+    return {
+        "id": "069a79f444e94726a5befca90e38aaf5",
+        "name": "Notch",
+        "properties": [{"name": "textures", "value": value}],
+    }
+
+
+def test_normalise_name_accepts_usernames_and_uuids() -> None:
+    assert minecraft.normaliseName(" @Notch ") == "Notch"
+    assert (
+        minecraft.normaliseName("069A79F4-44E9-4726-A5BE-FCA90E38AAF5")
+        == "069a79f444e94726a5befca90e38aaf5"
+    )
+    assert minecraft.normaliseName("not a name") is None
+    assert minecraft.normaliseName("x" * 17) is None
+
+
+def test_normalise_address_strips_noise_and_rejects_junk() -> None:
+    assert (
+        minecraft.normaliseAddress("https://Play.Example.com:25565/")
+        == "play.example.com:25565"
+    )
+    assert minecraft.normaliseAddress("../etc/passwd") is None
+    assert minecraft.normaliseAddress("a b") is None
+    assert minecraft.normaliseAddress("   ") is None
+
+
+def test_parse_textures_reads_skin_model_and_cape() -> None:
+    textures = minecraft.parseTextures(
+        profileWith(
+            {
+                "SKIN": {
+                    "url": "http://textures.minecraft.net/texture/abc",
+                    "metadata": {"model": "slim"},
+                },
+                "CAPE": {"url": "http://textures.minecraft.net/texture/c"},
+            }
+        )
+    )
+    assert textures.skin == "https://textures.minecraft.net/texture/abc"
+    assert textures.slim
+    assert textures.cape is not None
+
+
+def test_parse_textures_handles_missing_or_broken_data() -> None:
+    empty = minecraft.parseTextures({"properties": []})
+    assert empty.skin is None and not empty.slim and empty.cape is None
+
+    broken = minecraft.parseTextures(
+        {"properties": [{"name": "textures", "value": "!!!"}]}
+    )
+    assert broken.skin is None
+
+
+def test_status_panel_offline() -> None:
+    text = textOf(
+        minecraft.statusPanel({"online": False}, "example.com", "java")
+    )
+    assert "offline" in text
+
+
+def test_status_panel_online_lists_details() -> None:
+    data = {
+        "online": True,
+        "ip": "1.2.3.4",
+        "port": 25565,
+        "motd": {"clean": ["  Welcome!  ", ""]},
+        "players": {
+            "online": 12,
+            "max": 100,
+            "list": [{"name": f"p_{i}"} for i in range(12)],
+        },
+        "version": "§aPaper 1.21",
+    }
+    text = textOf(minecraft.statusPanel(data, "example.com", "java"))
+    assert "online" in text
+    assert "12 / 100" in text
+    assert "Welcome!" in text
+    assert "Paper 1.21" in text and "§" not in text
+    assert "`1.2.3.4:25565`" in text
+    assert "and 2 more" in text