-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
218 lines (183 loc) · 6.61 KB
/
main.py
File metadata and controls
218 lines (183 loc) · 6.61 KB
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
# script/example/main.py
import logging
import os
import sys
import re
import json
# 添加项目根目录到sys.path
sys.path.append(
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
)
from app.config import *
from app.api import send_group_msg, send_private_msg
from app.switch import load_switch, save_switch
# 数据存储路径,实际开发时,请将Example替换为具体的数据存放路径
DATA_DIR = os.path.join(
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
"data",
"Example",
)
# 查看功能开关状态
def load_function_status(group_id):
return load_switch(group_id, "Example")
# 保存功能开关状态
def save_function_status(group_id, status):
save_switch(group_id, "Example", status)
# 处理开关状态
async def toggle_function_status(websocket, group_id, message_id, authorized):
if not authorized:
await send_group_msg(
websocket,
group_id,
f"[CQ:reply,id={message_id}]❌❌❌你没有权限对Example功能进行操作,请联系管理员。",
)
return
if load_function_status(group_id):
save_function_status(group_id, False)
await send_group_msg(
websocket,
group_id,
f"[CQ:reply,id={message_id}]🚫🚫🚫Example功能已关闭",
)
else:
save_function_status(group_id, True)
await send_group_msg(
websocket, group_id, f"[CQ:reply,id={message_id}]✅✅✅Example功能已开启"
)
# 群消息处理函数
async def handle_group_message(websocket, msg):
"""处理群消息"""
# 确保数据目录存在
os.makedirs(DATA_DIR, exist_ok=True)
try:
user_id = str(msg.get("user_id"))
group_id = str(msg.get("group_id"))
raw_message = str(msg.get("raw_message"))
message_id = str(msg.get("message_id"))
authorized = user_id in owner_id
# 处理开关命令
if raw_message.lower() == "example":
await toggle_function_status(websocket, group_id, message_id, authorized)
return
# 检查功能是否开启
if load_function_status(group_id):
# 其他群消息处理逻辑
pass
except Exception as e:
logging.error(f"处理Example群消息失败: {e}")
await send_group_msg(
websocket,
group_id,
"处理Example群消息失败,错误信息:" + str(e),
)
return
# 私聊消息处理函数
async def handle_private_message(websocket, msg):
"""处理私聊消息"""
os.makedirs(DATA_DIR, exist_ok=True)
try:
user_id = str(msg.get("user_id"))
raw_message = str(msg.get("raw_message"))
# 私聊消息处理逻辑
pass
except Exception as e:
logging.error(f"处理Example私聊消息失败: {e}")
await send_private_msg(
websocket,
msg.get("user_id"),
"处理Example私聊消息失败,错误信息:" + str(e),
)
return
# 群通知处理函数
async def handle_group_notice(websocket, msg):
"""处理群通知"""
# 确保数据目录存在
os.makedirs(DATA_DIR, exist_ok=True)
try:
user_id = str(msg.get("user_id"))
group_id = str(msg.get("group_id"))
notice_type = str(msg.get("notice_type"))
operator_id = str(msg.get("operator_id", ""))
except Exception as e:
logging.error(f"处理Example群通知失败: {e}")
await send_group_msg(
websocket,
group_id,
"处理Example群通知失败,错误信息:" + str(e),
)
return
# 回应事件处理函数
async def handle_response(websocket, msg):
"""处理回调事件"""
try:
echo = msg.get("echo")
if echo and echo.startswith("xxx"):
# 回调处理逻辑
pass
except Exception as e:
logging.error(f"处理Example回调事件失败: {e}")
await send_group_msg(
websocket,
msg.get("group_id"),
f"处理Example回调事件失败,错误信息:{str(e)}",
)
return
# 请求事件处理函数
async def handle_request_event(websocket, msg):
"""处理请求事件"""
try:
request_type = msg.get("request_type")
pass
except Exception as e:
logging.error(f"处理Example请求事件失败: {e}")
return
# 统一事件处理入口
async def handle_events(websocket, msg):
"""统一事件处理入口"""
post_type = msg.get("post_type", "response") # 添加默认值
try:
# 这里可以放一些定时任务,在函数内设置时间差检测即可
# 处理回调事件,用于一些需要获取ws返回内容的事件
if msg.get("status") == "ok":
await handle_response(websocket, msg)
return
post_type = msg.get("post_type")
# 处理元事件,每次心跳时触发,用于一些定时任务
if post_type == "meta_event":
pass
# 处理消息事件,用于处理群消息和私聊消息
elif post_type == "message":
message_type = msg.get("message_type")
if message_type == "group":
await handle_group_message(websocket, msg)
elif message_type == "private":
await handle_private_message(websocket, msg)
# 处理通知事件,用于处理群通知
elif post_type == "notice":
await handle_group_notice(websocket, msg)
# 处理请求事件,用于处理请求事件
elif post_type == "request":
await handle_request_event(websocket, msg)
except Exception as e:
error_type = {
"message": "消息",
"notice": "通知",
"request": "请求",
"meta_event": "元事件",
}.get(post_type, "未知")
logging.error(f"处理Example{error_type}事件失败: {e}")
# 发送错误提示
if post_type == "message":
message_type = msg.get("message_type")
if message_type == "group":
await send_group_msg(
websocket,
msg.get("group_id"),
f"处理Example{error_type}事件失败,错误信息:{str(e)}",
)
elif message_type == "private":
await send_private_msg(
websocket,
msg.get("user_id"),
f"处理Example{error_type}事件失败,错误信息:{str(e)}",
)