1
0
forked from bot/app

🐛 修复注册失败的问题

This commit is contained in:
远野千束 2024-08-18 19:58:36 +08:00 committed by GitHub
parent b56ec5ce38
commit 37ed3b0824
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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()
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()