mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
✨ 添加并修复webui对kook、discord的头像显示
This commit is contained in:
parent
1e7ae38684
commit
ab4116a253
@ -54,15 +54,20 @@ class ApiDataSource:
|
|||||||
if platform == "qq":
|
if platform == "qq":
|
||||||
login_info = await bot.get_login_info()
|
login_info = await bot.get_login_info()
|
||||||
nickname = login_info["nickname"]
|
nickname = login_info["nickname"]
|
||||||
ava_url = (
|
ava_url: str = (
|
||||||
PlatformUtils.get_user_avatar_url(
|
await PlatformUtils.get_user_avatar_url(
|
||||||
bot.self_id, "qq", BotConfig.get_qbot_uid(bot.self_id)
|
bot.self_id, "qq", BotConfig.get_qbot_uid(bot.self_id)
|
||||||
)
|
)
|
||||||
or ""
|
or ""
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
nickname = bot.self_id
|
nickname = bot.self_id
|
||||||
ava_url = ""
|
ava_url: str = (
|
||||||
|
await PlatformUtils.get_user_avatar_url(
|
||||||
|
bot.self_id, platform
|
||||||
|
)
|
||||||
|
or ""
|
||||||
|
)
|
||||||
bot_info = BotInfo(
|
bot_info = BotInfo(
|
||||||
self_id=bot.self_id, nickname=nickname, ava_url=ava_url, platform=platform
|
self_id=bot.self_id, nickname=nickname, ava_url=ava_url, platform=platform
|
||||||
)
|
)
|
||||||
|
|||||||
@ -76,11 +76,15 @@ class ApiDataSource:
|
|||||||
login_info = await bot.get_login_info()
|
login_info = await bot.get_login_info()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning("调用接口get_login_info失败", "WebUi", e=e)
|
logger.warning("调用接口get_login_info失败", "WebUi", e=e)
|
||||||
|
platform = PlatformUtils.get_platform(bot) or ""
|
||||||
return TemplateBaseInfo(
|
return TemplateBaseInfo(
|
||||||
bot=bot,
|
bot=bot,
|
||||||
self_id=bot.self_id,
|
self_id=bot.self_id,
|
||||||
nickname=login_info["nickname"] if login_info else bot.self_id,
|
nickname=login_info["nickname"] if login_info else bot.self_id,
|
||||||
ava_url=AVA_URL.format(bot.self_id),
|
ava_url=await PlatformUtils.get_user_avatar_url(
|
||||||
|
bot.self_id, platform
|
||||||
|
)
|
||||||
|
or AVA_URL.format(bot.self_id),
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
@ -19,6 +19,10 @@ class BotSetting(BaseModel):
|
|||||||
"""平台超级用户"""
|
"""平台超级用户"""
|
||||||
qbot_id_data: dict[str, str] = Field(default_factory=dict)
|
qbot_id_data: dict[str, str] = Field(default_factory=dict)
|
||||||
"""官bot id:账号id"""
|
"""官bot id:账号id"""
|
||||||
|
kaiheila_bots: list[dict[str,str]] = Field(default_factory=list)
|
||||||
|
"""kaiheilabot id:账号id"""
|
||||||
|
discord_bots: list[dict] = Field(default_factory=list)
|
||||||
|
"""discordbot id:账号id"""
|
||||||
|
|
||||||
def get_qbot_uid(self, qbot_id: str) -> str | None:
|
def get_qbot_uid(self, qbot_id: str) -> str | None:
|
||||||
"""获取官bot账号id
|
"""获取官bot账号id
|
||||||
|
|||||||
@ -233,7 +233,7 @@ class PlatformUtils:
|
|||||||
return await AsyncHttpx.get_content(url) if url else None
|
return await AsyncHttpx.get_content(url) if url else None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_user_avatar_url(
|
async def get_user_avatar_url(
|
||||||
cls, user_id: str, platform: str, appid: str | None = None
|
cls, user_id: str, platform: str, appid: str | None = None
|
||||||
) -> str | None:
|
) -> str | None:
|
||||||
"""快捷获取用户头像url
|
"""快捷获取用户头像url
|
||||||
@ -242,12 +242,37 @@ class PlatformUtils:
|
|||||||
user_id: 用户id
|
user_id: 用户id
|
||||||
platform: 平台
|
platform: 平台
|
||||||
"""
|
"""
|
||||||
if platform != "qq":
|
if platform == "qq":
|
||||||
return None
|
if user_id.isdigit():
|
||||||
if user_id.isdigit():
|
return f"http://q1.qlogo.cn/g?b=qq&nk={user_id}&s=160"
|
||||||
return f"http://q1.qlogo.cn/g?b=qq&nk={user_id}&s=160"
|
else:
|
||||||
|
return f"https://q.qlogo.cn/qqapp/{appid}/{user_id}/640"
|
||||||
|
elif platform == "kaiheila":
|
||||||
|
if not hasattr(cls,'_kook_cache'):
|
||||||
|
params = {
|
||||||
|
'user_id': user_id
|
||||||
|
}
|
||||||
|
header={
|
||||||
|
'Authorization': f"Bot {BotConfig.kaiheila_bots[0].get('token')}"
|
||||||
|
}
|
||||||
|
result=await AsyncHttpx.get(url='https://www.kookapp.cn/api/v3/user/view',params=params,headers=header)
|
||||||
|
cls._kook_cache = result.json()['data']['avatar']
|
||||||
|
return cls._kook_cache
|
||||||
|
else:
|
||||||
|
return cls._kook_cache #使用临时缓存
|
||||||
|
elif platform == "discord":
|
||||||
|
if not hasattr(cls,'_discord_cache'):
|
||||||
|
header={
|
||||||
|
'Authorization': f"Bot {BotConfig.discord_bots[0].get('token')}"
|
||||||
|
}
|
||||||
|
result=await AsyncHttpx.get(url=f'https://discord.com/api/users/{user_id}',headers=header)
|
||||||
|
cls._discord_cache = f"https://cdn.discordapp.com/avatars/{user_id}/{result.json()['avatar']}.png?size=256"
|
||||||
|
return cls._discord_cache
|
||||||
|
else:
|
||||||
|
return cls._discord_cache #使用临时缓存
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return f"https://q.qlogo.cn/qqapp/{appid}/{user_id}/640"
|
return None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def get_group_avatar(cls, gid: str, platform: str) -> bytes | None:
|
async def get_group_avatar(cls, gid: str, platform: str) -> bytes | None:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user