mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-28 16:16:21 +08:00
19 lines
537 B
Python
19 lines
537 B
Python
|
import random
|
||
|
from typing import Union, Sequence, Callable
|
||
|
|
||
|
from aiocqhttp import message
|
||
|
from aiocqhttp.message import Message
|
||
|
|
||
|
|
||
|
def render(expr: Union[str, Sequence[str], Callable], *, escape_args=True,
|
||
|
**kwargs) -> Message:
|
||
|
if isinstance(expr, Callable):
|
||
|
expr = expr()
|
||
|
elif isinstance(expr, Sequence):
|
||
|
expr = random.choice(expr)
|
||
|
if escape_args:
|
||
|
for k, v in kwargs.items():
|
||
|
if isinstance(v, str):
|
||
|
kwargs[k] = message.escape(v)
|
||
|
return expr.format(**kwargs)
|