Skip to content

Commit

Permalink
adjust onebot_v12
Browse files Browse the repository at this point in the history
  • Loading branch information
MeetWq committed Jun 24, 2023
1 parent bd92044 commit c9cb7b8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
63 changes: 37 additions & 26 deletions nonebot_plugin_userinfo/adapters/onebot_v12.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
@register_user_info_getter(Bot, Event)
class Getter(UserInfoGetter[Bot, Event]):
async def _get_info(self, user_id: str) -> Optional[UserInfo]:
avatar = None
if self.bot.platform == "qq":
avatar = QQAvatar(qq=int(user_id))

info = None

if self.session.level == SessionLevel.LEVEL3:
Expand All @@ -43,37 +39,52 @@ async def _get_info(self, user_id: str) -> Optional[UserInfo]:
logger.warning(f"Error calling get_guild_member_info: {e}")
pass

platform = self.bot.platform
impl = self.bot.impl
if platform == "qqguild" and impl == "nonebot-plugin-all4one":
# 先转成 dict,这样就算以后用扩展模型也不会出错
event_dict = self.event.dict()
try:
if user_id == str(event_dict["qqguild"]["author"]["id"]):
avatar = ImageUrl(
url=str(event_dict["qqguild"]["author"]["avatar"])
)
if not avatar and info:
avatar = ImageUrl(
url=str(info["qqguild"]["user"]["avatar"]) # type: ignore
)
except KeyError:
pass

elif self.session.level == SessionLevel.LEVEL2:
if self.session.id2:
info = await self.bot.get_group_member_info(
group_id=self.session.id2, user_id=user_id
)

if not info:
try:
info = await self.bot.get_user_info(user_id=user_id)
except ActionFailed as e:
logger.warning(f"Error calling get_user_info: {e}")
pass
if self.bot.self_id == user_id:
try:
info = await self.bot.get_self_info()
except ActionFailed as e:
logger.warning(f"Error calling get_self_info: {e}")
pass
else:
try:
info = await self.bot.get_user_info(user_id=user_id)
except ActionFailed as e:
logger.warning(f"Error calling get_user_info: {e}")
pass

if info:
avatar = None

platform = self.bot.platform
impl = self.bot.impl

if platform == "qq":
avatar = QQAvatar(qq=int(user_id))

elif platform == "qqguild" and impl == "nonebot-plugin-all4one":
event_dict = self.event.dict() # 先转成 dict,这样就算以后用扩展模型也不会出错
url = None
try:
if user_id == str(event_dict["qqguild"]["author"]["id"]):
url = str(event_dict["qqguild"]["author"]["avatar"])
except KeyError:
pass
if url is None:
try:
# TODO: 这里的 qqguild 扩展字段是否是嵌套的?
url = str(info["qqguild"]["user"]["avatar"]) # type: ignore
except KeyError:
pass
if url:
avatar = ImageUrl(url=url)

user_name = info["user_name"]
user_displayname = info["user_displayname"]
user_remark = info.get("user_remark")
Expand Down
6 changes: 3 additions & 3 deletions tests/test_onebot_v12.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async def test_channel_message_event(app: App):
user_gender="unknown",
)

# TODO
# TODO: qqguild all4one 相关测试
# async with app.test_matcher(user_info_cmd) as ctx:
# bot = ctx.create_bot(
# base=Bot, self_id="2233", impl="nonebot-plugin-all4one", platform="qqguild"
Expand Down Expand Up @@ -175,8 +175,8 @@ async def test_bot_user_info(app: App):
bot = ctx.create_bot(base=Bot, self_id="2233", impl="walle-q", platform="qq")
ctx.receive_event(bot, event)
ctx.should_call_api(
"get_user_info",
{"user_id": "2233"},
"get_self_info",
{},
{
"user_id": "2233",
"user_name": "Bot",
Expand Down

0 comments on commit c9cb7b8

Please sign in to comment.