Skip to content

Commit

Permalink
调整函数位置
Browse files Browse the repository at this point in the history
  • Loading branch information
he0119 committed Sep 13, 2023
1 parent 6049dfb commit d154648
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 52 deletions.
55 changes: 3 additions & 52 deletions src/plugins/user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,60 +13,11 @@
from nonebot_plugin_datastore import create_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, nickname: str):
async with create_session() as session:
user = User(name=nickname)
session.add(user)
bind = Bind(
pid=pid,
platform=platform,
auser=user,
buser=user,
)
session.add(bind)
await session.commit()
await session.refresh(user)
return user


async def get_user(pid: str, platform: str):
async with create_session() as session:
bind = (
await session.scalars(
select(Bind)
.where(Bind.pid == pid)
.where(Bind.platform == platform)
.options(selectinload(Bind.auser))
)
).one_or_none()

if not bind:
return

return bind.auser


async def set_user(pid: str, platform: str, aid: int):
async with create_session() as session:
bind = (
await session.scalars(
select(Bind).where(Bind.pid == pid).where(Bind.platform == platform)
)
).one_or_none()

if not bind:
raise ValueError("找不到用户信息")

bind.aid = aid
await session.commit()

from .models import Bind
from .utils import create_user, get_user, set_user

user_cmd = on_alconna(Alconna("user"), use_cmd_start=True)

Expand All @@ -81,7 +32,7 @@ async def _(session: Session, user_info: MyUserInfo):
if not user:
user = await create_user(session.id1, session.platform, user_info.user_name)

await user_cmd.finish(f"{user.id} {user.name}")
await user_cmd.finish(f"用户名: {user.name}\n创建日期: {user.created_at}")


tokens = cast(
Expand Down
53 changes: 53 additions & 0 deletions src/plugins/user/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from nonebot_plugin_datastore import create_session
from sqlalchemy import select
from sqlalchemy.orm import selectinload

from .models import Bind, User


async def create_user(pid: str, platform: str, nickname: str):
async with create_session() as session:
user = User(name=nickname)
session.add(user)
bind = Bind(
pid=pid,
platform=platform,
auser=user,
buser=user,
)
session.add(bind)
await session.commit()
await session.refresh(user)
return user


async def get_user(pid: str, platform: str):
async with create_session() as session:
bind = (
await session.scalars(
select(Bind)
.where(Bind.pid == pid)
.where(Bind.platform == platform)
.options(selectinload(Bind.auser))
)
).one_or_none()

if not bind:
return

return bind.auser


async def set_user(pid: str, platform: str, aid: int):
async with create_session() as session:
bind = (
await session.scalars(
select(Bind).where(Bind.pid == pid).where(Bind.platform == platform)
)
).one_or_none()

if not bind:
raise ValueError("找不到用户信息")

bind.aid = aid
await session.commit()

0 comments on commit d154648

Please sign in to comment.