Skip to content

Commit

Permalink
docs: 更新文档 (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
st1020 authored Oct 28, 2023
1 parent c20e7bb commit 6ebe175
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,44 @@
<img src="https://img.shields.io/github/license/AliceBotProject/alicebot" alt="license">
</a>
<a href="https://pypi.python.org/pypi/alicebot">
<img src="https://img.shields.io/pypi/v/alicebot" alt="pypi">
<img src="https://img.shields.io/python/required-version-toml?tomlFilePath=https%3A%2F%2Fgithub.com%2FAliceBotProject%2Falicebot%2Fraw%2Fmaster%2Fpyproject.toml" alt="pypi">
</a>
<a href="https://pypi.python.org/pypi/alicebot">
<img src="https://img.shields.io/pypi/pyversions/alicebot" alt="pypi">
<img src="https://img.shields.io/pypi/v/alicebot" alt="pypi">
</a>
<a href="https://github.com/AliceBotProject/alicebot/">
<img src="https://img.shields.io/github/stars/AliceBotProject/alicebot?style=social" alt="github">
</a>
<br />
<a href="https://github.com/psf/black">
<img src="https://img.shields.io/badge/code%20style-black-000000.svg" alt="black">
</a>
<a href="https://github.com/astral-sh/ruff">
<img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json" alt="ruff">
</a>
<a href="https://github.com/pylint-dev/pylint">
<img src="https://img.shields.io/badge/linting-pylint-blue" alt="pylint">
</a>
<a href="https://github.com/Microsoft/pyright">
<img src="https://img.shields.io/badge/type%20checker-pyright-yellowgreen" alt="pyright">
</a>
<a href="https://github.com/python/mypy">
<img src="https://img.shields.io/badge/type%20checker-mypy-blue" alt="mypy">
</a>
<br />
<a href="https://codecov.io/gh/AliceBotProject/alicebot">
<img src="https://codecov.io/gh/AliceBotProject/alicebot/graph/badge.svg?token=3H6ZU6NN0J" alt="codecov">
</a>
<a href="https://github.com/AliceBotProject/alicebot/actions/workflows/test.yml">
<img src="https://github.com/AliceBotProject/alicebot/actions/workflows/test.yml/badge.svg?branch=master&event=push" alt="github">
</a>
<a href="https://github.com/AliceBotProject/alicebot/actions/workflows/lint.yml">
<img src="https://github.com/AliceBotProject/alicebot/actions/workflows/lint.yml/badge.svg?branch=master&event=push" alt="github">
</a>
<a href="https://github.com/AliceBotProject/alicebot/actions/workflows/docs.yml">
<img src="https://github.com/AliceBotProject/alicebot/actions/workflows/docs.yml/badge.svg?branch=master&event=push" alt="github">
</a>
<br />
<a href="https://jq.qq.com/?_wv=1027&k=ZbE3p6tq">
<img src="https://img.shields.io/badge/QQ%E7%BE%A4-674802046-orange" alt="qq-group">
</a>
Expand Down
31 changes: 31 additions & 0 deletions docs/guide/advanced/generic-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,34 @@ class Count(Plugin[MessageEvent, int, Config], init_state=0, config=Config):
```

强烈推荐在编写插件时使用泛型,这可以让你最大化地利用 AliceBot 的类型注解和编辑器的类型检查功能,将大多数的错误杜绝在开发阶段。

在 AliceBot 0.9 以上版本中,当你使用泛型类时,可以不必额外指定创建子类时的 `init_state``config` 参数,AliceBot 将自动从泛型参数中读取:

```python {12}
from typing_extensions import Annotated

from alicebot import ConfigModel, Plugin
from alicebot.adapter.cqhttp.event import MessageEvent


class Config(ConfigModel):
prefix: str = "count: "
suffix: str = ""


class Count(Plugin[MessageEvent, Annotated[int, 0], Config]):
async def handle(self) -> None:
self.state += 1
await self.event.reply(
self.config.prefix + str(self.state) + self.config.suffix
)

async def rule(self) -> bool:
return (
isinstance(self.event, MessageEvent)
and self.event.message.get_plain_text() == "count"
)

```

但是在运行时读取泛型参数中的事件类型并自动判断以省略 `rule()` 方法中的类型判断尚不支持。

0 comments on commit 6ebe175

Please sign in to comment.