-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
2708 lines (2294 loc) · 91.6 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
from mirai import Mirai, WebSocketAdapter, GroupMessage, Image, FriendMessage, At, MessageEvent
from mirai_extensions.trigger.message import GroupMessageFilter, FriendMessageFilter
from mirai_extensions.trigger import InterruptControl
from PIL import ImageDraw, ImageFont
from io import BytesIO
from collections import defaultdict
from datetime import datetime, timedelta
import numpy as np
import base64
import qianfan
import jieba
import glob
import sqlite3
import snownlp
from snownlp import SnowNLP
import threading
import urllib
from pathlib import Path
import math
from typing import Tuple
import pandas as pd
import logging
import random
import os
import io
import re
import colorlog
import time
import sys
import configparser
import requests
import string
import json
import asyncio
import inspect
import websockets
# import shutil
py_version = 'v1.51'
data_dir = './data/'
db_path = os.path.join(data_dir, 'qq.db3')
pd.StringDtype('pyarrow')
# RL快速方法正则式
WEIGHTED_CHOICE_PATTERN = re.compile(
r'%(?P<name>[^%!]+)%'
r'(?:(?P<R>R:(\d*\.?\d*))?'
r'(?:(?P<sep1>,)?(?P<L>L:(\d+)))?)?'
r'!'
)
face_del = r'\{face:\d+\}'
csv_path = './data/reply.csv' # 替换为你的CSV文件路径
config = configparser.ConfigParser()
image_folder = '.\data\CG'
# 只是log
logger = logging.getLogger('LoveYou')
logger.setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.DEBUG)
fmt_string = '%(log_color)s[%(name)s][%(levelname)s]%(message)s'
# black red green yellow blue purple cyan 和 white
log_colors = {
'DEBUG': 'white',
'INFO': 'cyan',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'purple'
}
fmt = colorlog.ColoredFormatter(fmt_string, log_colors=log_colors)
stream_handler.setFormatter(fmt)
logger.addHandler(stream_handler)
logger.info(
'''
.____ _____.___.
| | _______ __ ____ \__ | | ____ __ __
| | / _ \ \/ // __ \ / | |/ _ \| | \
| |__( <_> ) /\ ___/ \____ ( <_> ) | /
|_______ \____/ \_/ \___ >__________/ ______|\____/|____/
\/ \/_____/_____|/
''')
logger.info('-by hlfzsi')
time.sleep(1)
logger.info('正在加载reply.csv')
try:
df = pd.read_csv(csv_path, header=None, dtype=str,
usecols=range(5), encoding='utf-8')
df.iloc[:, 4] = df.iloc[:, 4].fillna('1')
logger.info('reply.csv已成功加载')
except:
logger.error('未能成功读取reply.csv,请确认文件是否存在')
logger.error('程序将在5秒后退出')
time.sleep(5)
sys.exit()
logger.info('检查数据库...')
# 检查data目录是否存在,如果不存在则创建
if not os.path.exists(data_dir):
os.makedirs(data_dir)
# 连接到SQLite数据库
conn = None
try:
# 尝试连接数据库,如果文件不存在则会自动创建
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 检查表是否存在
cursor.execute('''
SELECT name
FROM sqlite_master
WHERE type='table' AND name='qq_love';
''')
table_exists = cursor.fetchone() is not None
# 如果表不存在,则创建表
if not table_exists:
cursor.execute('''
CREATE TABLE qq_love (
QQ TEXT PRIMARY KEY,
love INTEGER
);
''')
# 提交事务
conn.commit()
finally:
# 关闭数据库连接
if conn:
conn.close()
logger.info('数据库检查完成')
MAX_AGE = timedelta(minutes=10) # 消息的有效时间为10分钟
previous_msgs = defaultdict(datetime)
groups_df = {}
def loadconfig():
# 读取配置文件
config_path = os.path.join(os.getcwd(), "config.ini")
try:
with open(config_path, "r", encoding='utf-8') as config_file:
config.read_file(config_file)
logger.info('正在加载config.ini')
except FileNotFoundError:
logger.error('无法加载config.ini,请检查文件是否存在或格式是否正确')
time.sleep(5)
sys.exit(1)
# 获取配置项的值
bot_qq = config.get('bot', 'bot_qq')
verify_key = config.get('bot', 'verify_key')
host = config.get('bot', 'host')
port = config.get('bot', 'port')
bot_name = config.get('others', 'bot_name')
baseline = config.getint('random_CG', 'baseline')
rate = config.getfloat('random_CG', 'rate')
master = config.get('others', 'master')
lv_enable = config.get('lv', 'enable')
common_love = config.get('csv', 'common_love')
a, b = (value.strip() for value in common_love.split(','))
search_love = config.get('others', 'search_love_reply')
ws = config.get('others', 'ws')
react = config.get('ai', '@_react')
ws_port = config.getint('others', 'ws_port')
model = config.get('ai', 'model')
role = config.get('ai', 'role')
API_Key = config.get('ai', 'API_Key')
Secret_Key = config.get('ai', 'Secret_Key')
tank_enable = config.get('others', 'tank_enable')
memory = config.get('ai', 'memory')
if memory == 'True':
memory = True
else:
memory = False
logger.info('config.ini第一部分已成功加载')
return bot_qq, verify_key, host, port, bot_name, baseline, rate, master, lv_enable, a, b, search_love, ws, react, ws_port, model, role, API_Key, Secret_Key, tank_enable, memory
bot_qq, verify_key, host, port, bot_name, baseline, rate, master, lv_enable, Ca, Cb, search_love_reply, ws, botreact, ws_port, model, role, API_Key, Secret_Key, tank_enable, memory = loadconfig()
# 初始化ai回复
if botreact != 'True' or model == 'qingyunke':
del role, API_Key, Secret_Key
else:
os.environ["QIANFAN_AK"] = API_Key
os.environ["QIANFAN_SK"] = Secret_Key
logger.info('ai对话初始化完成')
# logger.debug(bot_qq+'\n'+verify_key+'\n'+host+'\n'+port+'\n'+bot_name+'\n'+master+'\n'+lv_enable)
df.iloc[:, 2] = df.iloc[:, 2].fillna(f'({Ca},{Cb})')
del Ca, Cb
def get_range(value):
if La <= value < Lb:
logger.debug('获得lv1')
return 1
elif Lc <= value < Ld:
logger.debug('获得lv2')
return 2
elif Le <= value < Lf:
logger.debug('获得lv3')
return 3
elif Lg <= value < Lh:
logger.debug('获得lv4')
return 4
elif Li <= value < Lj:
logger.debug('获得lv5')
return 5
else:
logger.debug('未获得lv')
return None # 返回None表示不属于任何已知范围
def ws_get_range(qq):
try:
love = read_love(qq)
try:
lv = get_range(int(love))
except:
lv = -1
if lv == None and love >= 0:
lv = 6
elif lv == None and love < 0:
lv = 0
return lv
except:
return 'Fail'
def loadconfig_part2():
# 读取配置文件
config_path = os.path.join(os.getcwd(), "config.ini")
try:
with open(config_path, "r", encoding='utf-8') as config_file:
config.read_file(config_file)
logger.info('正在加载config.ini')
except FileNotFoundError:
logger.error('无法加载config.ini,请检查文件是否存在或格式是否正确')
time.sleep(5)
sys.exit(1)
logger.info('正在加载第二部分config.ini')
lv1 = config.get('lv', 'lv1')
a, b = (value.strip() for value in lv1.split(','))
lv2 = config.get('lv', 'lv2')
c, d = (value.strip() for value in lv2.split(','))
lv3 = config.get('lv', 'lv3')
e, f = (value.strip() for value in lv3.split(','))
lv4 = config.get('lv', 'lv4')
g, h = (value.strip() for value in lv4.split(','))
lv5 = config.get('lv', 'lv5')
i, j = (value.strip() for value in lv5.split(','))
lv1_reply = config.get('lv', 'lv1_reply')
lv1_reply = lv1_reply.replace('\\n', '\n')
lv2_reply = config.get('lv', 'lv2_reply')
lv2_reply = lv2_reply.replace('\\n', '\n')
lv3_reply = config.get('lv', 'lv3_reply')
lv3_reply = lv3_reply.replace('\\n', '\n')
lv4_reply = config.get('lv', 'lv4_reply')
lv4_reply = lv4_reply.replace('\\n', '\n')
lv5_reply = config.get('lv', 'lv5_reply')
lv5_reply = lv5_reply.replace('\\n', '\n')
logger.info('config.ini第二部分已成功加载')
return a, b, c, d, e, f, g, h, i, j, lv1_reply, lv2_reply, lv3_reply, lv4_reply, lv5_reply
if lv_enable == 'True':
logger.info('初始化好感等级...')
La, Lb, Lc, Ld, Le, Lf, Lg, Lh, Li, Lj, lv1_reply, lv2_reply, lv3_reply, lv4_reply, lv5_reply = loadconfig_part2()
try:
La = int(La)
Lb = int(Lb)
Lc = int(Lc)
Ld = int(Ld)
Le = int(Le)
Lf = int(Lf)
Lg = int(Lg)
Lh = int(Lh)
Li = int(Li)
Lj = int(Lj)
logger.info('好感等级加载完成')
except:
logger.error('好感等级条件填写有误,请填写整数')
logger.error('程序将在5秒后退出')
time.sleep(5)
sys.exit
else:
logger.info('好感等级无需加载')
logger.info('config.ini第二部分已跳过加载')
time.sleep(1)
try:
response = requests.get(
"https://api.github.com/repos/hlfzsi/yirimirai_LoveYou/releases/latest")
py_update = (response.json()["tag_name"])
if py_version == py_update:
logger.info('当前已为最新版本'+py_version)
elif 'beta' in py_version:
logger.warning('当前为beta版,程序并不稳定')
logger.warning(
'前往https://github.com/hlfzsi/yirimirai_LoveYou/releases获得最新版')
else:
logger.warning('本项目有更新,请前往https://github.com/hlfzsi/yirimirai_LoveYou/releases\n' +
'当前版本:'+py_version+' 最新版本:'+py_update)
except:
logger.warning('未连接到网络,无法检查更新')
del py_update
del py_version
time.sleep(3)
'''
def clear_group_not_exist(groupid:str)->None:
#清理退出群聊文件
shutil.rmtree(f'./data/group/{groupid}')
shutil.rmtree(f'./data/pic/group/{groupid}')
logger.debug(f'{groupid}文件已清理')
'''
def chat_memory(qq: str, question: str, answer: str):
"""处理用户对话
如果answer为空,则只添加question并返回字符串.文件不保留.
如果answer非空,无return,保留修改
Args:
qq (str): 用户
question (str): 用户消息
answer (str): 回复消息
"""
# 构造文件路径
file_path = os.path.join('./data/memory', f'{qq}.json')
# 确保目录存在
os.makedirs(os.path.dirname(file_path), exist_ok=True)
# 初始化聊天记录列表,如果文件不存在则为空列表
try:
with open(file_path, 'r', encoding='utf-8') as file:
records = json.load(file)
except:
records = []
# 如果answer不是空字符串,则添加新的聊天记录
if answer: # 检查answer是否为非空字符串
records.append({"role": "user", "content": question})
records.append({"role": "assistant", "content": answer})
records_json = json.dumps(records, ensure_ascii=False, indent=4)
with open(file_path, 'w', encoding='utf-8') as file:
json.dump(records, file, ensure_ascii=False, indent=4)
return None
else:
records.append({"role": "user", "content": question})
records_json = json.dumps(records, ensure_ascii=False, indent=4)
records_json = json.loads(records_json)
return records_json
def clear_memory(qq: str):
file_path = os.path.join('./data/memory', f'{qq}.json')
try:
os.remove(file_path)
except:
pass
def reduce_memory(qq: str):
file_path = f"./data/memory/{qq}.json"
with open(file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
del data[:2]
with open(file_path, 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=4)
async def baidu_ai(msg: str, qq: str, intlove, name: str) -> str:
'''
通过百度模型获得ai回复
'''
intlove = str(intlove)
time = datetime.now()
time = time.strftime("%Y-%m-%d.%H:%M")
loacl_role = role
loacl_role = loacl_role.replace(
'[intlove]', intlove).replace('[sender]', name).replace('[time]', time)
if memory == True:
send_msg = chat_memory(qq, msg, '')
while len(send_msg) + len(loacl_role) >= 24000:
reduce_memory(qq)
send_msg = chat_memory(qq, msg, '')
resp = qianfan.ChatCompletion().do(model=model,
messages=send_msg, temperature=0.98, top_p=0.7, penalty_score=1, system=loacl_role)
try:
chat_memory(qq, msg, resp['result'])
if resp['need_clear_history'] == True or resp['need_clear_history'] == 'True':
clear_memory(qq)
logger.debug('清理用户记忆')
except:
pass
elif memory == False:
resp = qianfan.ChatCompletion().do(model=model,
messages=[{"role": "user", "content": msg}], temperature=0.98, top_p=0.7, penalty_score=1, system=loacl_role)
try:
return resp['result']
except:
return '模型填写错误喵~猫闹过载ing'
def choose_pic(qq, images_dir='./data/images/'):
"""
从给定路径下取出以qq为文件名的图片(不论后缀如何),
如果不存在,取出default.jpg。
参数:
qq (str): 文件名(不包括后缀)
images_dir (str, optional): 图片所在的目录路径。
返回:
str: 匹配的图片文件路径或default.jpg的路径
"""
# 将路径转换为Path对象,以便使用Path方法
images_dir = Path(images_dir)
# 遍历目录以查找匹配的文件
for file in images_dir.iterdir():
# 如果文件名(不包括后缀)与qq匹配
if file.stem == qq:
# 返回匹配的文件路径
return str(file)
# 如果没有找到匹配的文件,返回default.jpg的路径
logger.warning('没有找到匹配的文件,返回default.jpg')
return str(images_dir / 'default.jpg')
def pic_reply(qq, name, background_path, ico):
"""生成用户图片化的好感查询结果
Args:
qq (str): 用户qq号
name (str): 群名片
background_path (str): 用户底层背景
ico (str): 用户头像
Returns:
bytes: 图片结果的base64编码
"""
from PIL import Image
int_love, str_love = get_both_love(qq)
def sentence():
ci = random.choice('abcdefghijkl')
url = "https://v1.hitokoto.cn/?c=" + ci + "&encode=text"
r = requests.post(url)
return r.text
sen = sentence()
def part_cover(image):
# 创建一个与原图大小相同的RGBA图片,用于存放结果
result = image.copy().convert('RGBA')
# 创建一个与蒙版大小相同的白色(带有50%透明度的)蒙版
mask = Image.new('RGBA', (1000, 1000),
(255, 255, 255, 128)) # 128 = 50% 透明度
# 将蒙版粘贴到结果图片的中心位置
result.paste(mask, (12, 12, 1012, 1012), mask)
return result
def is_image_file(filename):
# 判断一个文件名是否为图片文件
return any(filename.lower().endswith(ext) for ext in ['.jpg', '.jpeg', '.png', '.bmp'])
def pick_pic(path, number):
# 构造对应number的文件夹路径
folder_path = os.path.join(path, str(number))
# 获取文件夹中的所有文件和子目录
files = os.listdir(folder_path)
# 过滤出图片文件
image_files = [os.path.join(folder_path, f)
for f in files if is_image_file(f)]
# 如果没有图片文件,则返回一个错误消息
if not image_files:
logger.error('无对应贴画可供使用')
# 随机选择一个图片文件
chosen_image = random.choice(image_files)
# 返回完整图片路径
return chosen_image
def add_cartoon(bg, cartoon):
# 随机选择一个位置放置cartoon
left = random.randint(0, 689)
top = random.randint(0, 689)
# 在bg图片上粘贴cartoon图片
bg.paste(cartoon, (left, top), cartoon)
# 返回结果图片
return bg
# 打开背景图片
background = Image.open(background_path)
background = background.resize((1024, 1024), Image.LANCZOS)
lv = get_range(int_love)
if lv == None and int_love > 0:
lv = 5
lv_r = 'Nan'
elif lv == None and int_love <= 0:
lv = 1
lv_r = 'Nan'
else:
lv_r = str(lv)
# 打开图标图片
response = requests.get(ico)
image_bytes = response.content
ico = Image.open(BytesIO(image_bytes))
cartoon = pick_pic('./data/images/cartoon/', lv)
cartoon = Image.open(cartoon)
# 缩放图标到325x325像素
ico = ico.resize((325, 325), Image.LANCZOS)
cartoon = cartoon.resize((335, 335), Image.LANCZOS)
cartoon = cartoon.convert('RGBA')
background = background.convert('RGBA')
background.paste(ico, (590, 75))
background = add_cartoon(background, cartoon)
background = part_cover(background)
draw = ImageDraw.Draw(background)
# 加载字体
font = ImageFont.truetype('arial.ttf', 60)
font2 = ImageFont.truetype('arial.ttf', 75)
draw.text((19, 100), name, font=font2, fill=(128, 118, 105))
draw.text((19, 354), '好感等级:Lv.'+lv_r, font=font, fill=(244, 27, 90))
draw.text((19, 508), '好感度:'+str_love, font=font, fill=(244, 27, 90))
senl = sen.replace(',', ',').replace(
'.', '。').replace(',', ',\n').replace('。', '。\n')
draw.text((75, 662), senl, font=font, fill=(0, 0, 0))
background = background.convert('RGB')
buffered = io.BytesIO()
background.save(buffered, format='JPEG')
result = buffered.getvalue()
result = base64.b64encode(result)
return result
async def qingyunke(msg):
url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(
urllib.parse.quote(msg))
html = requests.get(url)
return html.json()["content"]
def del_admin(groupid, qq, filename='./data/admin.json'):
admin_data = load_admin(filename)
# 检查groupid和common列表是否存在
if groupid in admin_data and 'common' in admin_data[groupid]:
if qq in admin_data[groupid]['common']:
# 尝试在common列表中删除qq
admin_data[groupid]['common'].remove(qq)
with open(filename, 'w') as file:
json.dump(admin_data, file, indent=4)
global admin_qqs
admin_qqs = load_admin()
def del_admin_high(groupid, qq, filename='./data/admin.json'):
admin_data = load_admin(filename)
# 检查groupid和high列表是否存在
if groupid in admin_data and 'high' in admin_data[groupid]:
# 检查qq是否在high列表中
if qq in admin_data[groupid]['high']:
# 尝试在high列表中删除qq
admin_data[groupid]['high'].remove(qq)
with open(filename, 'w') as file:
json.dump(admin_data, file, indent=4)
global admin_qqs
admin_qqs = load_admin()
def load_admin(filename='./data/admin.json'):
try:
with open(filename, 'r') as file:
data = json.load(file, strict=False)
return data
except:
return {}
admin_qqs = load_admin()
def write_admin(groupid, type_, qq, filename='./data/admin.json'):
admin_data = load_admin(filename)
# 检查groupid是否存在
if groupid not in admin_data:
# 如果不存在,则新建groupid,并初始化high和common列表
admin_data[groupid] = {
'high': [],
'common': []
}
# 检查type_是否有效
if type_ not in ['high', 'common']:
raise ValueError("Invalid type. It should be 'high' or 'common'.")
isAdmin = check_admin(groupid, qq)
if isAdmin == type_:
return None
# 将qq添加到对应type_的列表中
admin_data[groupid][type_].append(qq)
# 将更新后的数据写回JSON文件
with open(filename, 'w') as file:
json.dump(admin_data, file, indent=4)
global admin_qqs
admin_qqs = load_admin()
def check_admin(groupid, qq):
# 检查 groupid 是否存在于 admin_qqs 中
if groupid in admin_qqs:
# 检查 'high' 列表
if qq in admin_qqs[groupid]['high']:
return 'high'
# 检查 'common' 列表
elif qq in admin_qqs[groupid]['common']:
return 'common'
# 如果 qq 在所有的 groupid 下都没有找到,返回 False
return False
def group_load(groupid):
'''
加载群聊词库
这一函数通常可以被 global groups_df | groups_df[groupid] = df 完全取代
请避免使用本函数,因为它的性能不如替代方案
如果你需要加载所有词库,请使用read_csv_files_to_global_dict函数
'''
# 构造文件路径
file_path = os.path.join('./data/group', f"{groupid}.csv")
# 如果文件存在,读取CSV到DataFrame
if os.path.exists(file_path):
df = pd.read_csv(file_path, dtype=str,
usecols=range(6), encoding='utf-8')
# 将DataFrame添加到全局字典中,以groupid为键
global groups_df
groups_df[groupid] = df
def group_write(groupid: str, question: str, answer: str, type: str):
'''
groupid:群号
question:触发词
answer:回复
type:类型.1为精准匹配,2为模糊匹配
'''
# 构造文件路径
base_path = './data/group'
file_path = os.path.join(base_path, f"{groupid}.csv")
# 如果文件不存在,创建一个空的DataFrame
if not os.path.exists(file_path):
df = pd.DataFrame(
columns=['Question', 'Answer', 'Love', 'Range', 'Type', 'Status'])
else:
global groups_df
# 如果文件存在,读取它
df = groups_df[groupid]
# 将新的数据添加到DataFrame中
new_row = pd.DataFrame([[question, answer, '', '', type, '']], columns=[
'Question', 'Answer', 'Love', 'Range', 'Type', 'Status'])
df = pd.concat([df, new_row], ignore_index=True)
# 将DataFrame写入CSV文件
df.to_csv(file_path, index=False, encoding='utf-8')
groups_df[groupid] = df
# group_load(groupid)
logger.debug('写入成功')
def group_del(groupid, question):
# 构造文件路径
file_path = os.path.join('./data/group', f"{groupid}.csv")
global groups_df
if groupid in groups_df:
df = groups_df[groupid]
# 找到与question完全匹配且Status不为locked的行
mask = (df['Question'] == question) & (df['Status'] != 'locked')
rows_to_delete = df[mask].index.tolist()
# 从后向前删除行,避免索引变化影响迭代
for index in sorted(rows_to_delete, reverse=True):
row = df.iloc[index]
if '[pic=' in row['Answer']:
_, path = pic_support(row['Answer'])
try:
os.remove(f'./data/pic/group/{groupid}/{path}')
except FileNotFoundError:
logger.warning(f"文件 {path} 未找到,无法删除。")
# 删除DataFrame中的行
df.drop(index, inplace=True)
# 将修改后的DataFrame写回CSV文件
df.to_csv(file_path, index=False)
# 更新全局变量
groups_df[groupid] = df
logger.debug('删除成功')
else:
logger.warning(f"{file_path}不存在")
def jaccard_similarity(list1, list2):
intersection = len(set(list1).intersection(set(list2)))
union = len(set(list1)) + len(set(list2)) - intersection
return intersection / union if union else 0
def tokenize(text):
return list(jieba.cut(text, cut_all=False))
def new_msg_judge(msg, jaccard_threshold=0.75):
# 使用结巴库进行分词
tokens = tokenize(msg)
# 清理过期的消息
current_time = datetime.now()
for prev_msg, timestamp in list(previous_msgs.items()):
if (current_time - timestamp) >= MAX_AGE:
del previous_msgs[prev_msg]
# 检查当前消息是否与先前的消息高度相似
for prev_msg, timestamp in previous_msgs.items():
prev_tokens = tokenize(prev_msg)
jaccard_sim = jaccard_similarity(tokens, prev_tokens)
if jaccard_sim >= jaccard_threshold:
return False # 如果找到高度相似的msg且在MAX_AGE内,返回False
# 如果没有找到高度相似的msg,将其添加到previous_msgs字典中
previous_msgs[msg] = datetime.now()
return True
def map_sentiment_to_range(sentiment_score, target_min=-10, target_max=10):
# 线性映射函数,但调整斜率使得中间区域变化小,极端值变化大
if sentiment_score >= 0.54:
# 正面情感,使用较缓的斜率
mapped_score = (sentiment_score - 0.5) * 2 * (target_max -
target_min) + target_min + (target_max - target_min) / 2.41
elif sentiment_score <= 0.46:
mapped_score = (sentiment_score - 0.6) * 2 * (target_max -
target_min) + target_min + (target_max - target_min) / 2.41
else:
mapped_score = (sentiment_score - 0.5) * 2 * (target_max -
target_min) + target_min + (target_max - target_min) / 2.41
# 确保值在目标范围内
mapped_score = max(min(mapped_score, target_max), target_min)
return mapped_score
def add_random_fluctuation(score, target_min, target_max):
# 添加一个固定的随机波动在[-1, 1]范围内
fluctuation = random.uniform(-1, 1)
fluctuated_score = max(
min(score + fluctuation, target_max), target_min) # 确保值在目标范围内
return fluctuated_score
def adjust_score_if_high(score, threshold, deduction_range):
# 如果得分大于等于阈值,则随机减去一个整数
if score >= threshold:
deduction = random.randint(deduction_range[0], deduction_range[1])
score -= deduction
score = math.floor(score)
return score
def adjust_score_if_low(score, threshold, deduction_range):
# 如果得分小于等于阈值,则随机加上一个整数
if score <= threshold:
deduction = random.randint(deduction_range[0], deduction_range[1])
score += deduction
score = math.floor(score)
return score
def love_score(text: str, target_min=-10, target_max=10):
# 使用 SnowNLP 分析文本情感倾向
s = snownlp.SnowNLP(text)
sentiment_score = s.sentiments
# 映射情感倾向
mapped_score = map_sentiment_to_range(
sentiment_score, target_min, target_max)
# 添加随机波动
fluctuated_score = add_random_fluctuation(
mapped_score, target_min, target_max)
fluctuated_score = adjust_score_if_high(fluctuated_score, 7, [0, 7])
final_score = adjust_score_if_low(fluctuated_score, -7, [0, 7])
final_score = int(final_score)
# 返回结果
return final_score
def generate_codes(a, b):
'''a为数目,b为类型'''
if b == 0:
filename = './data/alias_code.txt'
elif b == 1:
filename = './data/love_code.txt'
elif b == 2:
filename = './data/pic_code.txt'
# 字符集,包含大小写字母和数字
characters = string.ascii_letters + string.digits
# 读取文件中的所有code并放入集合中
with open(filename, 'r', encoding='utf-8') as file:
existing_codes = set(line.strip() for line in file if line.strip())
# 初始生成的code集合
generated_codes = set()
# 生成code的循环
while len(generated_codes) < a:
# 一次性生成多个code以提高效率
batch_size = 1 # 可以根据实际情况调整
new_codes = set(
''.join(random.choices(characters, k=8)) for _ in range(batch_size)
)
# 使用集合的差集操作来获取不与现有code重复的code
unique_codes = new_codes - existing_codes
# 更新生成的code集合和已存在的code集合
generated_codes.update(unique_codes)
existing_codes.update(unique_codes)
# 将生成的code写入文件
with open(filename, 'w', encoding='utf-8') as file:
for code in generated_codes:
file.write(code + '\n')
def write_str_love(qq, str_value, file_path='.\data\qq.txt'):
with open(file_path, 'r+', encoding='utf-8') as file:
lines = file.readlines()
updated = False
new_lines = []
for line in lines:
line = line.strip() # 移除行尾的换行符和可能的空白字符
if line.startswith(qq + '='):
# 如果找到匹配的qq,则更新它并标记为已更新
new_lines.append(f"{qq}={str_value}\n")
updated = True
else:
# 否则保留原行
new_lines.append(line + '\n')
# 如果qq不存在于文件中,则添加新行
if not updated:
new_lines.append(f"{qq}={str_value}\n")
# 回到文件开头并写入所有行
file.seek(0)
file.writelines(new_lines)
file.truncate() # 确保文件大小正确
def write_pic(qq, pic, file_path='.\data\pic.txt'):
with open(file_path, 'r+', encoding='utf-8') as file:
lines = file.readlines()
updated = False
new_lines = []
for line in lines:
line = line.strip() # 移除行尾的换行符和可能的空白字符
if line.startswith(qq + '='):
# 如果找到匹配的qq,则更新它并标记为已更新
new_lines.append(f"{qq}={pic}\n")
updated = True
else:
# 否则保留原行
new_lines.append(line + '\n')
# 如果qq不存在于文件中,则添加新行
if not updated:
new_lines.append(f"{qq}={pic}\n")
# 回到文件开头并写入所有行
file.seek(0)
file.writelines(new_lines)
file.truncate() # 确保文件大小正确
def code_record(a):
# 获取当前时间,并格式化为字符串(精确到秒)
current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# 构建要写入文件的字符串,包括时间和传入的字符串a
message = f"{current_time} - {a}\n"
# 打开文件以追加模式('a'),将内容追加到文件末尾
with open('./data/code_users.txt', 'a', encoding='utf-8') as file:
file.write(message)
def RL_support(s: str) -> Tuple[str, int]:
items = []
total_weight = 0.0
# 解析字符串并构建items列表
for match in WEIGHTED_CHOICE_PATTERN.finditer(s):
item = {
'name': match.group('name'),
# R值,默认1.0
'R': float(match.group('R')[2:] if match.group('R') else '1.0'),
# L值,默认0
'L': int(match.group('L')[2:] if match.group('L') else '0')
}
total_weight += item['R']
items.append(item)
# 如果没有有效的items,返回默认值
if not items:
return '', 0
# 按照权重随机选择
r = random.random() * total_weight
for item in items:
r -= item['R']
if r <= 0:
return item['name'], item['L']
logger.warning('RL出现异常')
return None, 0 # 或者考虑抛出一个异常
def pic_support(text: str) -> Tuple[str, str]:
'''返回图片名称'''
# 正则表达式匹配 [pic=任意图片名.(png|jpg|jpeg)]
pattern = r'\[pic=(.*?\.(png|jpg|jpeg))\]'
# 查找所有匹配项
matches = re.findall(pattern, text)
# 如果没有找到匹配项,则直接返回原字符串和 None
if not matches:
return text, None
# 只关心第一个匹配项
path = matches[0][0]
# 使用 re.sub() 替换掉第一个匹配项
new_text = re.sub(pattern, '', text, count=1)
return new_text, path
def update_alias(qq, str_value, alias_file='./data/alias.txt'):
with open(alias_file, 'r+', encoding='utf-8') as file:
lines = file.readlines()
updated = False
new_lines = []
for line in lines:
line = line.strip() # 移除行尾的换行符和可能的空白字符
if line.startswith(qq + '='):
# 如果找到匹配的qq,则更新它并标记为已更新
new_lines.append(f"{qq}={str_value}\n")
updated = True
else:
# 否则保留原行
new_lines.append(line + '\n')
# 如果qq不存在于文件中,则添加新行
if not updated:
new_lines.append(f"{qq}={str_value}\n")