Skip to content

Commit

Permalink
Update to 2.2.0 version.
Browse files Browse the repository at this point in the history
  • Loading branch information
aicorein committed Mar 5, 2024
1 parent dd2ceb0 commit d80482b
Show file tree
Hide file tree
Showing 26 changed files with 658 additions and 328 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pip install melobot
```

版本支持:
- python >= 3.8
- python >= 3.10
- platform == All(mac 平台未测试)
- OneBot 标准 >= 11

Expand Down
18 changes: 12 additions & 6 deletions melobot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from .models import (
AttrSessionRule,
BotSession,
MessageEvent,
MetaEvent,
MsgEvent,
NoticeEvent,
Plugin,
PluginBus,
Expand All @@ -19,10 +19,13 @@
RespEvent,
RWController,
bot,
event,
finish,
get_twin_event,
meta_event,
msg_event,
msg_text,
notice_event,
req_evnt,
send,
send_hup,
send_reply,
Expand All @@ -34,7 +37,7 @@
BotEvent,
BotLife,
BotMatcher,
PriorityLevel,
PriorLevel,
SessionRule,
ShareObjArgs,
User,
Expand Down Expand Up @@ -81,7 +84,7 @@ def get_metainfo() -> MetaInfo:
"session",
"to_cq_arr",
"MetaEvent",
"MsgEvent",
"MessageEvent",
"NoticeEvent",
"Plugin",
"PluginBus",
Expand All @@ -90,7 +93,10 @@ def get_metainfo() -> MetaInfo:
"RespEvent",
"RWController",
"bot",
"event",
"msg_event",
"notice_event",
"req_evnt",
"meta_event",
"msg_text",
"finish",
"get_twin_event",
Expand All @@ -101,7 +107,7 @@ def get_metainfo() -> MetaInfo:
"BotEvent",
"BotLife",
"BotMatcher",
"PriorityLevel",
"PriorLevel",
"SessionRule",
"ShareObjArgs",
"User",
Expand Down
8 changes: 4 additions & 4 deletions melobot/core/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
NoticeEventHandler,
ReqEventHandler,
)
from ..types.core import IEventDispatcher
from ..types.core import AbstractDispatcher
from ..types.exceptions import *
from ..types.models import BotLife
from ..types.typing import *
from ..utils.logger import Logger


class BotDispatcher(IEventDispatcher):
class BotDispatcher(AbstractDispatcher):
"""
bot 调度模块。负责将传递的普通事件送入各事件总线
(接收的事件类型:消息、请求和通知)
Expand Down Expand Up @@ -62,10 +62,10 @@ async def dispatch(self, event: BotEvent) -> None:
把事件分发到对应的事件总线
"""
await self._ready_signal.wait()
await BotHookBus.emit(BotLife.EVENT_RECEIVED, event, wait=True)
await BotHookBus.emit(BotLife.EVENT_BUILT, event, wait=True)

try:
permit_priority = PriorityLevel.MIN.value
permit_priority = PriorLevel.MIN.value
handlers = self._handlers[event.type]
for handler in handlers:
# 事件处理器优先级不够,则不分配给它处理
Expand Down
4 changes: 2 additions & 2 deletions melobot/core/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from ..models.bot import BOT_PROXY, BotHookBus
from ..models.ipc import PluginBus, PluginStore
from ..models.plugin import Plugin
from ..types.core import IActionResponder
from ..types.core import AbstractResponder
from ..types.exceptions import *
from ..types.models import BotLife
from ..types.typing import *
Expand Down Expand Up @@ -90,7 +90,7 @@ def load(
cls,
plugin_target: Union[str, Type[Plugin]],
logger: Logger,
responder: IActionResponder,
responder: AbstractResponder,
) -> Plugin:
"""
加载插件
Expand Down
12 changes: 6 additions & 6 deletions melobot/core/linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
from ..models.action import BotAction
from ..models.bot import BotHookBus
from ..models.event import BotEventBuilder
from ..types.core import IActionSender, IEventDispatcher, IRespDispatcher
from ..types.core import AbstractDispatcher, AbstractSender
from ..types.exceptions import *
from ..types.models import BotLife
from ..types.typing import *
from ..utils.logger import Logger


class BotLinker(IActionSender):
class BotLinker(AbstractSender):
"""
Bot 连接模块通过连接适配器的代理,完成事件接收与行为发送。
"""
Expand All @@ -43,13 +43,13 @@ def __init__(
self._send_lock = aio.Lock()
self._rest_time = send_interval
self._pre_send_time = time.time()
self._common_dispatcher: IEventDispatcher
self._resp_dispatcher: IRespDispatcher
self._common_dispatcher: AbstractDispatcher
self._resp_dispatcher: AbstractDispatcher

def bind(
self,
common_dispatcher: IEventDispatcher,
resp_dispatcher: IRespDispatcher,
common_dispatcher: AbstractDispatcher,
resp_dispatcher: AbstractDispatcher,
) -> None:
"""
绑定其他核心组件的方法。
Expand Down
8 changes: 4 additions & 4 deletions melobot/core/responder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

from ..models.action import BotAction
from ..models.event import RespEvent
from ..types.core import IActionResponder, IActionSender, IRespDispatcher
from ..types.core import AbstractDispatcher, AbstractResponder, AbstractSender
from ..types.exceptions import *
from ..types.typing import *
from ..utils.logger import Logger


class BotResponder(IActionResponder, IRespDispatcher):
class BotResponder(AbstractResponder, AbstractDispatcher):
"""
bot 响应模块,是 action 发送方和 bot 连接模块的媒介。
提供 action 发送、响应回送功能
Expand All @@ -22,9 +22,9 @@ def __init__(self, logger: Logger) -> None:
self.logger = logger

self._ready_signal = aio.Event()
self._action_sender: IActionSender
self._action_sender: AbstractSender

def bind(self, action_sender: IActionSender) -> None:
def bind(self, action_sender: AbstractSender) -> None:
"""
绑定其他核心组件的方法。独立出来,方便上层先创建实例再调用
"""
Expand Down
2 changes: 1 addition & 1 deletion melobot/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class MetaInfo:
def __init__(self) -> None:
self.VER = "2.1.0"
self.VER = "2.2.0"
self.PROJ_NAME = "MeloBot"
self.PROJ_DESC = "A qbot module with friendly interface, session control and plugin-supported."
self.PROJ_SRC = "https://github.com/aicorein/melobot"
Expand Down
7 changes: 5 additions & 2 deletions melobot/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from .action import *
from .base import RWController, get_twin_event, to_cq_arr
from .bot import BOT_PROXY as bot
from .event import MetaEvent, MsgEvent, NoticeEvent, RequestEvent, RespEvent
from .event import MessageEvent, MetaEvent, NoticeEvent, RequestEvent, RespEvent
from .ipc import PluginBus, PluginStore
from .plugin import Plugin
from .session import SESSION_LOCAL as session
from .session import (
AttrSessionRule,
BotSession,
event,
finish,
meta_event,
msg_event,
msg_text,
notice_event,
req_evnt,
send,
send_hup,
send_reply,
Expand Down
17 changes: 14 additions & 3 deletions melobot/models/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from copy import deepcopy

from ..types.exceptions import *
from ..types.models import Flagable
from ..types.typing import *
from ..utils.base import get_id
from .event import *
Expand Down Expand Up @@ -395,7 +396,7 @@ def custom_msg_node(
msgs = temp
ret = {
"type": "node",
"data": {"name": sendName, "uin": str(sendId), "content": msgs},
"data": {"name": sendName, "uin": sendId, "content": msgs},
}
if seq:
ret["data"]["seq"] = seq
Expand All @@ -406,7 +407,7 @@ def refer_msg_node(msgId: int) -> MsgNodeDict:
"""
引用消息节点构造方法
"""
return {"type": "node", "data": {"id": str(msgId)}}
return {"type": "node", "data": {"id": msgId}}


class ActionPacker(ABC):
Expand All @@ -420,7 +421,7 @@ def __init__(self) -> None:
self.params: dict


class BotAction:
class BotAction(Flagable):
"""
Bot 行为类
"""
Expand All @@ -431,6 +432,7 @@ def __init__(
respWaited: bool = False,
triggerEvent: BotEvent = None,
) -> None:
super().__init__()
# 只有 action 对应的响应需要被等待单独处理时,才会生成 id
self.resp_id: Union[str, None] = str(get_id()) if respWaited else None
self.type: str = package.type
Expand Down Expand Up @@ -461,6 +463,15 @@ def flatten(self, indent: int = None) -> str:
"""
return json.dumps(self.extract(), ensure_ascii=False, indent=indent)

def _fill_trigger(self, event: BotEvent) -> None:
"""
后期指定触发 event
"""
if self.trigger is None:
self.trigger = event
return
raise BotActionError("action 已记录触发 event,拒绝再次记录")


class MsgPacker(ActionPacker):
"""
Expand Down
8 changes: 4 additions & 4 deletions melobot/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ def replace_func(m) -> str:
.replace(",", ",")
)
if val.isdigit():
val = int(val)
data[name] = int(val)
continue
try:
val = float(val)
data[name] = float(val)
except Exception:
pass
data[name] = val
data[name] = val
content.append({"type": cq_type, "data": data})
return content

Expand Down
6 changes: 3 additions & 3 deletions melobot/models/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from types import MethodType

from ..meta import MODULE_MODE_FLAG, MODULE_MODE_SET
from ..types.core import IActionResponder
from ..types.core import AbstractResponder
from ..types.exceptions import *
from ..types.models import BotLife, HookRunnerArgs
from ..types.typing import *
Expand Down Expand Up @@ -43,10 +43,10 @@ class BotHookBus:
v: [] for k, v in BotLife.__members__.items()
}
__logger: Logger
__responder: IActionResponder
__responder: AbstractResponder

@classmethod
def _bind(cls, logger: Logger, responder: IActionResponder) -> None:
def _bind(cls, logger: Logger, responder: AbstractResponder) -> None:
"""
初始化该类,绑定全局日志器和行为响应器
"""
Expand Down
Loading

0 comments on commit d80482b

Please sign in to comment.