From da5b1cf3fa2647e132810e87d75af45d96d3d6b0 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Fri, 13 Jan 2023 23:13:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20person=5Fmessage=E5=92=8Cgroup=5Fmessag?= =?UTF-8?q?e=E7=9A=84=E4=BA=8B=E4=BB=B6=E9=92=A9=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/plugin/host.py | 8 ++++++++ pkg/plugin/models.py | 30 ++++++++++++++++++++++++++++-- pkg/qqbot/manager.py | 22 ++++++++++++++++++++-- plugins/__init__.py | 14 ++++++++++++++ 4 files changed, 70 insertions(+), 4 deletions(-) diff --git a/pkg/plugin/host.py b/pkg/plugin/host.py index f0542029..0961a1e7 100644 --- a/pkg/plugin/host.py +++ b/pkg/plugin/host.py @@ -91,3 +91,11 @@ class PluginHost: def get_runtime_context(self) -> context: """获取运行时上下文""" return context + + def get_bot(self): + """获取机器人对象""" + return context.get_qqbot_manager().bot + + def notify_admin(self, message): + """通知管理员""" + context.get_qqbot_manager().notify_admin(message) diff --git a/pkg/plugin/models.py b/pkg/plugin/models.py index e7202a14..38cc53ac 100644 --- a/pkg/plugin/models.py +++ b/pkg/plugin/models.py @@ -1,22 +1,48 @@ import logging import pkg.plugin.host as host +import pkg.utils.context __current_registering_plugin__ = "" -import pkg.utils.context - PersonMessage = "person_message" +"""收到私聊消息时,在判断是否应该响应前触发 + kwargs: + launcher_type: str 发起对象类型(group/person) + launcher_id: int 发起对象ID(群号/QQ号) + sender_id: int 发送者ID(QQ号) + message_chain: mirai.models.message.MessageChain 消息链 +""" + GroupMessage = "group_message" +"""收到群聊消息时,在判断是否应该响应前触发(所有群消息)""" + PersonNormalMessage = "person_normal_message" +"""判断为应该处理的私聊普通消息时触发""" + PersonCommand = "person_command" +"""判断为应该处理的私聊指令时触发""" + GroupNormalMessage = "group_normal_message" +"""判断为应该处理的群聊普通消息时触发""" + GroupCommand = "group_command" +"""判断为应该处理的群聊指令时触发""" + SessionFirstMessage = "session_first_message" +"""会话被第一次交互时触发""" + SessionReset = "session_reset" +"""会话被用户手动重置时触发""" + SessionExpired = "session_expired" +"""会话过期时触发""" + KeyExceeded = "key_exceeded" +"""api-key超额时触发""" + KeySwitched = "key_switched" +"""api-key超额切换成功时触发""" class Plugin: diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index e13f01c3..54b5cb4c 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -54,7 +54,7 @@ def check_response_rule(text: str) -> (bool, str): class QQBotManager: retry = 3 - bot = None + bot: Mirai = None reply_filter = None @@ -158,7 +158,15 @@ class QQBotManager: # 私聊消息处理 def on_person_message(self, event: MessageEvent): - plugin_host.emit(plugin_models.PersonMessage) + + # 触发事件 + args = { + "launcher_type": "person", + "launcher_id": event.sender.id, + "sender_id": event.sender.id, + "message_chain": event.message_chain, + } + plugin_host.emit(plugin_models.PersonMessage, **args) reply = '' @@ -194,6 +202,16 @@ class QQBotManager: # 群消息处理 def on_group_message(self, event: GroupMessage): + + # 触发事件 + args = { + "launcher_type": "group", + "launcher_id": event.group.id, + "sender_id": event.sender.id, + "message_chain": event.message_chain, + } + plugin_host.emit(plugin_models.GroupMessage, **args) + reply = '' def process(text=None) -> str: diff --git a/plugins/__init__.py b/plugins/__init__.py index e69de29b..27b6735e 100644 --- a/plugins/__init__.py +++ b/plugins/__init__.py @@ -0,0 +1,14 @@ +# 在此处填写的插件仓库将会被自动下载并加载 +# 支持gitee和github仓库 +# 这种加载插件的方式是推荐的,便于插件的获取和更新 +# +# 示例: +# plugin_repos = [ +# 'https://github.com/SampleUser/SampleRepo', +# 'https://gitee.com/SampleUser/SampleRepo' +# ] + + +remote_repos = [ + +]