1
0
forked from bot/app

Merge remote-tracking branch 'origin/main'

This commit is contained in:
远野千束 2024-08-18 21:26:29 +08:00
commit d2b693b1e0

View File

@ -11,9 +11,9 @@ Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
import json import json
import os.path import os.path
import platform import platform
from aiohttp import ClientSession
import requests
from git import Repo from git import Repo
from liteyuki.plugin import PluginMetadata from liteyuki.plugin import PluginMetadata
from liteyuki import get_bot, logger from liteyuki import get_bot, logger
@ -25,7 +25,7 @@ liteyuki = get_bot()
commit_hash = Repo(".").head.commit.hexsha commit_hash = Repo(".").head.commit.hexsha
def register_bot(): async def register_bot():
url = "https://api.liteyuki.icu/register" url = "https://api.liteyuki.icu/register"
data = { data = {
"name" : "LiteyukiBot", "name" : "LiteyukiBot",
@ -37,16 +37,18 @@ def register_bot():
} }
try: try:
logger.info("Waiting for register to Liteyuki...") logger.info("Waiting for register to Liteyuki...")
resp = requests.post(url, json=data, timeout=(10, 15)) async with ClientSession() as session:
if resp.status_code == 200: async with session.post(url, json=data, timeout=15) as resp:
data = resp.json() if resp.status == 200:
data = await resp.json()
if liteyuki_id := data.get("liteyuki_id"): if liteyuki_id := data.get("liteyuki_id"):
with open("data/liteyuki/liteyuki.json", "wb") as f: with open("data/liteyuki/liteyuki.json", "wb") as f:
f.write(json.dumps(data).encode("utf-8")) f.write(json.dumps(data).encode("utf-8"))
logger.success(f"Register {liteyuki_id} to Liteyuki successfully") logger.success(f"Register {liteyuki_id} to Liteyuki successfully")
else: else:
raise ValueError(f"Register to Liteyuki failed: {data}") raise ValueError(f"Register to Liteyuki failed: {data}")
else:
raise ValueError(f"Register to Liteyuki failed: {resp.status}")
except Exception as e: except Exception as e:
logger.warning(f"Register to Liteyuki failed, but it's no matter: {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 @liteyuki.on_before_start
async def _(): async def _():
if not os.path.exists("data/liteyuki/liteyuki.json"): 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()