diff --git a/QQBot.py b/QQBot.py index 727676b..b2e53a6 100644 --- a/QQBot.py +++ b/QQBot.py @@ -423,6 +423,7 @@ def __init__(self, tuin, isSess, group_sig, service_type): self.tqq = uin_to_account(tuin) self.lastcheck = time.time() self.lastseq=0 + self.replystreak = 0 logging.info("私聊线程生成,私聊对象:"+str(self.tqq)) def check(self): self.lastcheck = time.time() @@ -441,8 +442,12 @@ def push(self, ipContent, seq): return True else: self.lastseq=seq - + #防止机器人对聊 + if self.replystreak>30: + self.replystreak = 0 + return True try: + self.replystreak = self.replystreak + 1 logging.info("PM get info from AI: "+ipContent) paraf={ 'userid' : str(self.tqq), 'key' : tulingkey, 'info' : ipContent} info = HttpClient_Ist.Get('http://www.tuling123.com/openapi/api?'+urllib.urlencode(paraf)) @@ -545,6 +550,8 @@ def handle(self, send_uin, content, seq): # pass if self.aboutme(content): return + if self.deleteall(content): + return if self.callout(send_uin, content): return if self.follow(send_uin, content): @@ -601,18 +608,21 @@ def follow(self, send_uin, content): return False def save(self): - with open("database.save", "w+") as savefile: - savefile.write(json.dumps(self.replyList)) - + try: + with open("database."+str(self.gid)+".save", "w+") as savefile: + savefile.write(json.dumps(self.replyList)) + savefile.close() + except Exception, e: + logging.error("写存档出错:"+str(e)) def load(self): try: - with open("database.save", "r") as savefile: + with open("database."+str(self.gid)+".save", "r") as savefile: saves = savefile.read() if saves: self.replyList = json.loads(saves) + savefile.close() except Exception, e: logging.info("读取存档出错:"+str(e)) - def callout(self, send_uin, content): pattern = re.compile(r'^(?:!|!)(ai) (.+)') match = pattern.match(content) @@ -644,13 +654,26 @@ def aboutme(self, content): try: if match: logging.info("output about info") - info="小黄鸡3.0 By Jeffery, 源代码:(github.com/zeruniverse/QQRobot)\n使用语法: (按优先级排序,若同时触发则只按优先级最高的类型回复。注意所有!均为半角符号,即英文!)\n\n1.帮助(关于),输入!about,样例:\n!about\n\n2.智能鸡:输入!ai (空格)+问题,小黄鸡自动回复,举例:\n!ai 你是谁?\n\n3.随从鸡:输入!follow QQ号,小黄鸡会重复发送该QQ号所有发送内容,如对自己使用可以直接使用!follow me,举例:\n!follow 123456789\n!follow me\n取消复读则输入!unfollow QQ(或me),举例:\n!unfollow 123456789\n!unfollow me\n\n4.学习鸡:使用!learn {A}{B}命令让小黄鸡学习,以后有人说A的时候小黄鸡会自动说B。!learn后面有空格,全部符号均为半角(英文),例如\n!learn {你是谁}{我是小黄鸡}\n删除该记录则\n!delete {你是谁}{我是小黄鸡}\n\n5.复读鸡:当群里连着两次出现同样信息时复读一遍\n\n\n私戳小黄鸡可以私聊,私聊无格式,全部当智能鸡模式处理。" + info="小黄鸡3.3 By Jeffery, 源代码:(github.com/zeruniverse/QQRobot)\n使用语法: (按优先级排序,若同时触发则只按优先级最高的类型回复。注意所有!均为半角符号,即英文!)\n\n1.帮助(关于),输入!about,样例:\n!about\n\n2.智能鸡:输入!ai (空格)+问题,小黄鸡自动回复,举例:\n!ai 你是谁?\n\n3.随从鸡:输入!follow QQ号,小黄鸡会重复发送该QQ号所有发送内容,如对自己使用可以直接使用!follow me,举例:\n!follow 123456789\n!follow me\n取消复读则输入!unfollow QQ(或me),举例:\n!unfollow 123456789\n!unfollow me\n\n4.学习鸡:使用!learn {A}{B}命令让小黄鸡学习,以后有人说A的时候小黄鸡会自动说B。!learn后面有空格,全部符号均为半角(英文),例如\n!learn {你是谁}{我是小黄鸡}\n删除该记录则\n!delete {你是谁}{我是小黄鸡}\n一次删除所有记录使用:\n!deleteall\n\n6.复读鸡:当群里连着两次出现同样信息时复读一遍\n\n\n私戳小黄鸡可以私聊,私聊无格式,全部当智能鸡模式处理。" self.reply(info) return True except Exception, e: logging.error("ERROR"+str(e)) return False - + def deleteall(self, content): + pattern = re.compile(r'^(?:!|!)(deleteall)') + match = pattern.match(content) + try: + if match: + logging.info("Delete all learned data for group:"+str(self.gid)) + info="已删除所有学习内容" + self.replyList.clear() + self.save() + self.reply(info) + return True + except Exception, e: + logging.error("ERROR:"+str(e)) + return False # ----------------- # 主程序 # ----------------- @@ -662,7 +685,7 @@ def aboutme(self, content): vpath = sys.argv[1] if len(sys.argv) > 2: qq = sys.argv[2] - print "QQRobot 3.0 By Jeffery" + print "QQRobot 3.3 By Jeffery" print "Log files will be recorded to log.txt in the same folder as this executable file" print "Please write all group numbers for groups you want the robot to follow in groupfollow.txt" print "This program can normally run stably for 1.5-2.5 days. Then QQ may ask to login again. When that happens, this program will exit due to fatal error and you should run it again.\n" @@ -677,7 +700,8 @@ def aboutme(self, content): qqLogin = Login(vpath, qq) except Exception, e: logging.error(str(e)) - print "Fatal error: Unable to get AI key or fail to login" + print "Fatal error: Unable to get AI key or fail to login.\nPlease restart this program. If you don't close the window, the program ends automatically in 15 seconds" + time.sleep(15) os._exit() t_check = check_msg() t_check.setDaemon(True)