Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check chronocat in adapter satori #22

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions nonebot_plugin_userinfo/adapters/onebot_v11.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ..getter import UserInfoGetter, register_user_info_getter
from ..image_source import QQAvatar
from ..user_info import UserGender, UserInfo
from ..utils import check_qq_number

try:
from nonebot.adapters.onebot.v11 import (
Expand All @@ -21,6 +22,15 @@
GroupUploadNoticeEvent,
)

def _sex_to_gender(sex: Optional[str]) -> UserGender:
return (
UserGender.male
if sex == "male"
else UserGender.female
if sex == "female"
else UserGender.unknown
)

@register_user_info_getter(Bot, Event)
class Getter(UserInfoGetter[Bot, Event]):
async def _get_info(self, user_id: str) -> Optional[UserInfo]:
Expand Down Expand Up @@ -55,22 +65,15 @@
if info:
qq = info["user_id"]
sex = info.get("sex")
user_gender = (
UserGender.male
if sex == "male"
else UserGender.female
if sex == "female"
else UserGender.unknown
)
return UserInfo(
user_id=str(qq),
user_name=info.get("nickname", ""),
user_displayname=info.get("card"),
user_avatar=QQAvatar(qq=qq),
user_gender=user_gender,
user_gender=_sex_to_gender(sex),
)

if user_id.isdigit() and 5 <= len(user_id) <= 11:
if check_qq_number(user_id):

Check warning on line 76 in nonebot_plugin_userinfo/adapters/onebot_v11.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_userinfo/adapters/onebot_v11.py#L76

Added line #L76 was not covered by tests
return UserInfo(
user_id=user_id,
user_name="",
Expand Down
33 changes: 24 additions & 9 deletions nonebot_plugin_userinfo/adapters/satori.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from nonebot.log import logger

from ..getter import UserInfoGetter, register_user_info_getter
from ..image_source import ImageUrl
from ..image_source import ImageUrl, QQAvatar
from ..user_info import UserInfo
from ..utils import check_qq_number

try:
from nonebot.adapters.satori import Bot
Expand All @@ -29,14 +30,28 @@
logger.warning(f"Error calling user_get: {e}")

if user:
user_name = user.name or user.nick
if user_name:
return UserInfo(
user_id=user.id,
user_name=user_name,
user_displayname=user.nick,
user_avatar=ImageUrl(url=user.avatar) if user.avatar else None,
)
user_name = user.name or user.nick or ""

avatar = None
if user.avatar:
avatar = ImageUrl(url=user.avatar)
else:
if self.event.platform == "chronocat" and check_qq_number(user_id):
avatar = QQAvatar(qq=int(user_id))

Check warning on line 40 in nonebot_plugin_userinfo/adapters/satori.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_userinfo/adapters/satori.py#L39-L40

Added lines #L39 - L40 were not covered by tests

return UserInfo(
user_id=user.id,
user_name=user_name,
user_displayname=user.nick,
user_avatar=avatar,
)

if self.event.platform == "chronocat" and check_qq_number(user_id):
return UserInfo(

Check warning on line 50 in nonebot_plugin_userinfo/adapters/satori.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_userinfo/adapters/satori.py#L49-L50

Added lines #L49 - L50 were not covered by tests
user_id=user_id,
user_name="",
user_avatar=QQAvatar(qq=int(user_id)),
)

except ImportError:
pass
5 changes: 5 additions & 0 deletions nonebot_plugin_userinfo/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import re

import httpx
from nonebot.log import logger
Expand All @@ -17,3 +18,7 @@
logger.warning(f"Error downloading {url}, retry {i}/3: {e}")
await asyncio.sleep(3)
raise NetworkError(f"{url} 下载失败!")


def check_qq_number(qq: str) -> bool:
return bool(re.match(r"^\d{5,11}$", qq))

Check warning on line 24 in nonebot_plugin_userinfo/utils.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_userinfo/utils.py#L24

Added line #L24 was not covered by tests
74 changes: 47 additions & 27 deletions tests/test_satori.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
from nonebug.app import App


def _fake_public_message_create_event(msg: str):
def _fake_public_message_create_event(
msg: str,
*,
user_id: str = "3344",
user_name: str = "Aislinn",
avatar: str = "https://img.kookapp.cn/avatars/xxx",
platform: str = "kook",
):
return PublicMessageCreatedEvent.model_validate(
{
"id": 4,
"type": "message-created",
"platform": "kook",
"platform": platform,
"self_id": "2233",
"timestamp": 17000000000,
"argv": None,
Expand All @@ -26,7 +33,7 @@ def _fake_public_message_create_event(msg: str):
"member": {
"user": None,
"name": None,
"nick": "Aislinn",
"nick": user_name,
"avatar": None,
"joined_at": None,
},
Expand All @@ -44,28 +51,28 @@ def _fake_public_message_create_event(msg: str):
"guild": None,
"member": {
"user": {
"id": "3344",
"name": "Aislinn",
"id": user_id,
"name": user_name,
"nick": None,
"avatar": "https://img.kookapp.cn/avatars/2021-08/GjdUSjtmtD06j06j.png?x-oss-process=style/icon",
"avatar": avatar,
"is_bot": None,
"username": "Aislinn",
"user_id": "3344",
"username": user_name,
"user_id": user_id,
"discriminator": "4261",
},
"name": None,
"nick": "Aislinn",
"nick": user_name,
"avatar": None,
"joined_at": None,
},
"user": {
"id": "3344",
"name": "Aislinn",
"id": user_id,
"name": user_name,
"nick": None,
"avatar": "https://img.kookapp.cn/avatars/2021-08/GjdUSjtmtD06j06j.png?x-oss-process=style/icon",
"avatar": avatar,
"is_bot": None,
"username": "Aislinn",
"user_id": "3344",
"username": user_name,
"user_id": user_id,
"discriminator": "4261",
},
"created_at": None,
Expand All @@ -79,16 +86,16 @@ def _fake_public_message_create_event(msg: str):
"operator": None,
"role": None,
"user": {
"id": "3344",
"name": "Aislinn",
"id": user_id,
"name": user_name,
"nick": None,
"avatar": "https://img.kookapp.cn/avatars/2021-08/GjdUSjtmtD06j06j.png?x-oss-process=style/icon",
"avatar": avatar,
"is_bot": None,
"username": "Aislinn",
"user_id": "3344",
"username": user_name,
"user_id": user_id,
"discriminator": "4261",
},
"_type": "kook",
"_type": platform,
}
)

Expand All @@ -113,9 +120,7 @@ async def test_message_event(app: App):
user_info = UserInfo(
user_id="3344",
user_name="Aislinn",
user_avatar=ImageUrl(
url="https://img.kookapp.cn/avatars/2021-08/GjdUSjtmtD06j06j.png?x-oss-process=style/icon"
),
user_avatar=ImageUrl(url="https://img.kookapp.cn/avatars/xxx"),
)
event = _fake_public_message_create_event("/user_info")
ctx.receive_event(bot, event)
Expand All @@ -124,9 +129,7 @@ async def test_message_event(app: App):
user_info = UserInfo(
user_id="5566",
user_name="Aislinn",
user_avatar=ImageUrl(
url="https://img.kookapp.cn/avatars/2021-08/GjdUSjtmtD06j06j.png?x-oss-process=style/icon"
),
user_avatar=ImageUrl(url="https://img.kookapp.cn/avatars/xxx"),
)
event = _fake_public_message_create_event("/user_info 5566")
ctx.receive_event(bot, event)
Expand All @@ -136,7 +139,7 @@ async def test_message_event(app: App):
User(
id="5566",
name="Aislinn",
avatar="https://img.kookapp.cn/avatars/2021-08/GjdUSjtmtD06j06j.png?x-oss-process=style/icon",
avatar="https://img.kookapp.cn/avatars/xxx",
),
)
ctx.should_call_send(event, "", True, user_info=user_info)
Expand All @@ -149,3 +152,20 @@ async def test_message_event(app: App):
event = _fake_public_message_create_event("/bot_user_info")
ctx.receive_event(bot, event)
ctx.should_call_send(event, "", True, user_info=user_info)

user_info = UserInfo(
user_id="114514",
user_name="User",
user_avatar=ImageUrl(
url="https://thirdqq.qlogo.cn/headimg_dl?dst_uin=114514&spec=640"
),
)
event = _fake_public_message_create_event(
"/user_info",
user_id="114514",
user_name="User",
avatar="https://thirdqq.qlogo.cn/headimg_dl?dst_uin=114514&spec=640",
platform="chronocat",
)
ctx.receive_event(bot, event)
ctx.should_call_send(event, "", True, user_info=user_info)
Loading