-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (61 loc) · 2.34 KB
/
Copy pathmain.py
File metadata and controls
66 lines (61 loc) · 2.34 KB
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
# 主程序
import keyboard # 检测键盘按键
import yaml
import ai
import global_data
import msg
# 读取YAML文件
with open("config.yml", "r", encoding='utf-8') as file:
config = yaml.safe_load(file)
# 归零初始化
msg.return_zero()
if __name__ == "__main__":
# 如果这是执行的脚本(及主程序)
# 定义聊天时间,聊天次数,聊天列表
chat_time = 0
chat_ans = 0
chat_list = []
print("###启动QQBot")
while True:
# 控制Bot的结束与暂停
if keyboard.is_pressed("home"):
global_data.is_exit = True
if keyboard.is_pressed("end"):
input("###程序暂停")
if global_data.is_exit:
print("###程序结束")
exit()
try:
# 如果发现了新消息
if msg.find():
# 聊天次数 + 1
chat_ans += 1
print(f"###发言计数器:{chat_ans}")
if chat_ans >= 5:
# 如果聊天次数 >= 5,聊天时间 + 1,聊天次数归1
chat_ans = 1
print("###计数器已满,开始发言")
# 获取消息加入至聊天列表
chat_list.append(msg.get() + "\n")
chat_text = ""
if chat_time < 3:
# 如果聊天时间 < 3,把0~聊天时间的聊天记录加入聊天文本
for i in range(0, chat_time):
chat_text += chat_list[i]
else:
# 否则,把聊天时间 - 3~聊天时间的聊天记录加入聊天文本
for i in range(chat_time - 3, chat_time):
chat_text += chat_list[i]
# 询问Ai,并且处理消息
text = msg.process(ai.zhipu_call_text(config["ai_settings"] + chat_text + "\n你:?"))
print(text)
if not ("拒绝回答" in text[0]):
msg.send(text)
else:
print("###拒绝回答")
chat_time += 1
# 归零
msg.return_zero()
except:
pass
# time.sleep(0.1)