~nicoco/slidcord

93aad460bf7f36bb8c576c1c1da5b29a23b969eb — nicoco 5 months ago 2fa7d38
fix: monkeypatch discord.py-self to fix reflect discord API changes

Fixes: https://todo.sr.ht/~nicoco/slidcord/7
1 files changed, 28 insertions(+), 0 deletions(-)

M slidcord/__init__.py
M slidcord/__init__.py => slidcord/__init__.py +28 -0
@@ 2,6 2,7 @@ import logging
from typing import Optional

import discord as di
import discord.utils
from slidge import BaseGateway, FormField
from slidge.util.util import get_version  # noqa: F401
from slixmpp import JID


@@ 9,6 10,33 @@ from slixmpp import JID
from . import commands, config, contact, group, session  # noqa: F401


async def _get_build_number(sess) -> int:
    """Fetches client build number"""
    default_build_number = 9999
    try:
        login_page_request = await sess.get("https://discord.com/login", timeout=7)
        login_page = await login_page_request.text()
        build_url = (
            "https://discord.com/assets/"
            + discord.utils.re.compile(r"assets/+([a-z0-9.]+)\.js").findall(login_page)[
                -2
            ]
            + ".js"
        )
        build_request = await sess.get(build_url, timeout=7)
        build_file = await build_request.text()
        build_find = discord.utils.re.findall(r'Build Number:\D+"(\d+)"', build_file)
        return int(build_find[0]) if build_find else default_build_number
    except Exception:
        discord.utils._log.critical(
            "Could not fetch client build number. Falling back to hardcoded value..."
        )
        return default_build_number


discord.utils._get_build_number = _get_build_number  # type: ignore


class Gateway(BaseGateway):
    COMPONENT_NAME = "Discord (slidge)"
    COMPONENT_TYPE = "discord"