Skip to content

Commit

Permalink
feat: 使用自带 typing 的 expiringdictx (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
he0119 authored Aug 7, 2024
1 parent 3bcf452 commit 6c2c4bd
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 91 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/
### Changed

- 事件响应器现在将阻断事件的传播
- 使用自带 typing 的 expiringdictx

## [0.3.0] - 2024-06-28

Expand Down
66 changes: 33 additions & 33 deletions nonebot_plugin_user/matchers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import random
from typing import Optional, cast
from typing import Optional

from expiringdict import ExpiringDict
from expiringdictx import ExpiringDict
from nonebot.adapters import Bot
from nonebot_plugin_alconna import (
Alconna,
Expand Down Expand Up @@ -76,9 +76,8 @@ async def _(bot: Bot, session: UserSession):
await inspect_cmd.finish("\n".join(msgs))


tokens = cast(
dict[str, tuple[str, str, int, Optional[SessionLevel]]],
ExpiringDict(max_len=100, max_age_seconds=300),
tokens = ExpiringDict[str, tuple[str, str, int, Optional[SessionLevel]]](
capacity=100, default_age=300
)


Expand Down Expand Up @@ -120,32 +119,33 @@ async def _(
)

# 绑定流程
if token in tokens:
# 平台的相关信息
platform, platform_id, user_id, level = tokens.pop(token)
# 群内绑定的第一步,会在原始平台发送令牌
# 此时 platform_id 和 platform 为目标平台的信息
if level == SessionLevel.LEVEL2 or level == SessionLevel.LEVEL3:
token = generate_token()
tokens[token] = (session.platform, session.platform_id, user_id, None)
await bind_cmd.finish(
f"令牌核验成功!下面将进行第二步操作。\n请在 5 分钟内使用你的账号在目标平台内向机器人发送以下文本:\n/bind {token}\n注意:当前平台是你的原始平台,这里的用户数据将覆盖目标平台的数据。" # noqa: E501
)
# 群内绑定的第二步,会在目标平台发送令牌
# 此时 platform_id 和 platform 为原始平台的信息
# 需要重新获取其用户信息,然后将目标平台绑定至原始平台
elif level is None:
if session.user_id != user_id:
await bind_cmd.finish("请使用最开始要绑定账号进行操作")

user = await get_user(platform, platform_id)
await set_bind(session.platform, session.platform_id, user.id)
await bind_cmd.finish("绑定成功")
# 私聊绑定时,会在原始平台发送令牌
# 此时 platform_id 和 platform 为目标平台的信息
# 直接将目标平台绑定至原始平台
elif level == SessionLevel.LEVEL1:
await set_bind(platform, platform_id, session.user_id)
await bind_cmd.finish("绑定成功")
else:
bind_info = tokens.pop(token)
if bind_info is None:
await bind_cmd.finish("令牌不存在或已过期")

# 平台的相关信息
platform, platform_id, user_id, level = bind_info
# 群内绑定的第一步,会在原始平台发送令牌
# 此时 platform_id 和 platform 为目标平台的信息
if level == SessionLevel.LEVEL2 or level == SessionLevel.LEVEL3:
token = generate_token()
tokens[token] = (session.platform, session.platform_id, user_id, None)
await bind_cmd.finish(
f"令牌核验成功!下面将进行第二步操作。\n请在 5 分钟内使用你的账号在目标平台内向机器人发送以下文本:\n/bind {token}\n注意:当前平台是你的原始平台,这里的用户数据将覆盖目标平台的数据。" # noqa: E501
)
# 群内绑定的第二步,会在目标平台发送令牌
# 此时 platform_id 和 platform 为原始平台的信息
# 需要重新获取其用户信息,然后将目标平台绑定至原始平台
elif level is None:
if session.user_id != user_id:
await bind_cmd.finish("请使用最开始要绑定账号进行操作")

user = await get_user(platform, platform_id)
await set_bind(session.platform, session.platform_id, user.id)
await bind_cmd.finish("绑定成功")
# 私聊绑定时,会在原始平台发送令牌
# 此时 platform_id 和 platform 为目标平台的信息
# 直接将目标平台绑定至原始平台
elif level == SessionLevel.LEVEL1:
await set_bind(platform, platform_id, session.user_id)
await bind_cmd.finish("绑定成功")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies = [
"nonebot-plugin-orm>=0.7.0",
"nonebot-plugin-alconna>=0.37.1",
"nonebot-plugin-session>=0.3.0",
"expiringdict>=1.2.2",
"expiringdictx>=1.1.0",
]
readme = "README.md"
license = { file = "LICENSE" }
Expand Down
67 changes: 22 additions & 45 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ anyio==4.4.0
# via httpx
# via starlette
# via watchfiles
arclet-alconna==1.8.18
arclet-alconna==1.8.23
# via arclet-alconna-tools
# via nonebot-plugin-alconna
arclet-alconna-tools==0.7.6
arclet-alconna-tools==0.7.9
# via nonebot-plugin-alconna
asgiref==3.8.1
# via nonebug
Expand All @@ -42,27 +42,20 @@ click==8.1.7
# via bump-my-version
# via nonebot-plugin-orm
# via rich-click
# via typer
# via uvicorn
colorama==0.4.6 ; platform_system == 'Windows' or sys_platform == 'win32'
# via click
# via loguru
# via pytest
# via uvicorn
coverage==7.6.0
coverage==7.6.1
# via pytest-cov
dnspython==2.6.1
# via email-validator
email-validator==2.2.0
# via fastapi
execnet==2.1.1
# via pytest-xdist
expiringdict==1.2.2
expiringdictx==1.1.0
# via nonebot-plugin-user
fastapi==0.111.0
fastapi==0.112.0
# via nonebot2
fastapi-cli==0.0.4
# via fastapi
freezegun==1.5.1
greenlet==3.0.3
# via sqlalchemy
Expand All @@ -78,30 +71,27 @@ httpcore==1.0.5
httptools==0.6.1
# via uvicorn
httpx==0.27.0
# via fastapi
# via nonebot2
hyperframe==6.0.1
# via h2
idna==3.7
# via anyio
# via email-validator
# via httpx
# via requests
# via yarl
importlib-metadata==7.2.1
importlib-metadata==8.2.0
# via nonebot-plugin-alconna
iniconfig==2.0.0
# via pytest
jinja2==3.1.4
# via fastapi
loguru==0.7.2
# via nonebot2
lru-dict==1.3.0
# via expiringdictx
mako==1.3.5
# via alembic
markdown-it-py==3.0.0
# via rich
markupsafe==2.1.5
# via jinja2
# via mako
mdurl==0.1.2
# via markdown-it-py
Expand All @@ -110,22 +100,22 @@ msgpack==1.0.8
multidict==6.0.5
# via async-asgi-testclient
# via yarl
nepattern==0.7.4
nepattern==0.7.6
# via arclet-alconna
# via arclet-alconna-tools
# via nonebot-plugin-alconna
nonebot-adapter-onebot==2.4.4
nonebot-adapter-qq==1.4.4
nonebot-adapter-qq==1.5.0
nonebot-adapter-satori==0.12.3
nonebot-plugin-alconna==0.49.0
nonebot-plugin-alconna==0.51.1
# via nonebot-plugin-user
nonebot-plugin-localstore==0.7.0
nonebot-plugin-localstore==0.7.1
# via nonebot-plugin-orm
nonebot-plugin-orm==0.7.4
nonebot-plugin-orm==0.7.5
# via nonebot-plugin-user
nonebot-plugin-session==0.3.1
nonebot-plugin-session==0.3.2
# via nonebot-plugin-user
nonebot-plugin-waiter==0.6.2
nonebot-plugin-waiter==0.7.1
# via nonebot-plugin-alconna
nonebot2==2.3.2
# via nonebot-adapter-onebot
Expand All @@ -139,8 +129,6 @@ nonebot2==2.3.2
# via nonebot-plugin-waiter
# via nonebug
nonebug==0.3.7
orjson==3.10.6
# via fastapi
packaging==24.1
# via pytest
pluggy==1.5.0
Expand All @@ -163,13 +151,13 @@ pygments==2.18.0
# via rich
pygtrie==2.5.0
# via nonebot2
pytest==8.2.2
pytest==8.3.2
# via nonebug
# via pytest-asyncio
# via pytest-cov
# via pytest-mock
# via pytest-xdist
pytest-asyncio==0.23.7
pytest-asyncio==0.23.8
pytest-cov==5.0.0
pytest-mock==3.14.0
pytest-xdist==3.6.1
Expand All @@ -179,9 +167,7 @@ python-dotenv==1.0.1
# via nonebot2
# via pydantic-settings
# via uvicorn
python-multipart==0.0.9
# via fastapi
pyyaml==6.0.1
pyyaml==6.0.2
# via uvicorn
questionary==2.0.1
# via bump-my-version
Expand All @@ -190,31 +176,26 @@ requests==2.32.3
rich==13.7.1
# via bump-my-version
# via rich-click
# via typer
rich-click==1.8.3
# via bump-my-version
shellingham==1.5.4
# via typer
six==1.16.0
# via python-dateutil
sniffio==1.3.1
# via anyio
# via httpx
sqlalchemy==2.0.31
sqlalchemy==2.0.32
# via alembic
# via nonebot-plugin-orm
starlette==0.37.2
# via fastapi
strenum==0.4.15
# via nonebot-plugin-session
tarina==0.5.4
tarina==0.5.5
# via arclet-alconna
# via nepattern
# via nonebot-plugin-alconna
tomlkit==0.13.0
# via bump-my-version
typer==0.12.3
# via fastapi-cli
typing-extensions==4.12.2
# via aiosqlite
# via alembic
Expand All @@ -231,17 +212,13 @@ typing-extensions==4.12.2
# via rich-click
# via sqlalchemy
# via tarina
# via typer
ujson==5.10.0
# via fastapi
urllib3==2.2.2
# via requests
uvicorn==0.30.1
# via fastapi
uvicorn==0.30.5
# via nonebot2
uvloop==0.19.0 ; platform_python_implementation != 'PyPy' and sys_platform != 'cygwin' and sys_platform != 'win32'
# via uvicorn
watchfiles==0.22.0
watchfiles==0.23.0
# via uvicorn
wcmatch==9.0
# via bump-my-version
Expand Down
26 changes: 14 additions & 12 deletions requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,47 @@ alembic==1.13.2
# via nonebot-plugin-orm
annotated-types==0.7.0
# via pydantic
arclet-alconna==1.8.18
arclet-alconna==1.8.23
# via arclet-alconna-tools
# via nonebot-plugin-alconna
arclet-alconna-tools==0.7.6
arclet-alconna-tools==0.7.9
# via nonebot-plugin-alconna
click==8.1.7
# via nonebot-plugin-orm
colorama==0.4.6 ; platform_system == 'Windows' or sys_platform == 'win32'
# via click
# via loguru
expiringdict==1.2.2
expiringdictx==1.1.0
# via nonebot-plugin-user
greenlet==3.0.3 ; python_version < '3.13' and (platform_machine == 'AMD64' or platform_machine == 'WIN32' or platform_machine == 'aarch64' or platform_machine == 'amd64' or platform_machine == 'ppc64le' or platform_machine == 'win32' or platform_machine == 'x86_64')
# via sqlalchemy
idna==3.7
# via yarl
importlib-metadata==7.2.1
importlib-metadata==8.2.0
# via nonebot-plugin-alconna
loguru==0.7.2
# via nonebot2
lru-dict==1.3.0
# via expiringdictx
mako==1.3.5
# via alembic
markupsafe==2.1.5
# via mako
multidict==6.0.5
# via yarl
nepattern==0.7.4
nepattern==0.7.6
# via arclet-alconna
# via arclet-alconna-tools
# via nonebot-plugin-alconna
nonebot-plugin-alconna==0.49.0
nonebot-plugin-alconna==0.51.1
# via nonebot-plugin-user
nonebot-plugin-localstore==0.7.0
nonebot-plugin-localstore==0.7.1
# via nonebot-plugin-orm
nonebot-plugin-orm==0.7.4
nonebot-plugin-orm==0.7.5
# via nonebot-plugin-user
nonebot-plugin-session==0.3.1
nonebot-plugin-session==0.3.2
# via nonebot-plugin-user
nonebot-plugin-waiter==0.6.2
nonebot-plugin-waiter==0.7.1
# via nonebot-plugin-alconna
nonebot2==2.3.2
# via nonebot-plugin-alconna
Expand All @@ -70,12 +72,12 @@ pygtrie==2.5.0
# via nonebot2
python-dotenv==1.0.1
# via nonebot2
sqlalchemy==2.0.31
sqlalchemy==2.0.32
# via alembic
# via nonebot-plugin-orm
strenum==0.4.15
# via nonebot-plugin-session
tarina==0.5.4
tarina==0.5.5
# via arclet-alconna
# via nepattern
# via nonebot-plugin-alconna
Expand Down

0 comments on commit 6c2c4bd

Please sign in to comment.