nonebot-plugin-genshinuid 4.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,442 @@
1
+ import os
2
+ import re
3
+ import asyncio
4
+ from pathlib import Path
5
+ from base64 import b64encode
6
+ from typing import Any, List, Union, Optional
7
+
8
+ import aiofiles
9
+ from nonebot.log import logger
10
+ from nonebot.adapters import Bot
11
+ from nonebot.matcher import Matcher
12
+ from nonebot.permission import SUPERUSER
13
+ from nonebot.internal.adapter import Event
14
+ from nonebot import on_notice, get_driver, on_message, on_fullmatch
15
+
16
+ from .client import GsClient
17
+ from .auto_install import start, install
18
+ from .models import Message, MessageReceive
19
+
20
+ get_message = on_message(priority=999)
21
+ get_notice = on_notice(priority=999)
22
+ install_core = on_fullmatch('gs一键安装', permission=SUPERUSER, block=True)
23
+ start_core = on_fullmatch('启动core', permission=SUPERUSER, block=True)
24
+ connect_core = on_fullmatch(
25
+ ('连接core', '链接core'), permission=SUPERUSER, block=True
26
+ )
27
+ driver = get_driver()
28
+ gsclient: Optional[GsClient] = None
29
+
30
+ command_start = driver.config.command_start
31
+
32
+
33
+ @get_notice.handle()
34
+ async def get_notice_message(bot: Bot, ev: Event):
35
+ if gsclient is None or not gsclient.is_alive:
36
+ return await connect()
37
+
38
+ raw_data = ev.dict()
39
+ logger.debug(raw_data)
40
+
41
+ try:
42
+ user_id = str(ev.get_user_id())
43
+ except ValueError:
44
+ user_id = '未知'
45
+
46
+ group_id = None
47
+ sp_user_type = None
48
+ sp_bot_id = None
49
+ self_id = str(bot.self_id)
50
+ msg_id = ''
51
+ pm = 6
52
+
53
+ if await SUPERUSER(bot, ev):
54
+ pm = 1
55
+
56
+ if 'group_id' in raw_data:
57
+ group_id = str(raw_data['group_id'])
58
+
59
+ if 'user_id' in raw_data:
60
+ user_id = str(raw_data['user_id'])
61
+
62
+ if sp_bot_id:
63
+ bot_id = sp_bot_id
64
+ else:
65
+ bot_id = ev.__class__.__module__.split('.')[2]
66
+
67
+ user_type = 'group' if group_id else 'direct'
68
+
69
+ if 'notice_type' in raw_data and raw_data['notice_type'] in [
70
+ 'group_upload',
71
+ 'offline_file',
72
+ ]:
73
+ val = raw_data['file']['url']
74
+ name = raw_data['file']['name']
75
+ message = [Message('file', f'{name}|{val}')]
76
+ # onebot_v11
77
+ else:
78
+ return
79
+
80
+ msg = MessageReceive(
81
+ bot_id=bot_id,
82
+ bot_self_id=self_id,
83
+ user_type=sp_user_type if sp_user_type else user_type,
84
+ group_id=group_id,
85
+ user_id=user_id,
86
+ content=message,
87
+ msg_id=msg_id,
88
+ user_pm=pm,
89
+ )
90
+ logger.info(f'【发送】[gsuid-core]: {msg.bot_id}')
91
+ await gsclient._input(msg)
92
+
93
+
94
+ @get_message.handle()
95
+ async def get_all_message(bot: Bot, ev: Event):
96
+ if gsclient is None or not gsclient.is_alive:
97
+ return await connect()
98
+
99
+ # 通用字段获取
100
+ group_id = None
101
+ user_id = ev.get_user_id()
102
+ messages = ev.get_message()
103
+ logger.debug(ev)
104
+
105
+ self_id = str(bot.self_id)
106
+ message: List[Message] = []
107
+ sp_bot_id: Optional[str] = None
108
+
109
+ pm = 6
110
+ msg_id = ''
111
+
112
+ # qqguild
113
+ if bot.adapter.get_name() == 'QQ Guild':
114
+ from nonebot.adapters.qqguild.event import (
115
+ MessageEvent,
116
+ DirectMessageCreateEvent,
117
+ )
118
+
119
+ # 私聊
120
+ if isinstance(ev, DirectMessageCreateEvent):
121
+ user_type = 'direct'
122
+ group_id = str(ev.guild_id)
123
+ msg_id = ev.id
124
+ # 群聊
125
+ elif isinstance(ev, MessageEvent):
126
+ user_type = 'group'
127
+ group_id = str(ev.channel_id)
128
+ if ev.member and ev.member.roles:
129
+ if 4 in ev.member.roles:
130
+ pm = 2
131
+ elif 2 in ev.member.roles:
132
+ pm = 3
133
+ elif 5 in ev.member.roles:
134
+ pm = 5
135
+ msg_id = ev.id
136
+ else:
137
+ logger.debug('[gsuid] 不支持该 QQ Guild 事件...')
138
+ return
139
+ # telegram
140
+ elif bot.adapter.get_name() == 'Telegram':
141
+ from nonebot.adapters.telegram.event import (
142
+ GroupMessageEvent,
143
+ PrivateMessageEvent,
144
+ )
145
+
146
+ if isinstance(ev, GroupMessageEvent) or isinstance(
147
+ ev, PrivateMessageEvent
148
+ ):
149
+ if ev.from_.is_bot:
150
+ return
151
+
152
+ user_id = str(ev.from_.id)
153
+ msg_id = str(ev.message_id)
154
+ if isinstance(ev, GroupMessageEvent):
155
+ user_type = 'group'
156
+ group_id = str(ev.chat.id)
157
+ else:
158
+ user_type = 'direct'
159
+ else:
160
+ logger.debug('[gsuid] 不支持该 Telegram 事件...')
161
+ return
162
+ # kaiheila
163
+ elif bot.adapter.get_name() == 'Kaiheila':
164
+ from nonebot.adapters.kaiheila.event import (
165
+ ChannelMessageEvent,
166
+ PrivateMessageEvent,
167
+ )
168
+
169
+ if isinstance(ev, ChannelMessageEvent) or isinstance(
170
+ ev, PrivateMessageEvent
171
+ ):
172
+ if ev.event.author.bot:
173
+ return
174
+
175
+ user_id = ev.author_id
176
+ msg_id = ev.msg_id
177
+ if isinstance(ev, ChannelMessageEvent):
178
+ user_type = 'group'
179
+ group_id = ev.target_id
180
+ else:
181
+ user_type = 'direct'
182
+ else:
183
+ logger.debug('[gsuid] 不支持该 kaiheila 事件...')
184
+ return
185
+ # onebot
186
+ elif bot.adapter.get_name() == 'OneBot V11':
187
+ from nonebot.adapters.onebot.v11.event import (
188
+ GroupMessageEvent,
189
+ PrivateMessageEvent,
190
+ )
191
+
192
+ if isinstance(ev, GroupMessageEvent) or isinstance(
193
+ ev, PrivateMessageEvent
194
+ ):
195
+ messages = ev.original_message
196
+ msg_id = str(ev.message_id)
197
+ if ev.sender.role == 'owner':
198
+ pm = 2
199
+ elif ev.sender.role == 'admin':
200
+ pm = 3
201
+
202
+ if isinstance(ev, GroupMessageEvent):
203
+ user_type = 'group'
204
+ group_id = str(ev.group_id)
205
+ else:
206
+ user_type = 'direct'
207
+ else:
208
+ logger.debug('[gsuid] 不支持该 onebotv11 事件...')
209
+ return
210
+ elif bot.adapter.get_name() == 'Feishu':
211
+ from nonebot.adapters.feishu.event import (
212
+ GroupEventMessage,
213
+ PrivateEventMessage,
214
+ )
215
+
216
+ if isinstance(ev, GroupEventMessage) or isinstance(
217
+ ev, PrivateEventMessage
218
+ ):
219
+ for feishu_msg in messages:
220
+ if 'image_key' in feishu_msg.data:
221
+ feishu_msg.data['url'] = feishu_msg.data['image_key']
222
+ user_id = ev.get_user_id()
223
+ msg_id = ev.message_id
224
+ if isinstance(ev, GroupEventMessage):
225
+ user_type = 'group'
226
+ group_id = ev.chat_id
227
+ else:
228
+ user_type = 'direct'
229
+ else:
230
+ logger.debug('[gsuid] 不支持该 Feishu 事件...')
231
+ return
232
+ # ntchat
233
+ elif bot.adapter.get_name() == 'ntchat':
234
+ from nonebot.adapters.ntchat.event import (
235
+ FileMessageEvent,
236
+ TextMessageEvent,
237
+ )
238
+
239
+ if isinstance(ev, TextMessageEvent):
240
+ user_id = ev.from_wxid
241
+ msg_id = ev.msgid
242
+ if 'chatroom' in ev.to_wxid:
243
+ user_type = 'group'
244
+ group_id = ev.to_wxid
245
+ else:
246
+ user_type = 'direct'
247
+ if 'image' in ev.data:
248
+ message.append(Message('image', ev.data['image']))
249
+ if 'from_wxid' in ev.data:
250
+ if 'raw_msg' in ev.data and 'xml' in ev.data['raw_msg']:
251
+ match = re.search(
252
+ r'<svrid>(\d+)</svrid>', ev.data['raw_msg']
253
+ )
254
+ if match:
255
+ message.append(Message('reply', match.group(1)))
256
+ if ev.at_user_list:
257
+ at_list = [Message('at', i) for i in ev.at_user_list]
258
+ at_list.pop(0)
259
+ message.extend(at_list)
260
+ elif isinstance(ev, FileMessageEvent):
261
+ if 'chatroom' in ev.to_wxid:
262
+ group_id = ev.to_wxid
263
+ user_type = 'group'
264
+ else:
265
+ user_type = 'direct'
266
+ val = ev.file
267
+ name = ev.file_name
268
+ await asyncio.sleep(2)
269
+ if (
270
+ os.path.exists(val)
271
+ and os.path.getsize(val) <= 5 * 1024 * 1024
272
+ and str(name).endswith('.json')
273
+ ):
274
+ message.append(await convert_file(val, name))
275
+ else:
276
+ logger.debug('[gsuid] 不支持该 ntchat 事件...')
277
+ return
278
+ # OneBot V12 (仅在 ComWechatClient 测试)
279
+ elif bot.adapter.get_name() == 'OneBot V12':
280
+ from nonebot.adapters.onebot.v12.event import (
281
+ GroupMessageEvent,
282
+ PrivateMessageEvent,
283
+ )
284
+
285
+ # v12msgid = raw_data['id'] # V12的消息id
286
+ # self = raw_data['self'] # 返回 platform='xxx' user_id='wxid_xxxxx'
287
+ # platform = self.platform # 机器人平台
288
+ # V12还支持频道等其他平台,速速Pr!
289
+
290
+ if isinstance(ev, GroupMessageEvent) or isinstance(
291
+ ev, PrivateMessageEvent
292
+ ):
293
+ messages = ev.original_message
294
+ msg_id = ev.message_id
295
+ sp_bot_id = 'onebot_v12'
296
+
297
+ if '[文件]' in ev.alt_message:
298
+ file_id = messages[0].data.get('file_id')
299
+ logger.info('[OB12文件ID]', file_id)
300
+ if file_id and file_id in messages[0].data.values():
301
+ data = await get_file(bot, file_id)
302
+ logger.info('[OB12文件]', data)
303
+ name = data['name']
304
+ path = data['path']
305
+ message.append(await convert_file(path, name))
306
+
307
+ if isinstance(ev, GroupMessageEvent):
308
+ user_type = 'group'
309
+ group_id = ev.group_id
310
+ else:
311
+ user_type = 'direct'
312
+ else:
313
+ logger.debug('[gsuid] 不支持该 onebotv12 事件...')
314
+ return
315
+ else:
316
+ logger.debug(f'[gsuid] 不支持该 {bot.adapter.get_name()} 事件...')
317
+ return
318
+
319
+ if sp_bot_id:
320
+ bot_id = sp_bot_id
321
+ else:
322
+ bot_id = messages.__class__.__module__.split('.')[2]
323
+
324
+ # 确认超管权限
325
+ if await SUPERUSER(bot, ev):
326
+ pm = 1
327
+
328
+ # 如果有at提及,增加AT
329
+ if ev.is_tome():
330
+ message.append(Message('at', self_id))
331
+
332
+ # 处理消息
333
+ for index, _msg in enumerate(messages):
334
+ message = convert_message(_msg, message, index)
335
+
336
+ if not message:
337
+ return
338
+
339
+ msg = MessageReceive(
340
+ bot_id=bot_id,
341
+ bot_self_id=self_id,
342
+ user_type=user_type,
343
+ group_id=group_id,
344
+ user_id=user_id,
345
+ content=message,
346
+ msg_id=msg_id if msg_id else '',
347
+ user_pm=pm,
348
+ )
349
+ logger.info(f'【发送】[gsuid-core]: {msg.bot_id}')
350
+ await gsclient._input(msg)
351
+
352
+
353
+ @install_core.handle()
354
+ async def send_install_msg(matcher: Matcher):
355
+ await matcher.send('即将开始安装...会持续一段时间, 且期间无法使用Bot!')
356
+ await matcher.send(await install())
357
+
358
+
359
+ @connect_core.handle()
360
+ async def send_connect_msg(matcher: Matcher):
361
+ await connect()
362
+ await matcher.send('链接成功!')
363
+
364
+
365
+ @start_core.handle()
366
+ async def send_start_msg(matcher: Matcher):
367
+ await matcher.send(await start())
368
+
369
+
370
+ @driver.on_bot_connect
371
+ async def start_client():
372
+ if gsclient is None:
373
+ await connect()
374
+
375
+
376
+ async def connect():
377
+ global gsclient
378
+ try:
379
+ gsclient = await GsClient().async_connect()
380
+ await gsclient.start()
381
+ except ConnectionRefusedError:
382
+ logger.error('Core服务器连接失败...请稍后使用[启动core]命令启动...')
383
+
384
+
385
+ def convert_message(_msg: Any, message: List[Message], index: int):
386
+ if _msg.type == 'text':
387
+ data: str = (
388
+ _msg.data['text'] if 'text' in _msg.data else _msg.data['content']
389
+ )
390
+ if index == 0:
391
+ if data.startswith(tuple(command_start)):
392
+ for word in command_start:
393
+ data = data.replace(word, '', 1)
394
+ message.append(Message('text', data))
395
+ elif _msg.type == 'image':
396
+ file_id = _msg.data.get('file_id')
397
+ if file_id in _msg.data.values():
398
+ message.append(Message('image', _msg.data['file_id']))
399
+ logger.debug('[OB12图片]', _msg.data['file_id'])
400
+ else:
401
+ message.append(Message('image', _msg.data['url']))
402
+ elif _msg.type == 'at':
403
+ message.append(Message('at', _msg.data['qq']))
404
+ elif _msg.type == 'reply':
405
+ message_id = _msg.data.get('message_id')
406
+ if message_id in _msg.data.values():
407
+ message.append(Message('reply', _msg.data['message_id']))
408
+ else:
409
+ message.append(Message('reply', _msg.data['id']))
410
+ elif _msg.type == 'mention':
411
+ if 'user_id' in _msg.data:
412
+ message.append(Message('at', _msg.data['user_id']))
413
+ return message
414
+
415
+
416
+ # 读取文件为base64
417
+ async def convert_file(
418
+ content: Union[Path, str, bytes], file_name: str
419
+ ) -> Message:
420
+ if isinstance(content, Path):
421
+ print(content)
422
+ async with aiofiles.open(str(content), 'rb') as fp:
423
+ file = await fp.read()
424
+ elif isinstance(content, bytes):
425
+ file = content
426
+ else:
427
+ async with aiofiles.open(content, 'rb') as fp:
428
+ file = await fp.read()
429
+ return Message(
430
+ type='file',
431
+ data=f'{file_name}|{b64encode(file).decode()}',
432
+ )
433
+
434
+
435
+ # 获取文件
436
+ async def get_file(bot: Bot, file_id: str):
437
+ data = await bot.call_api(
438
+ api='get_file',
439
+ file_id=f'{file_id}',
440
+ type='path',
441
+ )
442
+ return data
@@ -0,0 +1,42 @@
1
+ import asyncio
2
+ import subprocess
3
+
4
+ from git.repo import Repo
5
+ from nonebot.log import logger
6
+ from pip._internal import main as pip_install
7
+
8
+ from .path import CORE_PATH, GSUID_PATH
9
+
10
+ GS_GIT = 'https://ghproxy.com/https://github.com/KimigaiiWuyi/GenshinUID.git'
11
+ CORE_GIT = 'https://ghproxy.com/https://github.com/Genshin-bots/gsuid_core.git'
12
+
13
+
14
+ async def _install():
15
+ if not CORE_PATH.exists():
16
+ Repo.clone_from(CORE_GIT, CORE_PATH, single_branch=True, depth=1)
17
+ if not GSUID_PATH.exists():
18
+ Repo.clone_from(
19
+ GS_GIT, GSUID_PATH, b='v4', single_branch=True, depth=1
20
+ )
21
+ try:
22
+ import poetry.__version__ as poetry_version
23
+
24
+ if float(poetry_version.__version__[:3]) < 1.4:
25
+ pip_install(['install', 'poetry'])
26
+ except ImportError:
27
+ pip_install(['install', 'poetry'])
28
+ subprocess.call(['poetry', 'install'], cwd=f'{CORE_PATH}')
29
+
30
+
31
+ async def install():
32
+ done = await asyncio.gather(_install())
33
+ logger.info(done)
34
+ return '安装成功...'
35
+
36
+
37
+ async def start():
38
+ subprocess.Popen(
39
+ ['poetry', 'run', 'python', 'gsuid_core/core.py'],
40
+ cwd=f'{CORE_PATH}',
41
+ )
42
+ return '启动成功完成...'