mirror of
				https://github.com/nonebot/nonebot2.git
				synced 2025-10-31 15:06:42 +00:00 
			
		
		
		
	Allow positional arguments while rendering expressions
This commit is contained in:
		| @@ -63,22 +63,23 @@ async def send(bot: NoneBot, ctx: Context_T, | |||||||
|         return None |         return None | ||||||
|  |  | ||||||
|  |  | ||||||
| def render_expression(expr: Expression_T, *, | def render_expression(expr: Expression_T, *args, | ||||||
|                       escape_args: bool = True, **kwargs) -> str: |                       escape_args: bool = True, **kwargs) -> str: | ||||||
|     """ |     """ | ||||||
|     Render an expression to message string. |     Render an expression to message string. | ||||||
|  |  | ||||||
|     :param expr: expression to render |     :param expr: expression to render | ||||||
|     :param escape_args: should escape arguments or not |     :param escape_args: should escape arguments or not | ||||||
|  |     :param args: positional arguments used in str.format() | ||||||
|     :param kwargs: keyword arguments used in str.format() |     :param kwargs: keyword arguments used in str.format() | ||||||
|     :return: the rendered message |     :return: the rendered message | ||||||
|     """ |     """ | ||||||
|     if isinstance(expr, Callable): |     if isinstance(expr, Callable): | ||||||
|         expr = expr(**kwargs) |         expr = expr(*args, **kwargs) | ||||||
|     elif isinstance(expr, Sequence) and not isinstance(expr, str): |     elif isinstance(expr, Sequence) and not isinstance(expr, str): | ||||||
|         expr = random.choice(expr) |         expr = random.choice(expr) | ||||||
|     if escape_args: |     if escape_args: | ||||||
|         for k, v in kwargs.items(): |         for k, v in kwargs.items(): | ||||||
|             if isinstance(v, str): |             if isinstance(v, str): | ||||||
|                 kwargs[k] = escape(v) |                 kwargs[k] = escape(v) | ||||||
|     return expr.format(**kwargs) |     return expr.format(*args, **kwargs) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user