nonebot2/none/expression.py
Richard Chien c9291e1cba Improve
2018-06-26 08:49:08 +08:00

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)