Skip to content

Commit

Permalink
fix: 修复 utils 的 dict
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan-Zero committed Nov 15, 2024
1 parent ebe1faf commit 35c7b91
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 26 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ ratelimit 则是令牌桶管理,具体可以查看[限速配置](#限速配置

```yaml
__all__:
- ratelimit: limit.bucket(f"{user.id}_{plugin.name}", max=60)
- ratelimit: limit.bucket(f"{user.id}_{plugin.name}", max_burst=60)
reason: 你话好像有点多了?
```

Expand Down Expand Up @@ -146,19 +146,20 @@ py:
reason: 还有 {bucket.status():.2f} 秒哦。
```

### limit.bucket(key, rate: float, max: int) -> bool
### limit.bucket(key: Hashable, period: int, max_burst: int, count_pre_period: int) -> bool

- key: 限定是什么桶,注意,key = 1 和 key = '1' 是两个桶。
- rate: 速率限制,也就是经过多少秒添加一个令牌,默认 60.
- max: 令牌桶最大容量,默认 3.
- period: 令牌添加间隔,也就是经过多少秒添加 count_pre_period 个令牌,默认 60.
- max_burst: 令牌桶最大容量,默认 3.
- count_pre_period: 每个周期添加的令牌数量

返回值为 True 意味着当前处于速率限制。

### limit.status(key) -> float
### limit.status(key: Hashable) -> float

- key: 桶的key

返回值为还有多久的时间(单位 s)
返回值为还有多久的时间(单位 s)添加下一批令牌,如果为0则是当前还有令牌。

## TODO LIST

Expand Down
2 changes: 0 additions & 2 deletions nonebot_plugin_pam/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@ def __call__(
"IgnoredException": IgnoredException,
}
_kwargs["event"].type = event.__repr_name__()
print(event.__repr_name__())

_kwargs["plugin"].bucket = AwaitAttrDict(
{
"uid": _pbuid,
Expand Down
1 change: 1 addition & 0 deletions nonebot_plugin_pam/ratelimit.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Bucket:
def __new__(cls) -> Self:
if not hasattr(cls, "ins"):
cls.ins = super(Bucket, cls).__new__(cls)
cls.ins.__command__ = {}
return cls.ins

def bucket(
Expand Down
42 changes: 25 additions & 17 deletions nonebot_plugin_pam/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,14 @@ class AwaitAttrDict:
obj: Any

def __getattr__(self, name):
return self[name]

def __setattr__(self, name, value):
self[name] = value

def __getitem__(self, key):
async def _(d):
return d

try:
if hasattr(self, key):
ret = super().__getattribute__(key)
elif hasattr(self.obj, key):
ret = getattr(self.obj, key)
if hasattr(self.obj, name):
ret = getattr(self.obj, name)
else:
if isinstance(self.obj, dict):
return self.obj[key]
else:
return self.obj.__dict__[key]
ret = self[name]
if isinstance(ret, Coroutine):
return ret
elif isinstance(ret, Callable):
Expand All @@ -32,6 +21,15 @@ async def _(d):
except KeyError:
return _(None)

def __setattr__(self, name, value):
self[name] = value

def __getitem__(self, key):
if isinstance(self.obj, dict):
return self.obj[key]
else:
return self.obj.__dict__[key]

def __setitem__(self, key, value):
if isinstance(self.obj, dict):
self.obj[key] = value
Expand All @@ -49,7 +47,19 @@ class AttrDict:
obj: Any

def __getattr__(self, name):
return self[name]
try:
if hasattr(self, name):
ret = super().__getattribute__(name)
elif hasattr(self.obj, name):
ret = getattr(self.obj, name)
else:
if isinstance(self.obj, dict):
return self.obj[name]
else:
return self.obj.__dict__[name]
return ret
except KeyError:
return None

def __setattr__(self, name, value):
self[name] = value
Expand All @@ -65,8 +75,6 @@ def __getitem__(self, key):
return self.obj[key]
else:
return self.obj.__dict__[key]
if isinstance(ret, Callable):
return ret
return ret
except KeyError:
return None
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nonebot-plugin-pam"
version = "0.2.0"
version = "0.2.1"
description = "Nonebot 2 的权限与访问管理。"
authors = ["Yan <[email protected]>"]
license = "Apache License 2.0"
Expand Down

0 comments on commit 35c7b91

Please sign in to comment.