对报错进行了修复, 新加几个装饰器,以及防撤回

This commit is contained in:
Mr.yang
2023-10-08 22:02:57 +08:00
parent e759e5fa9d
commit f321ed6d85
11 changed files with 485 additions and 72 deletions
@@ -5,3 +5,4 @@ from wcfauto.auto_res.core import load_function
Register = load_function(Register)
+104 -17
View File
@@ -1,15 +1,19 @@
# -*- coding: utf-8 -*-
import logging
from abc import abstractmethod
from typing import Any, Callable
from typing import Callable, Any
from wcfauto.event import Event
from wcfauto.wcf import WcfV2 as Wcf
from abc import abstractmethod
from wcfauto.wcf import WcfV2 as Wcf, WxMsgV2 as WxMsg
import logging
class Register(Event):
def __init__(self, debug=True, **kwargs):
"""
消息注册器
"""
def __init__(self,
debug=True,
**kwargs):
super(Register, self).__init__()
logging.basicConfig(level='DEBUG', format="%(asctime)s %(message)s")
self._LOG = logging.getLogger("Demo")
@@ -18,8 +22,10 @@ class Register(Event):
self._wcf = Wcf(debug=debug, **kwargs)
@abstractmethod
def _process_msg(self, wcf: Wcf):
def _process_msg(self,
wcf: Wcf):
"""
外部不可访问接口
有消息的时候,通知分发器分发消息
:param wcf: Wcf
:return: None
@@ -28,11 +34,17 @@ class Register(Event):
@abstractmethod
def _register(self,
func: Callable[[Any], Any]):
kind: str,
register_name: str,
allow_other_receive: bool,
judge_msg: Callable[[Any], Any]):
"""
外部不可访问接口
消息处理工厂, 所有消息处理函数都会汇总在这里提交给事件分发器
:param func: 被装饰的待处理消息函数
:return: func
:param kind: 用于区分被装饰函数属于异步或者同步函数, 主要用于消息分发函数中做区分主要两大类函数
:param register_name: 函数类名 (用于一个装饰器用于装饰多个函数时, 将这些函数统一取名一个类名)
:param allow_other_receive 是否允许其他消息函数(指函数类名不同且必须同为同一个 kind的消息函数(即同为异步或者同步))接收消息
:param judge_msg 用来判断消息是否是本函数要求的消息
"""
raise NotImplementedError
@@ -40,14 +52,21 @@ class Register(Event):
def _processing_async_func(self,
isGroup: bool,
isDivision: bool,
isPyq: bool):
isPyq: bool,
register_name: str,
allow_other_receive: bool,
judge_msg: Callable[[Any], bool]):
"""
异步函数消息处理函数, 用来接受非协程函数
外部不可访问接口
异步函数消息处理函数, 用来接受协程函数, 此函数为异步装饰器函数基函数
参数:
:param isGroup 对消息进行限制, 当为True时, 只接受群消息, 当为False时, 只接受私聊消息,
注意! 仅当isDivision为 True时, isGroup参数生效
:param isPyq 是否接受朋友圈消息
:param isDivision 是否对消息分组
:param register_name 函数类名 (用于一个装饰器用于装饰多个函数时, 将这些函数统一取名一个类名)
:param allow_other_receive 是否允许其他消息函数(指函数类名不同且必须同为同一个 kind的消息函数(即同为异步或者同步))接收消息
:param judge_msg 用来判断消息是否是本函数要求的消息
"""
raise NotImplementedError
@@ -55,14 +74,21 @@ class Register(Event):
def _processing_universal_func(self,
isGroup: bool,
isDivision: bool,
isPyq: bool):
isPyq: bool,
register_name: str,
allow_other_receive: bool,
judge_msg: Callable[[Any], bool]):
"""
同步函数消息处理函数, 用来接受非协程函数
外部不可访问接口
同步函数消息处理函数, 用来接受非协程函数, 此函数为同步装饰器函数基函数
参数:
:param isGroup 对消息进行限制, 当为True时, 只接受群消息, 当为False时, 只接受私聊消息,
注意! 仅当isDivision为 True时, isGroup参数生效
:param isPyq 是否接受朋友圈消息
:param isDivision 是否对消息分组
:param register_name 函数类名 (用于一个装饰器用于装饰多个函数时, 将这些函数统一取名一个类名)
:param allow_other_receive 是否允许其他消息函数(指函数类名不同且必须同为同一个 kind的消息函数(即同为异步或者同步))接收消息
:param judge_msg 用来判断消息是否是本函数要求的消息
"""
raise NotImplementedError
@@ -73,7 +99,7 @@ class Register(Event):
isPyq: bool = False):
"""
外部可访问接口
消息处理函数注册器, 用来接受同步函数
[所有消息] 处理函数注册器, 用来接受同步函数
参数:
:param isGroup 对消息进行限制, 当为True时, 只接受群消息, 当为False时, 只接受私聊消息,
注意! 仅当isDivision为 True时, isGroup参数生效
@@ -89,7 +115,7 @@ class Register(Event):
isPyq: bool = False):
"""
外部可访问接口
消息处理函数注册器, 用来接受异步函数
[所有消息] 处理函数注册器, 用来接受异步函数
参数:
:param isGroup 对消息进行限制, 当为True时, 只接受群消息, 当为False时, 只接受私聊消息,
注意! 仅当isDivision为 True时, isGroup参数生效
@@ -99,7 +125,68 @@ class Register(Event):
raise NotImplementedError
@abstractmethod
def run(self, *args, **kwargs):
def revoke_message_register(self,
isGroup: bool = False,
isDivision: bool = False,
isPyq: bool = False,
allow_other_receive: bool = True):
"""
外部可访问接口
[撤回消息] 处理函数注册器, 用来接受异步函数
参数:
:param isGroup 对消息进行限制, 当为True时, 只接受群消息, 当为False时, 只接受私聊消息,
注意! 仅当isDivision为 True时, isGroup参数生效
:param isPyq 是否接受朋友圈消息
:param isDivision 是否对消息分组
:param allow_other_receive 是否允许符合本函数要求的消息(撤回消息)被其他消息函数(指函数类名不同且
必须同为同一个 kind的消息函数(即同为异步或者同步))接受
"""
raise NotImplementedError
def group_changed_register(self,
isGroup: bool = False,
isDivision: bool = False,
isPyq: bool = False,
allow_other_receive: bool = True):
"""
外部可访问接口
[群成员变动消息]处理函数注册器, 用来接受异步函数
参数:
:param isGroup 对消息进行限制, 当为True时, 只接受群消息, 当为False时, 只接受私聊消息,
注意! 仅当isDivision为 True时, isGroup参数生效
:param isPyq 是否接受朋友圈消息
:param isDivision 是否对消息分组
:param allow_other_receive 是否允许符合本函数要求的消息(群成员消息)被其他消息函数(指函数类名不同且
必须同为同一个 kind的消息函数(即同为异步或者同步))接受
"""
raise NotImplementedError
def custom_message_register(self,
register_name: str,
msg_judge_func: Callable[[WxMsg], bool],
allow_other_receive: bool,
isGroup: bool = False,
isDivision: bool = False,
isPyq: bool = False):
"""
外部可访问接口
[自定义消息]处理函数注册器, 用来接受异步函数
参数:
:param isGroup 对消息进行限制, 当为True时, 只接受群消息, 当为False时, 只接受私聊消息,
注意! 仅当isDivision为 True时, isGroup参数生效
:param isPyq 是否接受朋友圈消息
:param isDivision 是否对消息分组
:param register_name 注册函数类名, 注意不可为空, 注意函数类名决定最后消息分发结果, 因此每个新函数的类名应是不同的
:param msg_judge_func 判断消息是否为符合自定义消息的函数 (函数直接受一个参数 WcfMsg作为参数, 返回值为 bool)
在函数内编写需要符合自定义要求的函数, 但函数返回值必须为 bool类型, 符合要求返回 True,
不符合要求返回 False
:param allow_other_receive 是否允许符合本函数要求的消息(自定义符合消息)被其他消息函数(指函数类名不同且
必须同为同一个 kind的消息函数(即同为异步或者同步))接受
"""
raise NotImplementedError
@abstractmethod
def run(self, pyq=True):
"""
启动程序, 开始接受消息
"""
+96 -18
View File
@@ -5,8 +5,7 @@ import functools
import queue
import traceback
from threading import Thread
from typing import Any, Callable
from typing import Callable, Any
from wcfauto.wcf import WcfV2 as Wcf
from wcfauto.wcf import WxMsgV2 as WxMsg
@@ -18,6 +17,9 @@ def load_function(cls):
cls._processing_universal_func = _processing_universal_func
cls.message_register = message_register
cls.async_message_register = async_message_register
cls.revoke_message_register = revoke_message_register
cls.group_changed_register = group_changed_register
cls.custom_message_register = custom_message_register
cls.run = run
cls.stop_receiving = stop_receiving
return cls
@@ -35,20 +37,33 @@ def _process_msg(self, wcf: Wcf):
def _register(self,
func: Callable[[Any], Any]):
self._add_callback(func, self._wcf)
# 此处必须返回被装饰函数原函数, 否则丢失被装饰函数信息
return func
kind: str,
register_name: str,
allow_other_receive: bool,
judge_msg: Callable[[Any], Any]):
def __register(func: Callable[[Any], Any]):
self._add_callback(func,
self._wcf,
kind=kind,
register_name=register_name,
allow_other_rec=allow_other_receive,
judge_msg=judge_msg)
# 此处必须返回被装饰函数原函数, 否则丢失被装饰函数信息
return func
return __register
def _processing_async_func(self,
isGroup: bool,
isDivision: bool,
isPyq: bool,):
isPyq: bool,
register_name: str,
allow_other_receive: bool,
judge_msg: Callable[[Any], bool]):
def _async_func(func):
@functools.wraps(func)
@self._register
@self._register('async', register_name, allow_other_receive, judge_msg)
async def __async_func(bot: Wcf, message: WxMsg):
try:
# 判断被装饰函数是否为协程函数, 本函数要求是协程函数
@@ -71,17 +86,19 @@ def _processing_async_func(self,
def _processing_universal_func(self,
isGroup: bool,
isDivision: bool,
isPyq: bool, ):
isPyq: bool,
register_name: str,
allow_other_receive: bool,
judge_msg: Callable[[Any], bool]):
def _universal_func(func):
@functools.wraps(func)
@self._register
@self._register('universal', register_name, allow_other_receive, judge_msg)
def universal_func(bot: Wcf, message: WxMsg):
try:
# 判断被装饰函数是否为协程函数, 本函数要求是协程函数
if asyncio.iscoroutinefunction(func):
raise ValueError(
f'这里应使用非协程函数, 而被装饰函数-> ({func.__name__}) <-协程函数')
raise ValueError(f'这里应使用非协程函数, 而被装饰函数-> ({func.__name__}) <-协程函数')
if message.is_pyq() and isPyq:
return func(bot, message)
if not isDivision:
@@ -92,7 +109,6 @@ def _processing_universal_func(self,
return func(bot, message)
except:
traceback.print_exc()
return None
return universal_func
return _universal_func
@@ -101,18 +117,80 @@ def message_register(self,
isGroup: bool = False,
isDivision: bool = False,
isPyq: bool = False):
return self._processing_universal_func(isGroup, isDivision, isPyq)
return self._processing_universal_func(isGroup,
isDivision,
isPyq,
register_name='common',
allow_other_receive=True,
judge_msg=lambda x: True)
def revoke_message_register(self,
isGroup: bool = False,
isDivision: bool = False,
isPyq: bool = False,
allow_other_receive: bool = True):
def judge_msg(msg):
if msg['isRevokeMsg'] and msg['revokmsgid'] is not None:
return True
return False
return self._processing_async_func(isGroup,
isDivision,
isPyq,
register_name='revokeMessage',
allow_other_receive=allow_other_receive,
judge_msg=judge_msg)
def group_changed_register(self,
isGroup: bool = False,
isDivision: bool = False,
isPyq: bool = False,
allow_other_receive: bool = True):
def judge_msg(msg):
if msg['isGroup'] and msg['data']['type'] == 10000:
if '加入了群聊' in msg['data']['content'] or '出了群聊' in msg['data']['content']:
return True
return False
return self._processing_async_func(isGroup,
isDivision,
isPyq,
register_name='groupChanged',
allow_other_receive=allow_other_receive,
judge_msg=judge_msg)
def custom_message_register(self,
register_name: str,
msg_judge_func: Callable[[WxMsg], bool],
allow_other_receive: bool,
isGroup: bool = False,
isDivision: bool = False,
isPyq: bool = False):
return self._processing_async_func(isGroup,
isDivision,
isPyq,
register_name=register_name,
allow_other_receive=allow_other_receive,
judge_msg=msg_judge_func)
def async_message_register(self,
isGroup: bool = False,
isDivision: bool = False,
isPyq: bool = False):
return self._processing_async_func(isGroup, isDivision, isPyq)
return self._processing_async_func(isGroup,
isDivision,
isPyq,
register_name='common',
allow_other_receive=True,
judge_msg=lambda x: True)
def run(self, *args, **kwargs):
self._wcf.enable_receiving_msg(*args, pyq=True, **kwargs)
def run(self, pyq=True):
self._wcf.enable_receiving_msg(pyq=pyq)
Thread(target=self._process_msg, name="GetMessage", args=(self._wcf,), daemon=True).start()
self._LOG.debug("开始接受消息")
self._wcf.keep_running()