nonebot2/none/expression.py

18 lines
495 B
Python
Raw Normal View History

2018-06-25 13:14:27 +00:00
import random
from typing import Union, Sequence, Callable
from aiocqhttp import message
def render(expr: Union[str, Sequence[str], Callable], *, escape_args=True,
2018-06-26 00:49:08 +00:00
**kwargs) -> str:
2018-06-25 13:14:27 +00:00
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)