This repository has been archived by the owner on Dec 21, 2021. It is now read-only.
forked from OlivOS-Team/OlivaDiceCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.py
219 lines (204 loc) · 8.74 KB
/
middleware.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import asyncio
import hashlib
from typing import Callable, Optional
from nonebot import get_bots
from nonebot.adapters.cqhttp.bot import Bot
from nonebot.adapters.cqhttp.event import *
from nonebot.log import logger
class PluginEvent:
def __init__(
self,
bot: Optional[Bot] = None,
event: Optional[Event] = None,
log_func: Optional[Callable] = None,
):
self.platform = {}
self.platform["sdk"] = None
self.platform["platform"] = None
self.platform["model"] = None
self.data = None
self.active = False
self.blocked = False
self.log_func = log_func
self.base_info = {}
self.base_info["time"] = None
self.base_info["self_id"] = None
self.base_info["type"] = None
self.plugin_info = {}
self.plugin_info["func_type"] = None
self.plugin_info[
"message_mode_rx"
] = "old_string" # OlivOS_message_mode_rx_default
self.plugin_info[
"message_mode_tx"
] = "olivos_string" # OlivOS_message_mode_rx_default
self.plugin_info["name"] = "unity"
self.plugin_info["namespace"] = "unity"
self.plugin_info["tx_queue"] = []
if bot:
if isinstance(bot, Bot):
self.platform = {"sdk": "onebot", "platform": "qq", "model": "nonebot"}
self.bot_info = BotInfo(int(bot.self_id), self.platform)
if event:
self.get_Event_from_SDK(event)
# self.get_Event_on_Plugin()
def get_Event_from_SDK(self, event: Event):
self.data = event.copy()
self.base_info["time"] = event.time
self.base_info["self_id"] = event.self_id
self.base_info["type"] = event.post_type
self.base_info["sdk"] = "onebot"
self.base_info["platform"] = "qq"
self.base_info["model"] = "nonebot"
self.plugin_info["message_mode_rx"] = "old_string"
func_type_map = {
PrivateMessageEvent: "private_message",
GroupMessageEvent: "group_message",
GroupUploadNoticeEvent: "group_file_upload",
GroupAdminNoticeEvent: "group_admin",
GroupDecreaseNoticeEvent: "group_member_decrease",
GroupIncreaseNoticeEvent: "group_member_increase",
GroupBanNoticeEvent: "group_ban",
FriendAddNoticeEvent: "friend_add",
GroupRecallNoticeEvent: "group_message_recall",
FriendRecallNoticeEvent: "private_message_recall",
PokeNotifyEvent: "poke",
LuckyKingNotifyEvent: "group_lucky_king",
HonorNotifyEvent: "group_honor",
FriendRequestEvent: "friend_add_request",
GroupRequestEvent: None,
LifecycleMetaEvent: "lifecycle",
HeartbeatMetaEvent: "heartbeat",
}
if type(event) in func_type_map:
self.data.active = True
self.plugin_info["func_type"] = func_type_map[type(event)]
if isinstance(event, MessageEvent):
self.data.message = str(event.message)
self.data.sender = event.sender.dict()
self.data.extend = {}
# self.data.message_sdk = OlivOS.messageAPI.Message_templet(
# "old_string", str(event.message)
# )
# self.data.raw_message_sdk = OlivOS.messageAPI.Message_templet(
# "old_string", event.raw_message
# )
if isinstance(event, GroupMessageEvent):
self.data.host_id = None
if event.sub_type != "normal":
self.active = False
# TODO GuildEvent
# elif event.message_type == "guild":
# if event.sub_type == "channel":
# self.active = True
# self.plugin_info["func_type"] = "group_message"
# if "guild_id" in self.sdk_event.json:
# self.data.host_id = event.guild_id
# self.data.extend["host_group_id"] = self.sdk_event.json[
# "guild_id"
# ]
# if "self_tiny_id" in self.sdk_event.json:
# self.data.extend["sub_self_id"] = self.sdk_event.json[
# "self_tiny_id"
# ]
# if event.user_id == event.self_tiny_id:
# self.active = False
if event.raw_message == "":
self.active = False
elif isinstance(event, NoticeEvent):
if isinstance(event, GroupUploadNoticeEvent):
self.data.file = event.file.dict()
elif isinstance(
event,
(
GroupAdminNoticeEvent,
GroupDecreaseNoticeEvent,
GroupIncreaseNoticeEvent,
GroupBanNoticeEvent,
),
):
self.data.action = event.sub_type
elif isinstance(event, GroupBanNoticeEvent):
self.active = True
self.data.action = {"ban": "ban", "lift_ban": "unban"}[event.sub_type]
elif isinstance(event, NotifyEvent):
if isinstance(event, PokeNotifyEvent):
if not event.group_id:
self.data.group_id = -1
elif isinstance(event, HonorNotifyEvent):
self.active = True
self.data.type = event.honor_type
elif isinstance(event, (GroupRequestEvent, FriendRequestEvent)):
if not event.comment:
self.data.comment = ""
if isinstance(event, GroupRequestEvent):
if event.sub_type == "add":
self.plugin_info["func_type"] = "group_add_request"
elif event.sub_type == "invite":
self.plugin_info["func_type"] = "group_invite_request"
elif isinstance(event, MetaEvent):
if isinstance(event, LifecycleMetaEvent):
self.data.action = event.sub_type
def get_Event_on_Plugin(self):
if self.plugin_info["func_type"] in ["private_message", "group_message"]:
if self.data.message_sdk.mode_rx == self.plugin_info["message_mode_tx"]:
self.data.message = self.data.message_sdk.data_raw
else:
self.data.message = self.data.message_sdk.get(
self.plugin_info["message_mode_tx"]
)
if self.data.raw_message_sdk.mode_rx == self.plugin_info["message_mode_tx"]:
self.data.raw_message = self.data.raw_message_sdk.data_raw
else:
self.data.raw_message = self.data.raw_message_sdk.get(
self.plugin_info["message_mode_tx"]
)
def set_group_leave(self, group_id: int):
asyncio.create_task(
get_bots()[str(self.bot_info.id)].set_group_leave(group_id=group_id)
)
def set_friend_add_request(self, flag: str, approve: bool, remark: str):
asyncio.create_task(
get_bots()[str(self.bot_info.id)].set_friend_add_request(
flag=flag, approve=approve, remark=remark
)
)
def set_group_add_request(
self, flag: str, sub_type: str, approve: bool, remark: str
):
asyncio.create_task(
get_bots()[str(self.bot_info.id)].set_group_add_leave(
flag=flag, sub_type=sub_type, approve=approve, remark=remark
)
)
def reply(self, message: str):
if self.data:
asyncio.create_task(
get_bots()[str(self.bot_info.id)].send(self.data, message)
)
def sleep(self, delay: float):
asyncio.create_task(asyncio.sleep(delay))
class BotInfo:
def __init__(self, id: int, platform: dict) -> None:
self.id = id
self.platform = platform
self.hash = self.get_hash()
def get_hash(self):
hash_tmp = hashlib.new("md5")
hash_tmp.update(str(self.id).encode(encoding="UTF-8"))
hash_tmp.update(str(self.platform["sdk"]).encode(encoding="UTF-8"))
hash_tmp.update(str(self.platform["platform"]).encode(encoding="UTF-8"))
# hash_tmp.update(str(platform_model).encode(encoding='UTF-8'))
return hash_tmp.hexdigest()
class Proc:
def __init__(self) -> None:
self.Proc_data = {}
self.Proc_data["bot_info_dict"] = {}
for bot in get_bots().values():
if isinstance(bot, Bot):
bot_info = BotInfo(
int(bot.self_id),
{"sdk": "onebot", "platform": "qq", "model": "nonebot"},
)
self.Proc_data["bot_info_dict"].update({bot_info.hash: bot_info})
self.log = logger.info