Skip to content

Commit 1d5dd5e

Browse files
committed
Set bypass constant
1 parent a138739 commit 1d5dd5e

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,12 @@ async def handler(): ...
101101
自定义限制对象、定制最大使用量。
102102
```python
103103
from datetime import timedelta # 支持传入 timedelta
104+
from nonebot_plugin_limiter import BYPASS_ENTITY
104105

105106
# 同步样例。获取限制对象的唯一 ID
106107
def get_entity_id(bot: Bot, event: Event): # 可依赖注入
107108
if <any_condition>:
108-
return "__bypass" # 返回 `__bypass` 限制器将不会约束该对象的使用量
109+
return BYPASS_ENTITY # 返回 BYPASS_ENTITY 限制器将不会约束该对象的使用量
109110
return event.get_user_id()
110111

111112
# 异步样例。获取不同用户的最大使用量

nonebot_plugin_limiter/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from .cooldown import Cooldown as Cooldown
2222
from .cooldown import SlidingWindowCooldown as SlidingWindowCooldown
23+
from .entity import BYPASS_ENTITY as BYPASS_ENTITY
2324
from .entity import CooldownEntity as CooldownEntity
2425
from .entity import GlobalScope as GlobalScope
2526
from .entity import PrivateScope as PrivateScope

nonebot_plugin_limiter/cooldown.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from nonebot_plugin_alconna import UniMessage
1515
from tzlocal import get_localzone
1616

17-
from .entity import CooldownEntity
17+
from .entity import BYPASS_ENTITY, CooldownEntity
1818

1919
_tz = get_localzone()
2020

@@ -123,7 +123,7 @@ async def _limiter_dependency(
123123
entity_id: str = Depends(entity_id_dep),
124124
limit: int = Depends(limit_dep),
125125
) -> None:
126-
if entity_id == "__bypass":
126+
if entity_id == BYPASS_ENTITY:
127127
return
128128

129129
now = datetime.now(tz=_tz)
@@ -233,7 +233,7 @@ async def _limiter_dependency(
233233
entity_id: str = Depends(entity_id_dep),
234234
limit: int = Depends(limit_dep),
235235
) -> None:
236-
if entity_id == "__bypass":
236+
if entity_id == BYPASS_ENTITY:
237237
return
238238

239239
now = datetime.now(tz=_tz)

nonebot_plugin_limiter/entity.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from nonebot_plugin_uninfo import get_session
77

88
_IdType = str | int
9+
BYPASS_ENTITY = "__bypass"
910

1011

1112
class CooldownEntity:
@@ -69,13 +70,13 @@ def __init__(self, *, whitelist: None | tuple[_IdType, ...] = None, permission:
6970
async def get_entity_id(self, bot: Bot, event: Event) -> str:
7071
sess = await get_session(bot, event)
7172
if sess is None:
72-
return "__bypass"
73+
return BYPASS_ENTITY
7374

7475
user_id = sess.user.id
7576
if self.whitelist is not None and user_id in self.whitelist:
76-
return "__bypass"
77+
return BYPASS_ENTITY
7778
if self.permission is not None and (await self.permission(bot, event)):
78-
return "__bypass"
79+
return BYPASS_ENTITY
7980
return f"u`{user_id}`"
8081

8182

@@ -106,13 +107,13 @@ def __init__(self, *, whitelist: None | tuple[_IdType, ...] = None, permission:
106107
async def get_entity_id(self, bot: Bot, event: Event) -> str:
107108
sess = await get_session(bot, event)
108109
if sess is None:
109-
return "__bypass"
110+
return BYPASS_ENTITY
110111

111112
scene_id = sess.scene.id
112113
if self.whitelist is not None and scene_id in self.whitelist:
113-
return "__bypass"
114+
return BYPASS_ENTITY
114115
if self.permission is not None and (await self.permission(bot, event)):
115-
return "__bypass"
116+
return BYPASS_ENTITY
116117
return f"s`{scene_id}`"
117118

118119

@@ -151,16 +152,16 @@ def __init__(
151152
async def get_entity_id(self, bot: Bot, event: Event) -> str:
152153
sess = await get_session(bot, event)
153154
if sess is None:
154-
return "__bypass"
155+
return BYPASS_ENTITY
155156

156157
user_id = sess.user.id
157158
scene_id = sess.scene.id
158159
if self.whitelist is not None:
159160
for uid, sid in self.whitelist:
160161
if (uid == "*" or uid == user_id) and (sid == "*" or sid == scene_id):
161-
return "__bypass"
162+
return BYPASS_ENTITY
162163
if self.permission is not None and (await self.permission(bot, event)):
163-
return "__bypass"
164+
return BYPASS_ENTITY
164165
return f"u`{user_id}`_s`{scene_id}`"
165166

166167

@@ -193,13 +194,13 @@ def __init__(self, *, whitelist: None | tuple[_IdType, ...] = None, permission:
193194
async def get_entity_id(self, bot: Bot, event: Event) -> str:
194195
sess = await get_session(bot, event)
195196
if sess is None or not sess.scene.is_private:
196-
return "__bypass"
197+
return BYPASS_ENTITY
197198

198199
user_id = sess.user.id
199200
if self.whitelist is not None and user_id in self.whitelist:
200-
return "__bypass"
201+
return BYPASS_ENTITY
201202
if self.permission is not None and (await self.permission(bot, event)):
202-
return "__bypass"
203+
return BYPASS_ENTITY
203204
return f"u`{user_id}`"
204205

205206

@@ -232,11 +233,11 @@ def __init__(self, *, whitelist: None | tuple[_IdType, ...] = None, permission:
232233
async def get_entity_id(self, bot: Bot, event: Event) -> str:
233234
sess = await get_session(bot, event)
234235
if sess is None or sess.scene.is_private:
235-
return "__bypass"
236+
return BYPASS_ENTITY
236237

237238
user_id = sess.user.id
238239
if self.whitelist is not None and user_id in self.whitelist:
239-
return "__bypass"
240+
return BYPASS_ENTITY
240241
if self.permission is not None and (await self.permission(bot, event)):
241-
return "__bypass"
242+
return BYPASS_ENTITY
242243
return f"u`{user_id}`"

nonebot_plugin_limiter/handler.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from nonebot.params import Depends
2+
from nonebot.typing import T_State, _DependentCallable
3+
4+
class Limiter:
5+
pass

0 commit comments

Comments
 (0)