From 37ed3b0824167889fd1f3ee49c3adbe526bbd57d Mon Sep 17 00:00:00 2001 From: Snowykami Date: Sun, 18 Aug 2024 19:58:36 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/liteyuki_plugins/register_service.py | 32 +++++++++++++----------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/liteyuki_plugins/register_service.py b/src/liteyuki_plugins/register_service.py index 5f21afe5..1a319afc 100644 --- a/src/liteyuki_plugins/register_service.py +++ b/src/liteyuki_plugins/register_service.py @@ -11,9 +11,9 @@ Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved import json import os.path import platform - -import requests +from aiohttp import ClientSession from git import Repo + from liteyuki.plugin import PluginMetadata from liteyuki import get_bot, logger @@ -25,7 +25,7 @@ liteyuki = get_bot() commit_hash = Repo(".").head.commit.hexsha -def register_bot(): +async def register_bot(): url = "https://api.liteyuki.icu/register" data = { "name" : "LiteyukiBot", @@ -37,16 +37,18 @@ def register_bot(): } try: logger.info("Waiting for register to Liteyuki...") - resp = requests.post(url, json=data, timeout=(10, 15)) - if resp.status_code == 200: - data = resp.json() - if liteyuki_id := data.get("liteyuki_id"): - with open("data/liteyuki/liteyuki.json", "wb") as f: - f.write(json.dumps(data).encode("utf-8")) - logger.success(f"Register {liteyuki_id} to Liteyuki successfully") - else: - raise ValueError(f"Register to Liteyuki failed: {data}") - + async with ClientSession() as session: + async with session.post(url, json=data, timeout=15) as resp: + if resp.status == 200: + data = await resp.json() + if liteyuki_id := data.get("liteyuki_id"): + with open("data/liteyuki/liteyuki.json", "wb") as f: + f.write(json.dumps(data).encode("utf-8")) + logger.success(f"Register {liteyuki_id} to Liteyuki successfully") + else: + raise ValueError(f"Register to Liteyuki failed: {data}") + else: + raise ValueError(f"Register to Liteyuki failed: {resp.status}") except Exception as e: logger.warning(f"Register to Liteyuki failed, but it's no matter: {e}") @@ -54,4 +56,6 @@ def register_bot(): @liteyuki.on_before_start async def _(): if not os.path.exists("data/liteyuki/liteyuki.json"): - register_bot() + if not os.path.exists("data/liteyuki"): + os.makedirs("data/liteyuki") + await register_bot()