mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-12-18 09:25:46 +08:00
18 lines
495 B
Python
18 lines
495 B
Python
import random
|
|
from typing import Union, Sequence, Callable
|
|
|
|
from aiocqhttp import message
|
|
|
|
|
|
def render(expr: Union[str, Sequence[str], Callable], *, escape_args=True,
|
|
**kwargs) -> str:
|
|
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)
|