Skip to content

Commit

Permalink
📝 modify describe of unimsg
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Nov 5, 2023
1 parent 2e0d4e8 commit 4d92307
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions website/docs/best-practice/alconna/uniseg.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ async def _(msg: UniMsg):
</TabItem>
<TabItem value="method" label="使用 UniMessage.generate">

注意,`generate` 方法在响应器以外的地方需要 `bot` 参数
注意,`generate` 方法在响应器以外的地方如果不传入 `event``bot` 则无法处理 reply

```python
from nonebot import Message, EventMessage
Expand All @@ -129,7 +129,7 @@ async def _(message: Message = EventMessage()):

不仅如此,你还可以通过 `UniMessage``export``send` 方法来**跨平台发送消息**

`UniMessage.export` 会通过传入的 `bot: Bot` 参数读取适配器信息,并使用对应的生成方法把通用消息转为适配器对应的消息序列
`UniMessage.export` 会通过传入的 `bot: Bot` 参数,或上下文中的 `Bot` 对象读取适配器信息,并使用对应的生成方法把通用消息转为适配器对应的消息序列

```python
from nonebot import Bot, on_command
Expand All @@ -138,8 +138,8 @@ from nonebot_plugin_alconna.uniseg import Image, UniMessage
test = on_command("test")

@test.handle()
async def handle_test(bot: Bot):
await test.send(await UniMessage(Image(path="path/to/img")).export(bot))
async def handle_test():
await test.send(await UniMessage(Image(path="path/to/img")).export())
```

而在 `AlconnaMatcher` 下,`got`, `send`, `reject` 等可以发送消息的方法皆支持使用 `UniMessage`,不需要手动调用 export 方法:
Expand Down Expand Up @@ -170,11 +170,17 @@ from nonebot_plugin_alconna.uniseg import UniMessage
test = on_command("test")

@test.handle()
async def handle(bot: Bot, event: Event):
receipt = await UniMessage.text("hello!").send(event, bot, at_sender=True, reply_to=True)
async def handle():
receipt = await UniMessage.text("hello!").send(at_sender=True, reply_to=True)
await receipt.recall(delay=1)
```

:::caution

在响应器以外的地方,`bot` 参数必须手动传入。

:::

### 构造

如同 `Message`, `UniMessage` 可以传入单个字符串/消息段,或可迭代的字符串/消息段:
Expand Down Expand Up @@ -378,7 +384,7 @@ async def tt():
```python
async def send(
self,
target: Union[Event, Target],
target: Union[Event, Target, None] = None,
bot: Optional[Bot] = None,
fallback: bool = True,
at_sender: Union[str, bool] = False,
Expand All @@ -401,6 +407,8 @@ asycn def _(bot: Bot, event: Event):

```

`send`, `get_target`, `get_message_id` 中与 `event`, `bot` 相关的参数都会尝试从上下文中获取对象。

其中,`Target`:

```python
Expand Down

0 comments on commit 4d92307

Please sign in to comment.