1
1
import os
2
2
import re
3
- from typing import List , Dict , Optional , Union
3
+ from typing import List , Dict , Union
4
4
5
+ from creart import create
5
6
from graia .saya import Saya , Channel
6
7
from graia .ariadne .app import Ariadne
7
8
from graia .broadcast .interrupt .waiter import Waiter
11
12
from graia .saya .builtins .broadcast .schema import ListenerSchema
12
13
from graia .ariadne .event .message import Group , Member , GroupMessage
13
14
14
- from .utils import saya_data
15
+ from .utils import saya_data , uninstallable , reloadable
15
16
from sagiri_bot .internal_utils import MessageChainUtils
16
17
from sagiri_bot .internal_utils import user_permission_require
17
18
18
- saya = Saya . current ( )
19
+ saya = create ( Saya )
19
20
channel = Channel .current ()
20
21
21
22
channel .name ("SayaManager" )
30
31
inc = InterruptControl (saya .broadcast )
31
32
32
33
33
- def get_loaded_channels () -> Dict [str , Channel ]:
34
- return saya .channels
35
-
36
-
37
34
def get_all_channels () -> List [str ]:
38
35
ignore = ["__init__.py" , "__pycache__" ]
39
36
dirs = [
@@ -54,13 +51,9 @@ def get_all_channels() -> List[str]:
54
51
55
52
56
53
def get_unloaded_channels () -> List [str ]:
57
- loaded_channels = get_loaded_channels ()
54
+ loaded_channels = saya . channels
58
55
all_channels = get_all_channels ()
59
- return [channel for channel in all_channels if channel not in loaded_channels ]
60
-
61
-
62
- def get_channel (name : str ) -> Optional [Channel ]:
63
- return get_loaded_channels ().get (name )
56
+ return [c for c in all_channels if c not in loaded_channels ]
64
57
65
58
66
59
def load_channel (modules : Union [str , List [str ]]) -> Dict [str , Exception ]:
@@ -83,37 +76,37 @@ def unload_channel(modules: Union[str, List[str]]) -> Dict[str, Exception]:
83
76
exceptions = {}
84
77
if isinstance (modules , str ):
85
78
modules = [modules ]
86
- loaded_channels = get_loaded_channels ()
79
+ loaded_channels = saya . channels
87
80
channels_to_unload = {
88
81
module : loaded_channels [module ]
89
82
for module in modules
90
83
if module in loaded_channels
91
84
}
92
85
with saya .module_context ():
93
- for channel , value in channels_to_unload .items ():
86
+ for c , value in channels_to_unload .items ():
94
87
try :
95
88
saya .uninstall_channel (value )
96
89
except Exception as e :
97
- exceptions [channel ] = e
90
+ exceptions [c ] = e
98
91
return exceptions
99
92
100
93
101
94
def reload_channel (modules : Union [str , List [str ]]) -> Dict [str , Exception ]:
102
95
exceptions = {}
103
96
if isinstance (modules , str ):
104
97
modules = [modules ]
105
- loaded_channels = get_loaded_channels ()
98
+ loaded_channels = saya . channels
106
99
channels_to_reload = {
107
100
module : loaded_channels [module ]
108
101
for module in modules
109
102
if module in loaded_channels
110
103
}
111
104
with saya .module_context ():
112
- for channel , value in channels_to_reload .items ():
105
+ for c , value in channels_to_reload .items ():
113
106
try :
114
107
saya .reload_channel (value )
115
108
except Exception as e :
116
- exceptions [channel ] = e
109
+ exceptions [c ] = e
117
110
return exceptions
118
111
119
112
@@ -122,7 +115,7 @@ async def saya_manager(
122
115
app : Ariadne , message : MessageChain , group : Group , member : Member , source : Source
123
116
):
124
117
if message .display .strip () == "已加载插件" :
125
- loaded_channels = get_loaded_channels ()
118
+ loaded_channels = saya . channels
126
119
keys = list (loaded_channels .keys ())
127
120
keys .sort ()
128
121
return await app .send_group_message (
@@ -143,20 +136,20 @@ async def saya_manager(
143
136
)
144
137
elif re .match (r"插件详情 .+" , message .display ):
145
138
target = message .display [5 :].strip ()
146
- loaded_channels = get_loaded_channels ()
139
+ loaded_channels = saya . channels
147
140
keys = list (loaded_channels .keys ())
148
141
if target .isdigit ():
149
142
keys .sort ()
150
143
if not 0 <= int (target ) - 1 < len (keys ):
151
144
return await app .send_group_message (
152
145
group , MessageChain ("错误的编号!请检查后再发送!" ), quote = source
153
146
)
154
- channel = loaded_channels [keys [int (target ) - 1 ]]
147
+ c = loaded_channels [keys [int (target ) - 1 ]]
155
148
channel_path = keys [int (target ) - 1 ]
156
149
else :
157
150
for lchannel in loaded_channels .keys ():
158
- if loaded_channels [lchannel ]._name == target :
159
- channel = loaded_channels [lchannel ]
151
+ if loaded_channels [lchannel ].meta [ "name" ] == target :
152
+ c = loaded_channels [lchannel ]
160
153
channel_path = lchannel
161
154
break
162
155
else :
@@ -167,9 +160,9 @@ async def saya_manager(
167
160
group ,
168
161
MessageChain (
169
162
[
170
- Plain (text = f"插件名称:{ channel . _name } \n " ),
171
- Plain (text = f"插件作者:{ '、' .join (channel . _author )} \n " ),
172
- Plain (text = f"插件描述:{ channel . _description } \n " ),
163
+ Plain (text = f"插件名称:{ c . meta [ 'name' ] } \n " ),
164
+ Plain (text = f"插件作者:{ '、' .join (c . meta [ 'author' ] )} \n " ),
165
+ Plain (text = f"插件描述:{ c . meta [ 'description' ] } \n " ),
173
166
Plain (text = f"插件包名:{ channel_path } " ),
174
167
]
175
168
),
@@ -207,18 +200,18 @@ async def saya_manager(
207
200
return await app .send_group_message (
208
201
group , MessageChain ("错误的编号!请检查后再发送!" ), quote = source
209
202
)
210
- channel = unloaded_channels [int (target ) - 1 ]
203
+ c = unloaded_channels [int (target ) - 1 ]
211
204
else :
212
205
for ulchannel in unloaded_channels :
213
206
if ulchannel == target :
214
- channel = ulchannel
207
+ c = ulchannel
215
208
break
216
209
else :
217
210
return await app .send_group_message (
218
211
group , MessageChain ("错误的名称!请检查后再发送!" ), quote = source
219
212
)
220
213
221
- await app .send_message (group , MessageChain (f"你确定要加载插件 `{ channel } ` 吗?(是/否)" ))
214
+ await app .send_message (group , MessageChain (f"你确定要加载插件 `{ c } ` 吗?(是/否)" ))
222
215
223
216
@Waiter .create_using_function ([GroupMessage ])
224
217
def confirm_waiter (
@@ -236,10 +229,10 @@ def confirm_waiter(
236
229
group , MessageChain ("非预期回复,进程退出" ), quote = source
237
230
)
238
231
elif result == "是" :
239
- result = load_channel (channel )
232
+ result = load_channel (c )
240
233
if result :
241
234
return await app .send_group_message (
242
- group , MessageChain (f"发生错误:{ result [channel ]} " ), quote = source
235
+ group , MessageChain (f"发生错误:{ result [c ]} " ), quote = source
243
236
)
244
237
else :
245
238
return await app .send_group_message (
@@ -256,30 +249,37 @@ def confirm_waiter(
256
249
)
257
250
load_type = "reload" if message .display [0 ] == "重" else "unload"
258
251
target = message .display [5 :].strip ()
259
- loaded_channels = get_loaded_channels ()
252
+ loaded_channels = saya . channels
260
253
keys = list (loaded_channels .keys ())
261
254
keys .sort ()
262
255
if target .isdigit ():
263
256
if not 0 <= int (target ) - 1 < len (keys ):
264
257
return await app .send_group_message (
265
258
group , MessageChain ("错误的编号!请检查后再发送!" ), quote = source
266
259
)
267
- channel = loaded_channels [keys [int (target ) - 1 ]]
260
+ c = loaded_channels [keys [int (target ) - 1 ]]
268
261
channel_path = keys [int (target ) - 1 ]
269
262
else :
270
263
for lchannel in loaded_channels .keys ():
271
- if loaded_channels [lchannel ]._name == target :
272
- channel = loaded_channels [lchannel ]
264
+ if loaded_channels [lchannel ].meta [ "name" ] == target :
265
+ c = loaded_channels [lchannel ]
273
266
channel_path = lchannel
274
267
break
275
268
else :
276
269
return await app .send_group_message (
277
270
group , MessageChain ("错误的名称!请检查后再发送!" ), quote = source
278
271
)
279
-
272
+ if load_type == "reload" and not reloadable (c .module ):
273
+ return await app .send_group_message (
274
+ group , MessageChain (f"插件 `{ c .meta ['name' ]} ` 不可重载!" ), quote = source
275
+ )
276
+ if load_type == "unload" and not uninstallable (c .module ):
277
+ return await app .send_group_message (
278
+ group , MessageChain (f"插件 `{ c .meta ['name' ]} ` 不可卸载!" ), quote = source
279
+ )
280
280
await app .send_message (
281
281
group ,
282
- MessageChain (f"你确定要{ message .display [0 ]} 载插件 `{ channel . _name } ` 吗?(是/否)" ),
282
+ MessageChain (f"你确定要{ message .display [0 ]} 载插件 `{ c . meta [ 'name' ] } ` 吗?(是/否)" ),
283
283
)
284
284
285
285
@Waiter .create_using_function ([GroupMessage ])
@@ -322,7 +322,7 @@ def confirm_waiter(
322
322
)
323
323
switch_type = "on" if message .display [:2 ] == "打开" else "off"
324
324
target = message .display [5 :].strip ()
325
- loaded_channels = get_loaded_channels ()
325
+ loaded_channels = saya . channels
326
326
keys = list (loaded_channels .keys ())
327
327
keys .sort ()
328
328
channel_path = ""
@@ -334,7 +334,7 @@ def confirm_waiter(
334
334
channel_path = keys [int (target ) - 1 ]
335
335
else :
336
336
for lchannel in loaded_channels .keys ():
337
- if loaded_channels [lchannel ]._name == target :
337
+ if loaded_channels [lchannel ].meta [ "name" ] == target :
338
338
channel_path = lchannel
339
339
break
340
340
saya_data .switch_on (
0 commit comments