mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-01-19 17:58:26 +08:00
Improve context_id() function
This commit is contained in:
parent
3791560456
commit
1bf3976498
@ -1,3 +1,4 @@
|
|||||||
|
import hashlib
|
||||||
from typing import Dict, Any, Union, List, Sequence, Callable
|
from typing import Dict, Any, Union, List, Sequence, Callable
|
||||||
|
|
||||||
from aiocqhttp import Error as CQHttpError
|
from aiocqhttp import Error as CQHttpError
|
||||||
@ -5,16 +6,40 @@ from aiocqhttp import Error as CQHttpError
|
|||||||
from . import NoneBot, expression
|
from . import NoneBot, expression
|
||||||
|
|
||||||
|
|
||||||
def context_id(ctx: Dict[str, Any]) -> str:
|
def context_id(ctx: Dict[str, Any], *,
|
||||||
"""Calculate a unique id representing the current user."""
|
mode: str = 'default', use_hash: bool = False) -> str:
|
||||||
src = ''
|
"""
|
||||||
if ctx.get('group_id'):
|
Calculate a unique id representing the current context.
|
||||||
src += f'/group/{ctx["group_id"]}'
|
|
||||||
elif ctx.get('discuss_id'):
|
mode:
|
||||||
src += f'/discuss/{ctx["discuss_id"]}'
|
default: one id for one context
|
||||||
if ctx.get('user_id'):
|
group: one id for one group or discuss
|
||||||
src += f'/user/{ctx["user_id"]}'
|
user: one id for one user
|
||||||
return src
|
|
||||||
|
: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'):
|
||||||
|
ctx_id += f'/group/{ctx["group_id"]}'
|
||||||
|
elif ctx.get('discuss_id'):
|
||||||
|
ctx_id += f'/discuss/{ctx["discuss_id"]}'
|
||||||
|
if ctx.get('user_id'):
|
||||||
|
ctx_id += f'/user/{ctx["user_id"]}'
|
||||||
|
elif mode == 'group':
|
||||||
|
if ctx.get('group_id'):
|
||||||
|
ctx_id += f'/group/{ctx["group_id"]}'
|
||||||
|
elif ctx.get('discuss_id'):
|
||||||
|
ctx_id += f'/discuss/{ctx["discuss_id"]}'
|
||||||
|
elif mode == 'user':
|
||||||
|
if ctx.get('user_id'):
|
||||||
|
ctx_id += f'/user/{ctx["user_id"]}'
|
||||||
|
|
||||||
|
if ctx_id and use_hash:
|
||||||
|
ctx_id = hashlib.md5(ctx_id.encode('ascii')).hexdigest()
|
||||||
|
return ctx_id
|
||||||
|
|
||||||
|
|
||||||
async def send(bot: NoneBot, ctx: Dict[str, Any],
|
async def send(bot: NoneBot, ctx: Dict[str, Any],
|
||||||
|
Loading…
Reference in New Issue
Block a user