Skip to content

Commit fe66c00

Browse files
committed
update 3.0.3
1 parent 1f1ecbb commit fe66c00

12 files changed

+363
-354
lines changed

ChatAdmin.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,14 @@ def get_admin_command(data, adminlist=0, sender=0, group=0):
7777

7878

7979
def exitadmin(getadminsign=0):
80-
file = open('config.clc', 'r', encoding='utf-8-sig')
81-
config = json_load(file)
82-
file.close()
80+
81+
config = json_load('config.clc')
82+
8383
if getadminsign == 1:
8484
return config['admin']
8585
config['admin'] = 0
86-
file = open('config.clc', 'w', encoding='utf-8-sig')
87-
json_dump(config, file, indent=3, ensure_ascii=False)
88-
file.close()
86+
json_dump(config, 'config.clc', indent=3, ensure_ascii=False)
87+
8988

9089

9190
def getnode(data, tempdict, answerlist, group, sender, question):
@@ -107,7 +106,7 @@ def getnode(data, tempdict, answerlist, group, sender, question):
107106
time.sleep(0.8)
108107
tempdict.pop(question)
109108
filename = str(group) + '.cl' # 读取已缓存的词库
110-
pickle_dump(tempdict, open('WordStock/' + filename, 'wb'))
109+
pickle_dump(tempdict, 'WordStock/' + filename)
111110
simuse.Send_Message(data, sender, 2, '已清空', 1)
112111
return None
113112
if node == str(-1) or node == '–1':
@@ -156,7 +155,7 @@ def getnode(data, tempdict, answerlist, group, sender, question):
156155
if answerlist == []:
157156
tempdict.pop(question)
158157
filename = str(group) + '.cl' # 读取已缓存的词库
159-
pickle_dump(tempdict, open('WordStock/' + filename, 'wb'))
158+
pickle_dump(tempdict, 'WordStock/' + filename)
160159
if templist != []:
161160
if sendtext != '':
162161
simuse.Send_Message(
@@ -183,9 +182,9 @@ def getnode(data, tempdict, answerlist, group, sender, question):
183182

184183

185184
def getconfig(adminnum=0):
186-
file = open('config.clc', 'r', encoding='utf-8-sig')
187-
config = json_load(file)
188-
file.close()
185+
186+
config = json_load('config.clc')
187+
189188
if adminnum == 1:
190189
return config['Administrator']
191190
admin = config['admin']
@@ -303,7 +302,7 @@ def getanswer(data, sender, group, question): # 从词库中获取答案
303302
continue
304303
question = str(question)
305304
filename = str(group) + '.cl' # 读取已缓存的词库
306-
tempdict = pickle_load(open('WordStock/' + filename, 'rb'))
305+
tempdict = pickle_load('WordStock/' + filename)
307306
try: # 检索问题,若词库中无该问题,则函数返回-1,若有,则返回所有答案(答案列表)
308307
#print(question)
309308
questiondict = tempdict[question]

ChatAllfind.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ def createxcel(groupcldict):
215215

216216
#上传工作表至cos
217217
def uploadcos(data, filename):
218-
secret_id = 'xxx'
219-
secret_key = 'xxx'
218+
secret_id = 'XX'
219+
secret_key = 'XX'
220220
region = 'ap-shanghai'
221221
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key)
222222
client = CosS3Client(config)
@@ -357,7 +357,7 @@ def delcl(data, sender, lines_dict, nodelist, target_type=0, group=0):
357357
delsign = 0
358358
for group in changedict:
359359
cldict_orign = pickle_load(
360-
open('WordStock/' + str(group) + '.cl', 'rb'))
360+
'WordStock/' + str(group) + '.cl')
361361
cldict = copy.deepcopy(cldict_orign)
362362
questionlist = list(cldict_orign.keys())
363363
changelist = changedict[group]
@@ -377,8 +377,7 @@ def delcl(data, sender, lines_dict, nodelist, target_type=0, group=0):
377377
if answerlist == []:
378378
cldict.pop(question)
379379
cldict_orign = cldict
380-
pickle_dump(cldict_orign, open('WordStock/' + str(group) + '.cl',
381-
'wb'))
380+
pickle_dump(cldict_orign, 'WordStock/' + str(group) + '.cl')
382381
if changedict != {} and nofoundlines == []:
383382
print('删除{}个条目成功'.format(delsign))
384383
simuse.Send_Message(data, target, target_type,
@@ -575,7 +574,7 @@ def findallanswer(data, sender, cllist, answer, group=0, target_type=0):
575574
for i in cllist:
576575
findquestiondict = {}
577576
findquestion[i[:-3]] = findquestiondict
578-
cldict = pickle_load(open('WordStock/' + i, 'rb'))
577+
cldict = pickle_load('WordStock/' + i)
579578
for l in answer:
580579
for k in cldict:
581580
questiondict = cldict[k]

ChatCheck.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212

1313
def getallconfig():
14-
file = open('config.clc', 'r', encoding='utf-8-sig')
15-
config = json_load(file)
16-
file.close()
14+
15+
config = json_load('config.clc')
16+
1717
return config
1818

1919

@@ -36,7 +36,7 @@ def clcheck(filename, data, fromchat):
3636
question_num = 0
3737
answer_num = 0
3838
allanswerlist = []
39-
cldict = pickle_load(open('WordStock/' + filename, 'rb'))
39+
cldict = pickle_load('WordStock/' + filename)
4040
for i in cldict:
4141
question_num += 1
4242
questioninfo = cldict[i]
@@ -121,6 +121,11 @@ def main(data, fromchat):
121121
cosmatchtip = cosmatchtip.format('开启')
122122
else:
123123
cosmatchtip = cosmatchtip.format('关闭')
124+
atreplytip = '艾特回复:{}'
125+
if config['atreply'] == 1:
126+
atreplytip = atreplytip.format('开启')
127+
else:
128+
atreplytip = atreplytip.format('关闭')
124129

125130
replychancetip = '回复触发概率:{}%'.format(config['replychance'])
126131
replywaittip = '回复等待时间:{:g}±{:g}秒'.format(config['replywait'][0],
@@ -169,7 +174,7 @@ def main(data, fromchat):
169174
check_version[1])
170175
else:
171176
versiontip = "未连接至ChatLearning服务器"
172-
situation = learningtip + '\n' + replytip + '\n' + voicereplytip + '\n' + golbetip + '\n' +cosmatchtip+'\n'+ replychancetip + '\n' + replywaittip + '\n' + replycdtip + '\n' +cosmatchingtip+'\n'+ voicereplychancetip + '\n' + synthesizertip + '\n' + mergetimetip + '\n' + intervaltip + '\n' + blackfreqtip + '\n' + tempmessagenumtip + '\n' + botnametip + '\n' + replylengthtip
177+
situation = learningtip + '\n' + replytip + '\n' + voicereplytip + '\n' + golbetip + '\n' +cosmatchtip+'\n'+atreplytip+'\n'+ replychancetip + '\n' + replywaittip + '\n' + replycdtip + '\n' +cosmatchingtip+'\n'+ voicereplychancetip + '\n' + synthesizertip + '\n' + mergetimetip + '\n' + intervaltip + '\n' + blackfreqtip + '\n' + tempmessagenumtip + '\n' + botnametip + '\n' + replylengthtip
173178
situationchain = [{'type': 'Plain', 'text': situation}]
174179
typefreq_message = [{'type': 'Plain', 'text': typefreqtip}]
175180
siglereply_message = [{'type': 'Plain', 'text': singlereplytip}]

ChatClass.py

+28-39
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,35 @@
1818

1919
import simuse
2020

21-
version = '3.0.2'
21+
version = '3.0.3'
2222

23-
filename_set= set()
23+
lock = threading.Lock()
2424

25-
26-
def lock(filename):
27-
while True:
28-
if not(filename in filename_set):
29-
break
30-
time.sleep(0.1)
31-
filename_set.add(filename)
32-
33-
def release_lock(filename):
34-
filename_set.remove(filename)
35-
36-
def pickle_dump(obj, file):
37-
lock(file.name)
38-
pickle.dump(obj, file)
39-
release_lock(file.name)
40-
file.close()
25+
def pickle_dump(obj, path):
26+
with lock:
27+
with open(path,'wb') as file:
28+
pickle.dump(obj, file)
4129
return True
4230

4331

44-
def pickle_load(file):
45-
lock(file.name)
46-
obj = pickle.load(file)
47-
release_lock(file.name)
48-
file.close()
32+
def pickle_load(path):
33+
with lock:
34+
with open(path,'rb') as file:
35+
obj = pickle.load(file)
4936
return obj
5037

5138

52-
def json_dump(obj, file, indent=3, ensure_ascii=False):
53-
lock(file.name)
54-
json.dump(obj, file, indent=indent, ensure_ascii=ensure_ascii)
55-
release_lock(file.name)
56-
file.close()
39+
def json_dump(obj, path, indent=3, ensure_ascii=False):
40+
with lock:
41+
with open(path,'w',encoding='utf-8-sig') as file:
42+
json.dump(obj, file, indent=indent, ensure_ascii=ensure_ascii)
5743
return True
5844

5945

60-
def json_load(file):
61-
lock(file.name)
62-
obj = json.load(file)
63-
release_lock(file.name)
64-
file.close()
46+
def json_load(path):
47+
with lock:
48+
with open(path,'r',encoding='utf-8-sig') as file:
49+
obj = json.load(file)
6550
return obj
6651

6752
class Platform:
@@ -100,6 +85,7 @@ class commandclass():
10085
commandtips['cosmatch'] = '#开启/关闭问题余弦相似度计算'
10186
commandtips['learning <秒>'] = '#设定词库链间隔时间'
10287
commandtips['reply <%>'] = '#设定回复的触发几率'
88+
commandtips['atreply'] = '#开启/关闭艾特回复'
10389
commandtips['reply -s <%> <群号>'] = '#单独设定回复触发几率'
10490
commandtips['reply -d <群号>'] = '#清除单独设定的回复触发几率'
10591
commandtips['replywait <基准时间> <浮动时间>'] = '#设定回复的等待时间'
@@ -436,7 +422,7 @@ def Version():
436422

437423
def stop_run():
438424

439-
config = json_load(open('config.clc', 'r', encoding='utf-8-sig'))
425+
config = json_load('config.clc')
440426
sign = config["stopsign"]
441427

442428
if sign == 0:
@@ -509,7 +495,7 @@ def ClChange(self):
509495
dicts = file.read()
510496
dicts = eval(dicts)
511497
file.close()
512-
pickle_dump(dicts, open(i, 'wb'))
498+
pickle_dump(dicts, i)
513499
print('准备完毕!')
514500

515501
def Cl_version(self):
@@ -529,24 +515,24 @@ def Cl_version(self):
529515
if self.version < 280:
530516
print('正在更新词库版本,{} -> 280 请勿中途退出'.format(self.version))
531517
for i in cllist:
532-
dicts = pickle_load(open(i, 'rb'))
518+
dicts = pickle_load(i)
533519
for k in dicts.keys():
534520
questiondict = dicts[k]
535521
if not ('freq' in questiondict.keys()):
536522
questiondict['freq'] = 1
537-
pickle_dump(dicts, open(i, 'wb'))
523+
pickle_dump(dicts, i)
538524
shutil.move(i, 'WordStock/' + i)
539525

540526
# 3.0.0前版本更新需要为词库Key添加"regular"正则判断键
541527
if self.version < 300:
542528
print('正在更新词库版本,{} -> 300 请勿中途退出'.format(self.version))
543529
for i in cllist:
544-
dicts = pickle_load(open('WordStock/'+i, 'rb'))
530+
dicts = pickle_load('WordStock/'+i)
545531
for k in dicts.keys():
546532
questiondict = dicts[k]
547533
if not ('regular' in questiondict.keys()):
548534
questiondict['regular'] = False
549-
pickle_dump(dicts, open('WordStock/'+i, 'wb'))
535+
pickle_dump(dicts, 'WordStock/'+i)
550536

551537

552538

@@ -596,5 +582,8 @@ def Config_version(self):
596582
print('正在更新config, -> 302 请勿中途退出')
597583
config['botname'] = ['我']
598584
config['replylength'] = 100
585+
if self.version < 303:
586+
print('正在更新config, -> 303 请勿中途退出')
587+
config['atreply'] = 1
599588

600589
return config

ChatDelete.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def Delete(data, sender):
6868

6969
def Merge(Mergedict, filename):
7070
version_error = 0
71-
cldict = pickle_load(open('WordStock/' + filename, 'rb'))
71+
cldict = pickle_load('WordStock/' + filename)
7272
#print(Mergedict)
7373
try:
7474
repeatquestion = Mergedict.keys() & cldict.keys()
@@ -95,7 +95,7 @@ def Merge(Mergedict, filename):
9595

9696

9797
def Delete_freq(Mergedict, filename):
98-
cldict = pickle_load(open('WordStock/' + filename, 'rb'))
98+
cldict = pickle_load('WordStock/' + filename)
9999
freqdel_num = 0
100100
for question in Mergedict:
101101
questiondict = Mergedict[question]
@@ -105,12 +105,12 @@ def Delete_freq(Mergedict, filename):
105105
freqdel_num += 1
106106
except:
107107
continue
108-
pickle_dump(cldict, open('WordStock/' + filename, 'wb'))
108+
pickle_dump(cldict, 'WordStock/' + filename)
109109
return freqdel_num
110110

111111

112112
def Delete_Filter(filename):
113-
cldict = pickle_load(open('WordStock/' + filename, 'rb'))
113+
cldict = pickle_load('WordStock/' + filename)
114114
question_list = list(cldict.keys())
115115
filterdel_num = 0
116116
for question in question_list:
@@ -126,5 +126,5 @@ def Delete_Filter(filename):
126126
if ChatFilter.filtercheck(eval(answertext), display=False) == 0:
127127
answerlist_origin.remove(answerdict)
128128
filterdel_num += 1
129-
pickle_dump(cldict, open('WordStock/' + filename, 'wb'))
129+
pickle_dump(cldict, 'WordStock/' + filename)
130130
return filterdel_num

0 commit comments

Comments
 (0)