Skip to content

Commit

Permalink
通过 nonebot_plugin_userinfo 获取用户名称
Browse files Browse the repository at this point in the history
  • Loading branch information
he0119 committed Sep 13, 2023
1 parent ccb1f70 commit 6049dfb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/plugins/user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import cast

from expiringdict import ExpiringDict
from nonebot.params import Depends
from nonebot_plugin_alconna import (
Alconna,
AlconnaQuery,
Expand All @@ -12,16 +11,18 @@
on_alconna,
)
from nonebot_plugin_datastore import create_session
from nonebot_plugin_session import Session, SessionLevel, extract_session
from nonebot_plugin_session import SessionLevel
from sqlalchemy import select
from sqlalchemy.orm import selectinload

from src.utils.annotated import MyUserInfo, Session

from .models import Bind, User


async def create_user(pid: str, platform: str):
async def create_user(pid: str, platform: str, nickname: str):
async with create_session() as session:
user = User(name=pid)
user = User(name=nickname)
session.add(user)
bind = Bind(
pid=pid,
Expand Down Expand Up @@ -71,15 +72,14 @@ async def set_user(pid: str, platform: str, aid: int):


@user_cmd.handle()
async def _(session: Session = Depends(extract_session)):
if session.platform == "unknown":
async def _(session: Session, user_info: MyUserInfo):
if session.platform == "unknown" or not session.id1:
await bind_cmd.finish("不支持的平台")

assert session.id1 and session.platform
return

user = await get_user(session.id1, session.platform)
if not user:
user = await create_user(session.id1, session.platform)
user = await create_user(session.id1, session.platform, user_info.user_name)

await user_cmd.finish(f"{user.id} {user.name}")

Expand All @@ -97,9 +97,10 @@ async def _(session: Session = Depends(extract_session)):

@bind_cmd.handle()
async def _(
session: Session,
user_info: MyUserInfo,
token: str | None = None,
remove: Query[bool] = AlconnaQuery("r.value", default=False),
session: Session = Depends(extract_session),
):
if (
session.platform == "unknown"
Expand All @@ -111,7 +112,7 @@ async def _(

user = await get_user(session.id1, session.platform)
if not user:
user = await create_user(session.id1, session.platform)
user = await create_user(session.id1, session.platform, user_info.user_name)

if remove.result:
async with create_session() as db_session:
Expand Down
6 changes: 6 additions & 0 deletions src/utils/annotated.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

from nonebot.params import Depends
from nonebot_plugin_datastore import get_session
from nonebot_plugin_session import Session as _Session
from nonebot_plugin_session import extract_session
from nonebot_plugin_userinfo import EventUserInfo
from nonebot_plugin_userinfo import UserInfo as _MyUserInfo
from sqlalchemy.ext.asyncio import AsyncSession as _AsyncSession

from .depends import (
Expand All @@ -24,3 +28,5 @@
OptionalPlainTextArgs = Annotated[str | None, Depends(get_plaintext_args)]
Platform = Annotated[str, Depends(get_platform)]
OptionalPlatform = Annotated[str | None, Depends(get_platform)]
MyUserInfo = Annotated[_MyUserInfo, EventUserInfo()]
Session = Annotated[_Session, Depends(extract_session)]

0 comments on commit 6049dfb

Please sign in to comment.