-
Notifications
You must be signed in to change notification settings - Fork 5
/
__init__.py
296 lines (268 loc) · 11.1 KB
/
__init__.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import asyncio
import re
import sys
import traceback
from collections import defaultdict, namedtuple
import hoshino
from hikari_core import callback_hikari, init_hikari, set_hikari_config
from hikari_core.game.help import check_version
from hikari_core.model import Hikari_Model
from hikari_core.moudle.wws_real_game import get_diff_ship
from hoshino import Service, get_bot, priv
from hoshino.typing import CQEvent, MessageSegment, NoticeSession
from hoshino.util import DailyNumberLimiter, FreqLimiter
from loguru import logger
from nonebot.exceptions import CQHttpError
from .data_source import config, dir_path
from .game.ocr import downlod_OcrResult, get_Random_Ocr_Pic, pic2txt_byOCR, upload_OcrResult
from .game.minimap_renderer import get_file, get_rep
from .game.pupu import get_pupu_msg
from .utils import bytes2b64
_max = 100
EXCEED_NOTICE = f'您今天已经冲过{_max}次了,请明早5点后再来!'
_nlmt = DailyNumberLimiter(_max)
_flmt = FreqLimiter(3)
WWS_help = """请发送wws help查看帮助"""
sv_help = WWS_help.strip()
sv = Service('wows-stats-bot', manage_priv=priv.ADMIN, enable_on_default=True, help_=sv_help)
set_hikari_config(
use_broswer=config['browser'],
http2=config['http2'],
proxy=config['proxy'],
token=config['token'],
game_path=str(dir_path / 'game'),
)
SlectState = namedtuple('SlectState', ['state', 'SlectIndex', 'SelectList'])
SecletProcess = defaultdict(lambda: SlectState(False, None, None))
@sv.on_prefix(('wws'))
async def main(bot, ev: CQEvent):
try:
qqid = ev['user_id']
group_id = None
if ev['message_type'] == 'group':
group_id = ev['group_id']
if not _nlmt.check(qqid):
await bot.send(ev, EXCEED_NOTICE, at_sender=True)
return
if not _flmt.check(qqid):
await bot.send(ev, '您冲得太快了,请稍候再冲', at_sender=True)
return
_flmt.start_cd(qqid)
_nlmt.increase(qqid)
superuser_command_list = ['重置监控']
adminuser_command_list = ['添加监控', '删除监控']
for each in superuser_command_list:
if each in str(ev.message) and int(qqid) not in hoshino.config.SUPERUSERS:
await bot.send(ev, '该命令仅限超级管理员使用')
return
for each in adminuser_command_list:
if each in str(ev.message) and int(qqid) not in config['listen_admin']:
await bot.send(ev, '请联系机器人搭建者添加权限')
return
hikari = await init_hikari(platform='QQ', PlatformId=str(ev['user_id']), command_text=str(ev.message), GroupId=group_id)
if hikari.Status == 'success':
if isinstance(hikari.Output.Data, bytes):
await bot.send(ev, str(MessageSegment.image(bytes2b64(hikari.Output.Data))))
elif isinstance(hikari.Output.Data, str):
await bot.send(ev, str(hikari.Output.Data))
elif hikari.Status == 'wait':
await bot.send(ev, str(MessageSegment.image(bytes2b64(hikari.Output.Data))))
hikari = await wait_to_select(hikari)
if hikari.Status == 'error':
await bot.send(ev, str(hikari.Output.Data))
return
hikari = await callback_hikari(hikari)
if isinstance(hikari.Output.Data, bytes):
await bot.send(ev, str(MessageSegment.image(bytes2b64(hikari.Output.Data))))
elif isinstance(hikari.Output.Data, str):
await bot.send(ev, str(hikari.Output.Data))
else:
await bot.send(ev, str(hikari.Output.Data))
except CQHttpError:
logger.error(traceback.format_exc())
try:
await bot.send(ev, '发不出图片,可能被风控了QAQ')
except Exception:
pass
return
except Exception:
logger.error(traceback.format_exc())
await bot.send(ev, '呜呜呜发生了错误,可能是网络问题,如果过段时间不能恢复请联系麻麻哦~')
return
@sv.on_message()
async def change_select_state(bot, ev):
try:
msg = ev['raw_message']
qqid = str(ev['user_id'])
if SecletProcess[qqid].state and str(msg).isdigit():
if int(msg) <= len(SecletProcess[qqid].SelectList):
SecletProcess[qqid] = SecletProcess[qqid]._replace(state=False)
SecletProcess[qqid] = SecletProcess[qqid]._replace(SlectIndex=int(msg))
else:
await bot.send(ev, '请选择列表中的序号哦~')
return
except Exception:
logger.error(traceback.format_exc())
return
async def wait_to_select(hikari):
SecletProcess[hikari.UserInfo.PlatformId] = SlectState(True, None, hikari.Input.Select_Data)
a = 0
while a < 40 and not SecletProcess[hikari.UserInfo.PlatformId].SlectIndex:
a += 1
await asyncio.sleep(0.5)
if SecletProcess[hikari.UserInfo.PlatformId].SlectIndex:
hikari.Input.Select_Index = SecletProcess[hikari.UserInfo.PlatformId].SlectIndex
SecletProcess[hikari.UserInfo.PlatformId] = SlectState(False, None, None)
return hikari
else:
SecletProcess[hikari.UserInfo.PlatformId] = SlectState(False, None, None)
return hikari.error('已超时退出')
@sv.scheduled_job('cron', hour='12')
async def job1():
bot = get_bot()
hikari = Hikari_Model()
hikari = await check_version(hikari)
superid = hoshino.config.SUPERUSERS[0]
await bot.send_private_msg(user_id=superid, message=hikari.Output.Data)
@sv.scheduled_job('interval', minutes=10)
async def job2():
await downlod_OcrResult()
@sv.scheduled_job('interval', minutes=1)
async def job3():
bot = get_bot()
hikari = Hikari_Model()
hikari = await get_diff_ship(hikari)
if hikari.Status == 'success':
for _each in hikari.Output.Data:
await bot.send_group_msg(group_id=_each['group_id'], message=_each['msg'])
logger.add(
str(dir_path / 'logs/error.log'),
rotation='00:00',
retention='1 week',
diagnose=False,
level='ERROR',
encoding='utf-8',
)
logger.add(
str(dir_path / 'logs/info.log'),
rotation='00:00',
retention='1 week',
diagnose=False,
level='INFO',
encoding='utf-8',
)
logger.add(
str(dir_path / 'logs/warning.log'),
rotation='00:00',
retention='1 week',
diagnose=False,
level='WARNING',
encoding='utf-8',
)
@sv.on_fullmatch('噗噗')
async def send_pupu_msg(bot, ev: CQEvent):
try:
if config['pupu']:
msg = await get_pupu_msg()
await bot.send(ev, msg)
except CQHttpError:
logger.error(traceback.format_exc())
try:
await bot.send(ev, '噗噗寄了>_<可能被风控了QAQ')
except Exception:
pass
return
except Exception:
logger.error(traceback.format_exc())
await bot.send(ev, '呜呜呜发生了错误,可能是网络问题,如果过段时间不能恢复请联系麻麻哦~')
return
@sv.on_fullmatch('wws 随机表情包')
async def send_random_ocr_image(bot, ev: CQEvent):
try:
img = await get_Random_Ocr_Pic()
if isinstance(img, bytes):
await bot.send(ev, str(MessageSegment.image(bytes2b64(img))))
elif isinstance(img, str):
await bot.send(ev, str(img))
except Exception:
logger.error(traceback.format_exc())
await bot.send(ev, '呜呜呜发生了错误,可能是网络问题,如果过段时间不能恢复请联系麻麻哦~')
return
@sv.on_message()
async def OCR_listen(bot, ev: CQEvent):
try:
if not config['ocr_on']:
return
if not (str(ev.message).find('[CQ:image') + 1): # 判断收到的信息是否为图片,不是就退出
return
for seg in ev.message:
if seg.type == 'image':
tencent_url = seg.data['url']
filename = str(seg.data['file']).replace('.image', '')
ocr_text = await pic2txt_byOCR(tencent_url, filename)
if ocr_text:
match = re.search(r'^(/?)wws(.*?)$', ocr_text)
if match:
qqid = ev['user_id']
if not _nlmt.check(qqid):
await bot.send(ev, EXCEED_NOTICE, at_sender=True)
return
if not _flmt.check(qqid):
await bot.send(ev, '您冲得太快了,请稍候再冲', at_sender=True)
return
_flmt.start_cd(qqid)
_nlmt.increase(qqid)
searchtag = re.sub(r'^(/?)wws', '', ocr_text) # 删除wws和/wws
hikari = await init_hikari(platform='QQ', PlatformId=str(ev['user_id']), command_text=str(searchtag))
if hikari.Status == 'success':
if isinstance(hikari.Output.Data, bytes):
await bot.send(ev, str(MessageSegment.image(bytes2b64(hikari.Output.Data))))
elif isinstance(hikari.Output.Data, str):
await bot.send(ev, str(hikari.Output.Data))
elif hikari.Status == 'wait':
await bot.send(ev, str(MessageSegment.image(bytes2b64(hikari.Output.Data))))
hikari = await wait_to_select(hikari)
if hikari.Status == 'error':
await bot.send(ev, str(hikari.Output.Data))
return
hikari = await callback_hikari(hikari)
if isinstance(hikari.Output.Data, bytes):
await bot.send(ev, str(MessageSegment.image(bytes2b64(hikari.Output.Data))))
elif isinstance(hikari.Output.Data, str):
await bot.send(ev, str(hikari.Output.Data))
else:
await bot.send(ev, str(hikari.Output.Data))
await upload_OcrResult(ocr_text, filename)
else:
return
except CQHttpError:
logger.error(traceback.format_exc())
try:
await bot.send(ev, '发不出图片,可能被风控了QAQ')
except Exception:
pass
except Exception:
logger.error(traceback.format_exc())
return
@sv.on_notice('group_upload')
async def handle_group_file_upload(session: NoticeSession):
try:
if not config['minimap_renderer_on']:
return
upload_file = session.ctx.get('file')
upload_file_id = upload_file.get('id')
# 判断文件是否是需要处理的类型
if not upload_file.get('name').endswith('.wowsreplay'):
return
if 'url' in upload_file:
base64_file = await get_file(upload_file.get('url'))
await get_rep(base64_file, session)
else:
base64_file = await session.bot.get_file(file_id=upload_file_id)
if 'base64' not in base64_file:
await get_rep(base64_file['url'], session)
else:
await get_rep(base64_file['base64'], session)
except Exception:
logger.error(traceback.format_exc())
await session.send(MessageSegment.text('请求minimap_renderer服务异常'))