-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
1003 lines (956 loc) · 42.5 KB
/
main.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import time
from copy import deepcopy
from json import load, dump
from nonebot import get_bot, on_command
from os.path import dirname, join, exists
from asyncio import Lock, gather
from .query import queue
from .pcrclient import ApiException
from .safeservice import SafeService
from .text2img import image_draw
from .create_img import generate_info_pic, generate_support_pic
from hoshino import priv,get_self_ids
from hoshino.util import pic2b64
from hoshino.config import SUPERUSERS
from nonebot.log import logger
from hoshino.typing import NoticeSession, MessageSegment
#from .jjchistory import *
sv_help='''\t\t\t\t【竞技场帮助】
可以添加的订阅:[jjc][pjjc][排名上升][上线提醒]
#排名上升提醒对jjc和pjjc同时生效
#每个QQ号至多添加8个uid的订阅
#默认开启jjc、pjjc,关闭排名上升、上线提醒
#手动查询时,返回昵称、jjc/pjjc排名、场次、
jjc/pjjc当天排名上升次数、最后登录时间。
#支持群聊使用。只允许群聊使用!!!
------------------------------------------------
命令格式:
#只绑定1个uid时,绑定的序号可以不填。
[绑定的序号]1~8对应绑定的第1~8个uid,序号0表示全部
1)竞技场绑定[uid][昵称](昵称可省略)
2)删除竞技场绑定[绑定的序号](这里序号不可省略)
3)开启/关闭竞技场推送(不会删除绑定)
4)清空竞技场绑定
5)竞技场查询[uid](uid可省略)
6)竞技场订阅状态
7)竞技场修改昵称 [绑定的序号] [新昵称]
8)竞技场设置[开启/关闭][订阅内容][绑定的序号]
9)竞技场/击剑记录[绑定的序号](序号可省略)
10)竞技场设置1110[绑定的序号]
#0表示关闭,1表示开启
#4个数字依次代表jjc、pjjc、排名上升、上线提醒
#例如:“竞技场设置1011 2” “竞技场设置1110 0”
#上线提醒:第4位表示上线提醒等级,可以写0~3
0表示关闭,1表示10分钟cd,仅在2点半~3点报,
2表示10分钟cd,全天报;3表示1分钟cd全天报。
每天5点会把上线提醒等级3改成2,有需要的可以再次手动开启。
11)在本群推送(限群聊发送,无需好友)
'''
sv_help_adm='''------------------------------------------------
管理员帮助:
1)pcrjjc负载查询
2)pcrjjc删除绑定[qq号]
3)pcrjjc关闭私聊推送
'''
sv = SafeService('竞技场推送',help_=sv_help, bundle='pcr查询')
# 数据库对象初始化
#JJCH = JJCHistoryStorage()
MAX_PRI = 0 #最大私聊人数
friendList = []
pcrid_list = []
config = join(dirname(__file__), 'binds.json')
root = {'arena_bind': {}}
if exists(config):
with open(config) as fp:
root = load(fp)
lck = Lock()
lck_friendList = Lock()
bind_cache = root['arena_bind']
cache = {}
jjc_log = {}
query_cache = {}
today_notice = 0
yesterday_notice = 0
@sv.on_fullmatch('竞技场帮助', only_to_me=False)
async def send_jjchelp(bot, ev):
if not priv.check_priv(ev, priv.SUPERUSER):
pic = image_draw(sv_help)
else:
pic = image_draw(sv_help+sv_help_adm)
await bot.send(ev,f'[CQ:image,file={pic}]')
#========================================查询========================================
@sv.on_fullmatch('查询群数', only_to_me=False)
async def group_num(bot, ev):
global bind_cache, lck
gid = int(ev['group_id'])
async with lck:
for sid in get_self_ids():
gl = await bot.get_group_list(self_id=sid)
gl = [g['group_id'] for g in gl]
try:
await bot.send_group_msg(
self_id=sid,
group_id=gid,
message=f"本Bot目前正在为【{len(gl)}】个群服务"
)
except Exception as e:
sv.logger.info(f'bot账号{sid}不在群{gid}中,将忽略该消息')
@sv.on_fullmatch('查询竞技场订阅数', only_to_me=False)
async def pcrjjc_number(bot, ev):
global bind_cache, lck
async with lck:
await bot.send(ev, f'当前竞技场已订阅的账号数量为【{len(bind_cache)}】个')
@sv.on_rex(r'^竞技场查询 ?(\d+)?$')
async def on_query_arena(bot, ev):
global bind_cache, lck
ret = ev['match']
qid = str(ev.user_id)
try:
pcrid = int(ret.group(1))
if(len(ret.group(1))) != 13:
await bot.send(ev, '位数不对,uid是13位的!')
return
else:
manual_query_list = [pcrid] #手动查询的列表
manual_query_list_name = [None]
except:
if qid in bind_cache:
manual_query_list = bind_cache[qid]["pcrid"]
manual_query_list_name = bind_cache[qid]["pcrName"]
else:
await bot.send(ev, '木有找到绑定信息,查询时不能省略13位uid!')
return
for i in range(len(manual_query_list)):
query_cache[ev.user_id] = []
pcrid = manual_query_list[i]
await queue.put((3,(resolve2,pcrid,{"bot":bot,"ev":ev,"list":manual_query_list_name,"index":i,"uid":pcrid})))
@sv.on_fullmatch('竞技场订阅状态')
async def send_arena_sub_status(bot,ev):
global bind_cache
qid = str(ev['user_id'])
gid = ev.group_id
member_info = await bot.get_group_member_info(group_id=gid,user_id=qid)
name = member_info["card"] or member_info["nickname"]
if qid in bind_cache:
private = '私聊推送' if bind_cache[qid]["private"] else '群聊推送'
notice_on = '推送已开启' if bind_cache[qid]["notice_on"] else '推送未开启'
reply = f'{name}({qid})的竞技场订阅列表:\n\n'
reply += f'群号:{bind_cache[qid]["gid"]}\n'
reply += f'''推送方式:{private}\n状态:{notice_on}\n'''
for pcrid_id in range(len(bind_cache[qid]["pcrid"])):
reply += f'\n【{pcrid_id+1}】{bind_cache[qid]["pcrName"][pcrid_id]}({bind_cache[qid]["pcrid"][pcrid_id]})\n'
tmp = bind_cache[qid]["noticeType"][pcrid_id]
jjcNotice = True if tmp//1000 else False
pjjcNotice = True if (tmp%1000)//100 else False
riseNotice = True if (tmp%100)//10 else False
onlineNotice = tmp%10
noticeType = '推送内容:'
if jjcNotice:
noticeType += 'jjc、'
if pjjcNotice:
noticeType += 'pjjc、'
if riseNotice:
noticeType += '排名上升、'
if onlineNotice:
noticeType += '上线提醒LV'+str(onlineNotice)+'、'
if noticeType == '推送内容:':
noticeType += '无'
else:
noticeType = noticeType.strip('、')
reply += noticeType
reply += '\n'
reply += '###上线提醒LV越高,提醒越频繁。详情见竞技场帮助\n'
pic = image_draw(reply)
await bot.send(ev,f'[CQ:image,file={pic}]')
else:
await bot.send(ev,'您还没有绑定竞技场!')
@sv.on_rex(r'^(?:击剑|竞技场)记录 ?(\d)?$')
async def jjc_log_query(bot,ev):
global jjc_log, bind_cache
qid = str(ev.user_id)
member_info = await bot.get_group_member_info(group_id=ev.group_id,user_id=qid)
name = member_info["card"] or member_info["nickname"]
print_all = False
too_long = False
if qid not in bind_cache:
await bot.send(ev,'您还没有绑定竞技场!')
return
pcrid_num = len(bind_cache[qid]['pcrid'])
try:
ret = ev["match"]
pcrid_id_input = int(ret.group(1))
except:
if pcrid_num == 1:
pcrid_id_input = 1
else:
print_all = True
if print_all == False:
if pcrid_id_input == 0 or pcrid_id_input > pcrid_num:
await bot.send(ev,'序号超出范围,请检查您绑定的竞技场列表')
return
if print_all:
msg = f'''\t\t\t\t【{name}的击剑记录】\n'''
jjc_log_cache = []
len_pcrName = []
for pcrid_id in range(pcrid_num): #复制相关的log,并排序
pcrid = bind_cache[qid]['pcrid'][pcrid_id]
pcrName = bind_cache[qid]['pcrName'][pcrid_id]
if pcrid in jjc_log: #计算名字长度
width = 0
for c in pcrName:
if len(c.encode('utf8')) == 3: # 中文
width += 2
else:
width += 1
len_pcrName.append(width)
for log in jjc_log[pcrid]:
log_tmp = list(log)
log_tmp.append(pcrid_id)
jjc_log_cache.append(log_tmp)
else:
len_pcrName.append(0) #没有击剑记录的uid名字长度写0
longest_pcrName = max(len_pcrName)
for i in range(len(len_pcrName)):
len_pcrName[i] = longest_pcrName - len_pcrName[i] #改成补空格的数量
jjc_log_cache_num = len(jjc_log_cache)
if jjc_log_cache_num:
jjc_log_cache.sort(key = lambda x:x[0], reverse=True)
if jjc_log_cache_num > 50:
too_long = True
jjc_log_cache_num = 50
for i in range(jjc_log_cache_num):
timeStamp = jjc_log_cache[i][0]
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
pcrid_id = jjc_log_cache[i][4]
pcrName = bind_cache[qid]['pcrName'][pcrid_id]
space = ' '*len_pcrName[pcrid_id]
jjc_pjjc = 'jjc ' if jjc_log_cache[i][1] == 1 else 'pjjc'
new = jjc_log_cache[i][2]
old = jjc_log_cache[i][3]
if new < old:
change = f'''{old}->{new} [▲{old-new}]'''
else:
change = f'''{old}->{new} [▽{new-old}]'''
msg += f'''{otherStyleTime} {pcrName}{space} {jjc_pjjc}:{change}\n'''
if too_long:
msg += '###由于您订阅了太多账号,记录显示不下嘞~\n###如有需要,可以在查询时加上序号。'
else:
msg += '没有击剑记录!'
else:
msg = f'''\t\t\t【{name}的击剑记录】\n'''
pcrid_id = pcrid_id_input-1
pcrid = bind_cache[qid]['pcrid'][pcrid_id]
pcrName = bind_cache[qid]['pcrName'][pcrid_id]
msg += f'''{pcrName}({pcrid})\n'''
if pcrid in jjc_log:
jjc_log_num = len(jjc_log[pcrid])
for i in range(jjc_log_num):
n = jjc_log_num-1-i #倒序输出,是最近的log在上面
timeStamp = jjc_log[pcrid][n][0]
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
jjc_pjjc = 'jjc' if jjc_log[pcrid][n][1] == 1 else 'pjjc'
new = jjc_log[pcrid][n][2]
old = jjc_log[pcrid][n][3]
if new < old:
change = f'''{old}->{new} [▲{old-new}]'''
else:
change = f'''{old}->{new} [▽{new-old}]'''
msg += f'''{otherStyleTime} {jjc_pjjc}:{change}\n'''
else:
msg += '没有击剑记录!'
pic = image_draw(msg)
await bot.send(ev,f'[CQ:image,file={pic}]')
#========================================竞技场绑定========================================
@sv.on_rex(r'^竞技场绑定 ?(\d+) ?(\S+)?$')
async def on_arena_bind(bot, ev):
ret = ev["match"]
pcrid = int(ret.group(1))
if len(ret.group(1)) != 13:
await bot.send(ev, '位数不对,uid是13位的!')
return
else:
try: #是否指定昵称
if len(ret.group(2)) <=12:
nickname = ret.group(2)
else:
await bot.send(ev, '昵称不能超过12个字,换个短一点的昵称吧~')
return
except:
nickname = ''
await queue.put((4,(resolve3,pcrid,{"bot":bot,"ev":ev,'nickname':nickname,'uid':pcrid,'friendlist':friendList})))
@sv.on_rex(r'^删除竞技场绑定 ?(\d)?$')
async def delete_arena_sub(bot,ev):
global bind_cache, lck
qid = str(ev.user_id)
ret = ev["match"]
if ret.group(1):
pcrid_id = int(ret.group(1))
else:
await bot.send(ev,'输入格式不对!“删除竞技场绑定+【序号】”(序号不可省略)')
return
async with lck:
if qid in bind_cache:
pcrid_num = len(bind_cache[qid]["pcrid"])
if pcrid_num == 1:
await bot.send(ev,'您只有一个绑定的uid,请使用“清空竞技场绑定”删除')
return
if pcrid_id > 0 and pcrid_id <= pcrid_num:
pcrid_id -= 1
result = f'您已成功删除:【{pcrid_id+1}】{bind_cache[qid]["pcrName"][pcrid_id]}({bind_cache[qid]["pcrid"][pcrid_id]})'
del bind_cache[qid]["pcrid"][pcrid_id]
del bind_cache[qid]["noticeType"][pcrid_id]
del bind_cache[qid]["pcrName"][pcrid_id]
save_binds()
await bot.send(ev,result)
else:
await bot.send(ev,'输入的序号超出范围!')
@sv.on_fullmatch('清空竞技场绑定', only_to_me=False)
async def pcrjjc_del(bot, ev):
global bind_cache, lck
qid = str(ev.user_id)
async with lck:
if qid in bind_cache:
reply = '删除成功!\n'
for pcrid_id in range(len(bind_cache[qid]["pcrid"])):
reply += f'''【{pcrid_id+1}】{bind_cache[qid]["pcrName"][pcrid_id]}\n({bind_cache[qid]["pcrid"][pcrid_id]})\n'''
del bind_cache[qid]
else:
reply = '您还没有绑定竞技场!'
await bot.send(ev, reply)
return
save_binds()
await bot.send(ev, reply)
#========================================竞技场设置========================================
@sv.on_rex(r'^竞技场修改昵称 ?(\d)? (\S+)$')
async def change_nickname(bot,ev):
global bind_cache, lck
qid = str(ev.user_id)
if qid not in bind_cache:
reply = '您还没有绑定竞技场!'
await bot.send(ev,reply)
return
ret = ev["match"]
try:
pcrid_id = int(ret.group(1))
except:
pcrid_id = None
if len(ret.group(2)) <= 12:
name = ret.group(2)
else:
await bot.send(ev,'昵称不能超过12个字,换个短一点的昵称吧~')
return
pcrid_num = len(bind_cache[qid]["pcrid"])
if pcrid_id is None:
if pcrid_num == 1:
pcrid_id = 1
else:
await bot.send(ev,'您绑定了多个uid,更改昵称时需要加上序号。')
return
if pcrid_id ==0 or pcrid_id > pcrid_num:
await bot.send(ev,'序号超出范围,请检查您绑定的竞技场列表')
return
async with lck:
pcrid_id -= 1
bind_cache[qid]["pcrName"][pcrid_id] = name
save_binds()
await bot.send(ev,'更改成功!')
@sv.on_fullmatch('在本群推送')
async def group_set(bot,ev):
global bind_cache, lck
qid = str(ev.user_id)
gid = ev.group_id
if qid in bind_cache:
async with lck:
bind_cache[qid]['gid'] = gid
bind_cache[qid]['private'] = False
bind_cache[qid]['notice_on'] = True
reply = '设置成功!已为您开启推送。'
save_binds()
else:
reply = '您还没有绑定竞技场!'
await bot.send(ev,reply)
@on_command('private_notice',aliases=('换私聊推送'),only_to_me= False)
async def private_notice(session):
bot = get_bot()
global bind_cache, lck, friendList
pri_user = 0
qid = str(session.ctx['user_id'])
for i in bind_cache:
if bind_cache[i]['notice_on'] and bind_cache[i]['private']:
pri_user += 1
if pri_user >= MAX_PRI:
await session.send('私聊推送用户已达上限!')
return
if session.ctx['message_type'] != 'private':
await session.send('仅限好友私聊使用!')
return
if len(friendList):
await renew_friendlist()
if qid not in friendList:
return
async with lck:
bind_cache[qid]['private'] = True
bind_cache[qid]['notice_on'] = True
save_binds()
reply = '设置成功!已为您开启推送。已通知管理员!'
reply_adm = f'''{qid}开启了私聊jjc推送!'''
await session.send(reply)
await bot.send_private_msg(user_id = SUPERUSERS[0], message = reply_adm)
@sv.on_rex(r'^竞技场设置 ?(开启|关闭) ?(jjc|pjjc|排名上升|上线提醒) ?(\d)?$')
async def set_noticeType(bot,ev):
global bind_cache, lck
qid = str(ev.user_id)
ret = ev["match"]
turn_on = True if str(ret.group(1))=='开启'else False
change = ret.group(2)
pcrid_id = int(ret.group(3)) if ret.group(3) else None
async with lck:
if qid in bind_cache:
pcrid_num = len(bind_cache[qid]["pcrid"]) #这个qq号绑定的pcrid个数
if pcrid_id is None: #只绑定1个uid时,绑定的序号可以不填。
if pcrid_num == 1:
pcrid_id = 1
else:
reply = '您绑定了多个uid,更改设置时需要加上序号。'
if 0 <= pcrid_id and pcrid_id <= pcrid_num: ##设置成功!
if pcrid_id ==0:
for i in range(pcrid_num):
tmp = int(bind_cache[qid]["noticeType"][i])
jjcNotice = True if tmp//1000 else False
pjjcNotice = True if (tmp%1000)//100 else False
riseNotice = True if (tmp%100)//10 else False
onlineNotice = True if tmp%10 else False
if change == 'jjc':
jjcNotice = turn_on
elif change == 'pjjc':
pjjcNotice = turn_on
elif change == '排名上升':
riseNotice = turn_on
elif change == '上线提醒':
onlineNotice = turn_on
tmp = jjcNotice*1000 + pjjcNotice*100 + riseNotice*10 + onlineNotice
bind_cache[qid]["noticeType"][i] = tmp
else:
pcrid_id -= 1 #从0开始计数,-1
tmp = int(bind_cache[qid]["noticeType"][pcrid_id])
jjcNotice = True if tmp//1000 else False
pjjcNotice = True if (tmp%1000)//100 else False
riseNotice = True if (tmp%100)//10 else False
onlineNotice = True if tmp%10 else False
if change == 'jjc':
jjcNotice = turn_on
elif change == 'pjjc':
pjjcNotice = turn_on
elif change == '排名上升':
riseNotice = turn_on
elif change == '上线提醒':
onlineNotice = turn_on
tmp = jjcNotice*1000 + pjjcNotice*100 + riseNotice*10 + onlineNotice
bind_cache[qid]["noticeType"][pcrid_id] = tmp
reply = '设置成功!'
save_binds()
else:
reply = '序号超出范围,请检查您绑定的竞技场列表'
else:
reply = '您还没有绑定jjc,绑定方式:\n[竞技场绑定 uid] uid为pcr(b服)个人简介内13位数字'
await bot.send(ev,reply)
@sv.on_rex(r'^竞技场设置 ?([01]{3}[0123]) ?(\d)?$')
async def set_allType(bot,ev):
global bind_cache, lck
qid = str(ev.user_id)
ret = ev["match"]
change = ret.group(1) #change: str
pcrid_id = int(ret.group(2)) if ret.group(2) else None
async with lck:
if qid in bind_cache:
pcrid_num = len(bind_cache[qid]["pcrid"]) #这个qq号绑定的pcrid个数
if pcrid_id is None: #只绑定1个uid时,绑定的序号可以不填。
if pcrid_num == 1:
pcrid_id = 1
else:
reply = '您绑定了多个uid,更改设置时需要加上序号。'
if 0 <= pcrid_id and pcrid_id <= pcrid_num: ##设置成功!
change_quick_set = int(change)
if pcrid_id ==0:
for i in range(pcrid_num):
bind_cache[qid]["noticeType"][i] = change_quick_set
else:
pcrid_id -= 1 #从0开始计数,-1
bind_cache[qid]["noticeType"][pcrid_id] = change_quick_set
reply = '设置成功!'
save_binds()
else:
reply = '序号超出范围,请检查您绑定的竞技场列表'
else:
reply = '您还没有绑定jjc,绑定方式:\n[竞技场绑定 uid] uid为pcr(b服)个人简介内13位数字'
await bot.send(ev,reply)
@sv.on_rex(r'^(开启|关闭)竞技场推送$')
async def notice_on_change(bot,ev):
global bind_cache, lck ,friendList
qid = str(ev.user_id)
try:
ret = ev["match"]
turn_on =True if ret.group(1) == '开启' else False
except:
await bot.send(ev,'出错了,请联系管理员!')
return
async with lck:
if qid in bind_cache:
if bind_cache[qid]["notice_on"] == turn_on:
await bot.send(ev,f'您的竞技场推送,已经是{ret.group(1)}状态,不要重复{ret.group(1)}!')
return
else:
if turn_on:
if len(friendList):
await renew_friendlist()
if bind_cache[qid]["private"]:
if qid not in friendList:
await bot.send(ev,'开启私聊推送需要先加好友!你也可以发送“在本群推送”,改为群聊推送。')
return
else:
for i in bind_cache:
if bind_cache[i]['notice_on'] and bind_cache[i]['private']:
pri_user += 1
if pri_user >= MAX_PRI:
await bot.send(ev,'私聊推送用户已达上限!')
return
reply_adm = f'''{qid}开启了私聊jjc推送!'''
await bot.send(ev,'已通知管理员')
await bot.send_private_msg(user_id = SUPERUSERS[0], message = reply_adm)
bind_cache[qid]["notice_on"] = turn_on
else:
await bot.send(ev,'您还没有绑定竞技场!')
return
save_binds()
await bot.send(ev,f'竞技场推送{ret.group(1)}成功!')
#========================================管理员指令========================================
@sv.on_fullmatch('pcrjjc负载查询')
async def load_query(bot,ev):
global bind_cache, today_notice, yesterday_notice
if not priv.check_priv(ev, priv.SUPERUSER):
return
qid_notice_on_private = 0
qid_notice_on_group = 0
pcrid_num_private = 0
pcrid_num_group = 0
for qid in bind_cache:
if bind_cache[qid]['notice_on']:
if bind_cache[qid]['private']:
qid_notice_on_private += 1
pcrid_num_private += len(bind_cache[qid]['pcrid'])
else:
qid_notice_on_group += 1
pcrid_num_group += len(bind_cache[qid]['pcrid'])
msg = f'''pcrjjc负载:\n群聊用户数量:{qid_notice_on_group} 群聊绑定的uid:{pcrid_num_group}个\n私聊用户数量:{qid_notice_on_private} 私聊绑定的uid:{pcrid_num_private}个\n昨天推送次数:{yesterday_notice} 今天推送次数:{today_notice}'''
pic = image_draw(msg)
await bot.send(ev,f'[CQ:image,file={pic}]')
@sv.on_fullmatch('pcrjjc关闭私聊推送')
async def no_private(bot,ev):
global bind_cache ,lck
if not priv.check_priv(ev, priv.SUPERUSER):
return
async with lck:
for qid in bind_cache:
if bind_cache[qid]['private'] and bind_cache[qid]['notice_on']:
bind_cache[qid]['notice_on'] = False
save_binds()
await bot.send(ev,'所有设置为私聊推送的用户的推送已关闭!')
@sv.on_rex(r'^pcrjjc删除绑定 ?(\d{6,10})')
async def del_binds(bot,ev):
global bind_cache, lck
if not priv.check_priv(ev, priv.SUPERUSER):
return
ret = ev["match"]
qid = str(ret.group(1))
if qid in bind_cache:
async with lck:
del bind_cache[qid]
save_binds()
reply = '删除成功!'
else:
reply = '绑定列表中找不到这个qq号!'
await bot.send(ev,reply)
#========================================头像框========================================
# 头像框设置文件不存在就创建文件,并且默认彩色
current_dir = os.path.join(os.path.dirname(__file__), 'frame.json')
if not os.path.exists(current_dir):
data = {"default_frame": "color.png","customize": {}}
with open(current_dir, 'w', encoding='UTF-8') as f:
dump(data, f, indent=4, ensure_ascii=False)
@sv.on_rex(r'^详细查询 ?(\d+)?$')
async def on_query_arena_all(bot, ev):
global bind_cache, lck
ret = ev['match']
id = ret.group(1)
if not id:
await bot.send(ev,'请在详细查询后带上uid或编号', at_sender=True)
return
qid = str(ev['user_id'])
async with lck:
if len(id) < 13:
if not qid in bind_cache:
await bot.send(ev,'您还未绑定竞技场', at_sender=True)
return
elif len(bind_cache[qid]["pcrid"]) < int(id):
await bot.send(ev,'输入的序号超出范围,可发送竞技场查询查看你的绑定', at_sender=True)
return
else:
id = bind_cache[qid]['pcrid'][int(id)-1]
await queue.put((2,(resolve1,id,{"bot":bot,"ev":ev,"uid":id})))
@sv.on_prefix('竞技场换头像框', '更换竞技场头像框', '更换头像框')
async def change_frame(bot, ev):
user_id = ev.user_id
frame_tmp = ev.message.extract_plain_text()
path = os.path.join(os.path.dirname(__file__), 'img/frame/')
frame_list = os.listdir(path)
if not frame_list:
await bot.send(ev, 'img/frame/路径下没有任何头像框,请联系维护组检查目录')
if frame_tmp not in frame_list:
msg = f'文件名输入错误,命令样例:\n更换头像框 color.png\n目前可选文件有:\n' + '\n'.join(frame_list)
await bot.send(ev, msg)
data = {str(user_id): frame_tmp}
current_dir = os.path.join(os.path.dirname(__file__), 'frame.json')
with open(current_dir, 'r', encoding='UTF-8') as f:
f_data = load(f)
f_data['customize'] = data
with open(current_dir, 'w', encoding='UTF-8') as rf:
dump(f_data, rf, indent=4, ensure_ascii=False)
await bot.send(ev, f'已成功选择头像框:{frame_tmp}')
frame_path = os.path.join(os.path.dirname(__file__), f'img/frame/{frame_tmp}')
msg = MessageSegment.image(f'file:///{os.path.abspath(frame_path)}')
await bot.send(ev, msg)
@sv.on_fullmatch('查竞技场头像框', '查询竞技场头像框', '查询头像框')
async def see_a_see_frame(bot, ev):
user_id = str(ev.user_id)
current_dir = os.path.join(os.path.dirname(__file__), 'frame.json')
with open(current_dir, 'r', encoding='UTF-8') as f:
f_data = load(f)
id_list = list(f_data['customize'].keys())
if user_id not in id_list:
frame_tmp = f_data['default_frame']
else:
frame_tmp = f_data['customize'][user_id]
path = os.path.join(os.path.dirname(__file__), f'img/frame/{frame_tmp}')
msg = MessageSegment.image(f'file:///{os.path.abspath(path)}')
await bot.send(ev, msg)
#========================================函数========================================
def save_binds():
with open(config, 'w') as fp:
dump(root, fp, indent=4)
def delete_arena(uid):
'''
订阅删除方法
'''
try:
bind_cache.pop(uid)
save_binds()
except:
logger.info("该用户可能已经不在订阅中")
async def renew_pcrid_list():
global bind_cache, pcrid_list, lck, lck_friendList, friendList
pcrid_list=[]
async with lck_friendList:
copy_friendList = friendList
if len(copy_friendList)==0:
await renew_friendlist()
async with lck_friendList:
copy_friendList = friendList
async with lck:
for qid in bind_cache:
if bind_cache[qid]["notice_on"] == False:
continue
else:
if qid not in copy_friendList and bind_cache[qid]["private"]:
bind_cache[qid]["notice_on"] = False
continue
for i in bind_cache[qid]["pcrid"]:
pcrid_list.append(int(i))
pcrid_list = list(set(pcrid_list))
async def resolve0(data):
global cache, timeStamp
timeStamp = int(time.time())
try:
info = data["res"]['user_info']
except:
return
pcrid = data["uid"]
#logger.info(f'渠query for {pcrid}') #debug
res = [int(info['arena_rank']), int(info['grand_arena_rank']), int(info['last_login_time']), 0, 0]
if pcrid not in cache:
cache[pcrid] = res
else:
last = deepcopy(cache[pcrid])
cache[pcrid][0] = res[0]
cache[pcrid][1] = res[1]
cache[pcrid][2] = res[2]
if res[0] != last[0]:
if res[0] < last[0]:
cache[pcrid][3] += 1 #今日jjc排名上升次数+1
await sendNotice(res[0],last[0],pcrid,1)
if res[1] != last[1]:
if res[1] < last[1]:
cache[pcrid][4] += 1 #今日pjjc排名上升次数+1
await sendNotice(res[1],last[1],pcrid,2)
if res[2] != last[2]:
await sendNotice(res[2],last[2],pcrid,3)
async def resolve1(data):
global bind_cache, lck
res=data["res"]
bot=data["bot"]
ev=data["ev"]
pcrid = data["uid"]
try:
sv.logger.info('开始生成竞技场查询图片...') # 通过log显示信息
result_image = await generate_info_pic(res,pcrid)
result_image = pic2b64(result_image) # 转base64发送,不用将图片存本地
result_image = MessageSegment.image(result_image)
result_support = await generate_support_pic(res,pcrid)
result_support = pic2b64(result_support) # 转base64发送,不用将图片存本地
result_support = MessageSegment.image(result_support)
sv.logger.info('竞技场查询图片已准备完毕!')
for sid in get_self_ids():
try:
await bot.send_group_msg(self_id = sid,group_id = int(ev.group_id),message = f"\n{str(result_image)}\n{result_support}")
break
except Exception as e:
pass
except ApiException as e:
await bot.send(ev,f'查询出错,{e}', at_sender=True)
async def resolve2(data):
global bind_cache, cache, lck
res = data["res"]['user_info']
bot = data["bot"]
ev = data["ev"]
i = data["index"]
pcrid = data["uid"]
manual_query_list_name = data["list"]
query_list = query_cache[ev.user_id]
last_login_hour = (int(res["last_login_time"])%86400//3600+8)%24
last_login_min = int(res["last_login_time"])%3600//60
last_login_min = '%02d' % last_login_min #分钟补零,变成2位
if manual_query_list_name[i]:
res["user_name"] = manual_query_list_name[i]
extra = ''
if pcrid in cache:
extra = f'''上升: {cache[pcrid][3]}次 / {cache[pcrid][4]}次\n'''
query =f'【{i+1}】{res["user_name"]}\n{res["arena_rank"]}({res["arena_group"]}场) / {res["grand_arena_rank"]}({res["grand_arena_group"]}场)\n{extra}最近上号{last_login_hour}:{last_login_min}\n\n'
async with lck:
query_list.append(query)
if len(query_list) == len(manual_query_list_name):
query_list.sort()
pic = image_draw(''.join(query_list))
for sid in get_self_ids():
try:
await bot.send_group_msg(self_id = sid,group_id = int(ev.group_id),message = f'[CQ:image,file={pic}]')
break
except Exception as e:
pass
async def resolve3(data):
global bind_cache, lck, friendList
bot=data["bot"]
ev=data["ev"]
nickname = data["nickname"]
pcrid = data["uid"]
friendList = data['friendlist']
try:
res = data["res"]['user_info']
qid = str(ev.user_id)
gid = ev.group_id
async with lck:
if qid in bind_cache:
bind_num = len(bind_cache[qid]["pcrid"])
if bind_num >= 8:
reply = '您订阅了太多账号啦!'
elif pcrid in bind_cache[qid]["pcrid"]:
reply = '这个uid您已经订阅过了,不要重复订阅!'
else:
bind_cache[qid]["pcrid"].append(pcrid)
bind_cache[qid]["pcrName"].append(nickname if nickname else res["user_name"])
bind_cache[qid]["noticeType"].append(1100)
reply = '添加成功!'
else:
bind_cache[qid] = {
"pcrid": [pcrid],
"noticeType": [1100],
"pcrName": [nickname if nickname else res["user_name"]],
"gid": gid,
"bot_id": 0,
"private":False,
"notice_on":False
}
reply = '添加成功!'
if gid == 0:
bind_cache[qid]["private"] = True
if len(friendList):
await renew_friendlist()
if qid in friendList:
pri_user = 0
for i in bind_cache:
if bind_cache[i]['notice_on'] and bind_cache[i]['private']:
pri_user += 1
if pri_user >= MAX_PRI:
reply += '私聊推送用户已达上限!无法开启私聊推送。你可以发送“在本群推送”,改为群聊推送。'
else:
bind_cache[qid]["notice_on"] = True
reply_adm = f'''{qid}添加了私聊pcrjjc推送'''
await bot.send_private_msg(user_id = SUPERUSERS[0], message = reply_adm)
reply += '已为您开启推送。由于是私聊推送,已通知管理员!'
else:
reply += '开启私聊推送需要先加好友!你也可以发送“在本群推送”,改为群聊推送。'
else:
bind_cache[qid]["notice_on"] = True
reply +='已为您开启群聊推送!'
save_binds()
except:
reply = f'找不到这个uid,大概率是你输错了!'
for sid in get_self_ids():
try:
await bot.send_group_msg(self_id = sid,group_id = int(ev.group_id),message = reply)
break
except Exception as e:
pass
async def sendNotice(new:int, old:int, pcrid:int, noticeType:int): #noticeType:1:jjc排名变动 2:pjjc排名变动 3:登录时间刷新
global bind_cache
global timeStamp, jjc_log, today_notice
bot = get_bot()
if noticeType == 3:
change = '上线了!'
else:
jjc_log_new = (timeStamp, noticeType, new, old)
if pcrid in jjc_log:
if len(jjc_log[pcrid]) >= 20:
del jjc_log[pcrid][0]
jjc_log[pcrid].append(jjc_log_new)
else:
jjc_log_new_tmp = []
jjc_log_new_tmp.append(jjc_log_new)
jjc_log[pcrid] = jjc_log_new_tmp
if noticeType == 1:
change = '\njjc: '
elif noticeType == 2:
change = '\npjjc: '
if new < old:
change += f'''{old}->{new} [▲{old-new}]'''
else:
change += f'''{old}->{new} [▽{new-old}]'''
#-----------------------------------------------------------------
for qid in bind_cache:
if bind_cache[qid]["notice_on"] == False:
continue
for i in range(len(bind_cache[qid]["pcrid"])):
if bind_cache[qid]["pcrid"][i] == pcrid:
msg = ''
tmp = bind_cache[qid]["noticeType"][i]
name = bind_cache[qid]["pcrName"][i]
jjcNotice = True if tmp//1000 else False
pjjcNotice = True if (tmp%1000)//100 else False
riseNotice = True if (tmp%100)//10 else False
onlineNotice = False
if (OnlineType := tmp%10) and noticeType == 3:
if (new-old) < (60 if OnlineType == 3 else 60 * 10):
cache[pcrid][2] = old #间隔太短,不更新缓存
elif OnlineType != 1 or ((new % 86400//3600+8)%24 == 14 and new % 3600 // 60 >= 30): #类型1,只在特定时间播报
onlineNotice = True
if (((noticeType == 1 and jjcNotice) or (noticeType == 2 and pjjcNotice)) and (riseNotice or (new>old))) or (noticeType == 3 and onlineNotice):
print('sendNotice sendNotice sendNotice')
msg = name + change
today_notice += 1
if bind_cache[qid]["private"] == True:
for sid in get_self_ids():
try:
await bot.send_private_msg(self_id = sid, user_id=int(qid), message = msg)
return
except:
pass
bind_cache[qid]["notice_on"] = False
else:
msg += '[CQ:at,qq=' + qid + ']'
for sid in get_self_ids():
try:
await bot.send_group_msg(self_id = sid, group_id = int(bind_cache[qid]["gid"]), message = msg)
break
except Exception as e:
pass
break
#========================================AUTO========================================
@sv.scheduled_job('interval', hours = 5)
async def renew_friendlist():
global friendList, lck_friendList
bot = get_bot()
old_friendList = friendList
for sid in get_self_ids():
flist = await bot.get_friend_list(self_id = sid)
async with lck_friendList:
friendList = []
for i in flist:
friendList.append(str(i['user_id']))
old_friendList = list(set(old_friendList))
friendList = list(set(friendList))
@sv.on_notice('friend_add') #新增好友时,不全部刷新好友列表
async def friend_add(session: NoticeSession):
global friendList
ev = session.event
new_friend = str(ev.user_id)
async with lck_friendList:
friendList.append(new_friend)
@sv.scheduled_job('interval', minutes=0.3) # minutes是刷新频率,可按自身服务器性能输入其他数值,可支持整数、小数
async def on_arena_schedule():
await renew_pcrid_list()
await queue.join()
await gather(*map(lambda uid: queue.put((10,(resolve0,uid,{"uid":uid}))), pcrid_list))
@sv.on_notice('group_decrease.leave')
async def leave_notice(session: NoticeSession):
global lck, bind_cache
uid = str(session.ctx['user_id'])
gid = str(session.ctx['group_id'])
bot = get_bot()
async with lck:
binds = deepcopy(bind_cache)
info = binds[uid]
if uid in bind_cache and info['gid'] == gid:
delete_arena(uid)
await bot.send_group_msg(group_id = int(info['gid']),message = f'{uid}退群了,已自动删除其绑定在本群的竞技场订阅推送')
@sv.scheduled_job('cron', hour='5')
async def clear_ranking_rise_time():
global cache, today_notice ,yesterday_notice
yesterday_notice = today_notice
today_notice = 0
for pcrid in list(cache.keys()):
if pcrid in pcrid_list:
cache[pcrid][3] = 0
cache[pcrid][4] = 0
else:
del cache[pcrid]
async with lck: #上线提醒LV3改成2
for qid in bind_cache:
for i in range(len(bind_cache[qid]['noticeType'])):
if bind_cache[qid]['noticeType'][i]%10 ==3:
bind_cache[qid]['noticeType'][i] -= 1
save_binds()
#========================================TODO========================================
'''
@sv.on_prefix('竞技场历史')
async def send_arena_history(bot, ev):
#竞技场历史记录
global bind_cache, lck
uid = str(ev['user_id'])
if uid not in bind_cache:
await bot.send(ev, '未绑定竞技场', at_sender=True)
else:
ID = bind_cache[uid]['id']
msg = f'\n{JJCH._select(ID, 1)}'
await bot.send(ev, msg, at_sender=True)
@sv.on_prefix('公主竞技场历史')
async def send_parena_history(bot, ev):
global bind_cache, lck
uid = str(ev['user_id'])
if uid not in bind_cache:
await bot.send(ev, '未绑定竞技场', at_sender=True)
else:
ID = bind_cache[uid]['id']