2018-07-29 20:45:07 +08:00
|
|
|
import hashlib
|
2018-12-22 12:44:10 +08:00
|
|
|
import random
|
2019-01-05 21:36:57 +08:00
|
|
|
from typing import Sequence, Callable, Any
|
2018-06-27 22:05:12 +08:00
|
|
|
|
2018-12-22 12:44:10 +08:00
|
|
|
from . import NoneBot
|
2018-10-16 01:03:50 +08:00
|
|
|
from .exceptions import CQHttpError
|
2018-12-22 12:44:10 +08:00
|
|
|
from .message import escape
|
2018-10-16 01:03:50 +08:00
|
|
|
from .typing import Context_T, Message_T, Expression_T
|
2018-06-15 06:58:24 +08:00
|
|
|
|
|
|
|
|
2018-10-16 01:03:50 +08:00
|
|
|
def context_id(ctx: Context_T, *,
|
2018-07-29 20:45:07 +08:00
|
|
|
mode: str = 'default', use_hash: bool = False) -> str:
|
|
|
|
"""
|
|
|
|
Calculate a unique id representing the current context.
|
|
|
|
|
|
|
|
mode:
|
|
|
|
default: one id for one context
|
|
|
|
group: one id for one group or discuss
|
|
|
|
user: one id for one user
|
|
|
|
|
|
|
|
:param ctx: the context dict
|
|
|
|
:param mode: unique id mode: "default", "group", or "user"
|
|
|
|
:param use_hash: use md5 to hash the id or not
|
|
|
|
"""
|
|
|
|
ctx_id = ''
|
|
|
|
if mode == 'default':
|
|
|
|
if ctx.get('group_id'):
|
2019-01-26 20:41:22 +08:00
|
|
|
ctx_id = f'/group/{ctx["group_id"]}'
|
2018-07-29 20:45:07 +08:00
|
|
|
elif ctx.get('discuss_id'):
|
2019-01-26 20:41:22 +08:00
|
|
|
ctx_id = f'/discuss/{ctx["discuss_id"]}'
|
2018-07-29 20:45:07 +08:00
|
|
|
if ctx.get('user_id'):
|
|
|
|
ctx_id += f'/user/{ctx["user_id"]}'
|
|
|
|
elif mode == 'group':
|
|
|
|
if ctx.get('group_id'):
|
2019-01-26 20:41:22 +08:00
|
|
|
ctx_id = f'/group/{ctx["group_id"]}'
|
2018-07-29 20:45:07 +08:00
|
|
|
elif ctx.get('discuss_id'):
|
2019-01-26 20:41:22 +08:00
|
|
|
ctx_id = f'/discuss/{ctx["discuss_id"]}'
|
|
|
|
elif ctx.get('user_id'):
|
|
|
|
ctx_id = f'/user/{ctx["user_id"]}'
|
2018-07-29 20:45:07 +08:00
|
|
|
elif mode == 'user':
|
|
|
|
if ctx.get('user_id'):
|
2019-01-26 20:41:22 +08:00
|
|
|
ctx_id = f'/user/{ctx["user_id"]}'
|
2018-07-29 20:45:07 +08:00
|
|
|
|
|
|
|
if ctx_id and use_hash:
|
|
|
|
ctx_id = hashlib.md5(ctx_id.encode('ascii')).hexdigest()
|
|
|
|
return ctx_id
|
2018-06-27 22:05:12 +08:00
|
|
|
|
|
|
|
|
2018-12-23 20:20:40 +08:00
|
|
|
async def send(bot: NoneBot, ctx: Context_T,
|
|
|
|
message: Message_T, *,
|
|
|
|
ensure_private: bool = False,
|
|
|
|
ignore_failure: bool = True,
|
2019-01-05 21:36:57 +08:00
|
|
|
**kwargs) -> Any:
|
2018-07-04 19:50:42 +08:00
|
|
|
"""Send a message ignoring failure by default."""
|
2018-06-27 22:05:12 +08:00
|
|
|
try:
|
2018-12-23 20:20:40 +08:00
|
|
|
if ensure_private:
|
2018-06-27 22:05:12 +08:00
|
|
|
ctx = ctx.copy()
|
2018-12-23 20:20:40 +08:00
|
|
|
ctx['message_type'] = 'private'
|
2019-01-05 21:36:57 +08:00
|
|
|
return await bot.send(ctx, message, **kwargs)
|
2018-06-27 22:05:12 +08:00
|
|
|
except CQHttpError:
|
|
|
|
if not ignore_failure:
|
|
|
|
raise
|
2019-01-05 21:36:57 +08:00
|
|
|
return None
|
2018-06-27 22:05:12 +08:00
|
|
|
|
|
|
|
|
2019-02-01 19:39:09 +08:00
|
|
|
def render_expression(expr: Expression_T, *args,
|
2018-12-22 12:44:10 +08:00
|
|
|
escape_args: bool = True, **kwargs) -> str:
|
|
|
|
"""
|
|
|
|
Render an expression to message string.
|
|
|
|
|
|
|
|
:param expr: expression to render
|
|
|
|
:param escape_args: should escape arguments or not
|
2019-02-01 19:39:09 +08:00
|
|
|
:param args: positional arguments used in str.format()
|
2018-12-22 12:44:10 +08:00
|
|
|
:param kwargs: keyword arguments used in str.format()
|
|
|
|
:return: the rendered message
|
|
|
|
"""
|
|
|
|
if isinstance(expr, Callable):
|
2019-02-01 19:39:09 +08:00
|
|
|
expr = expr(*args, **kwargs)
|
2018-12-22 12:44:10 +08:00
|
|
|
elif isinstance(expr, Sequence) and not isinstance(expr, str):
|
|
|
|
expr = random.choice(expr)
|
|
|
|
if escape_args:
|
2020-02-10 12:43:04 +08:00
|
|
|
return expr.format(
|
|
|
|
*[escape(s) if isinstance(s, str) else s for s in args],
|
2020-02-14 09:33:46 +08:00
|
|
|
**{k: escape(v) if isinstance(v, str) else v for k, v in kwargs.items()}
|
2020-02-10 12:43:04 +08:00
|
|
|
)
|
2019-02-01 19:39:09 +08:00
|
|
|
return expr.format(*args, **kwargs)
|