-
Notifications
You must be signed in to change notification settings - Fork 0
/
robotmodel.py
336 lines (272 loc) · 8.46 KB
/
robotmodel.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
import sys
import os
import websocket
import time
class hackchat:
"""
Author:jmr(@jiangmuran)
restart_program:重新连接
connect:连接到ws服务器
join:加入到特定频道
disconnect:断开连接
reconnect:重新连接并加入
send:发送信息
sendme:发送me信息
sendw:发送私信
sendcmd:发送命令
changechannel:切换频道
changenick:切换昵称
changepasswd:切换密码
changecolor:切换颜色
getonlinelist:获取在线昵称列表
recvreal:接收实际信息
recv:只接收聊天信息
_choosemsg:辨别信息
restart_program():
程序内部重连,相当于reconnect()
connect(wsaddress):
连接到指定hc服务器
如果连接不成功将尝试重复连接,可修改全局变量reconn_time
join(channel,nick,passwd):
发送加入请求
正常返回为列表,[0]为在线昵称列表,[1]为用户详细信息列表
如果加入不成功将尝试重复连接,可修改全局变量reconn_time
dissconnect():
断开与hc连接
reconnect():
重新连接hc并加入特定频道(请务必在之前处于连接状态!)
send(text):
发送普通消息(要求加入)
sendme(text):
发送emote消息(要求加入)
sendw(nick,text):
发送指定私聊消息
sendcmd(cmd):
发送原始cmd消息(为字典)
changechannel(channel):
切换频道(注意频率限制)
changenick(nick):
切换昵称(限制少)
changepasswd(passwd):
切换昵称(限制少)
changecolor():
切换颜色(限制少)
getonlinelist():
返回当前的在线用户列表
请注意,本功能需要recv()函数作为支撑
recvreal():
获取当前的原始数据(字典)
如果遇到onlineuser修改会自动补充
recv():
获取当前最新一条分类信息
返回一个列表:
[0]:消息类型,0为正常,1为emote,2为私信,3为警告,4为提示,5为其他
[1]:发送者,字符串,如果是警告则为!warn,提示为!info,其他为发送方(如果已知)
[2]:消息内容,字符串,其他为具体内容(如果已知)
"""
def __init__(self):
self.wsaddress=''
self.ws=websocket.WebSocket()
self.channel=''
self.nick=''
self.passwd=''
self.color=''
self.onlinelist=[]
self.ver='qwq1.3'
self.reconn_time=30 # 出错后重试时间
# 重新连接hc并加入
def reconnect(self):
try:
self._connect(self.wsaddress)
return self.join(self.channel,self.nick,self.passwd)
except:
print("error,reconnect in "+str(self.reconn_time)+"s")
time.sleep(self.reconn_time)
print("reconnecting.....")
self.restart_program()
#出错后重试
def restart_program(self):
self.reconnect()
#字符串转字典
def _str2dict(self,string):
false=False
true=True
null=None
return eval(string)
#链接到hc服务器
def connect(self,wsaddress):
try:
self.wsaddress=wsaddress
self.ws.connect(self.wsaddress)
except:
print("error,reconnect in "+str(self.reconn_time)+"s")
time.sleep(self.reconn_time)
print("reconnecting.....")
python = sys.executable
os.execl(python, python, * sys.argv)
def _connect(self,wsaddress):
try:
self.wsaddress=wsaddress
self.ws.connect(self.wsaddress)
except:
print("error,reconnect in "+str(self.reconn_time)+"s")
time.sleep(self.reconn_time)
print("reconnecting.....")
self.restart_program()
# 加入频道
def join(self,channel,nick,passwd):
try:
self.channel=channel
self.nick=nick
self.passwd=passwd
self.onlinelist=[]
# 发送加入信息
self.ws.send(('{"cmd":"join","nick":"'+nick+'","pass":"'+passwd+'","channel":"'+channel+'"}'))
print("connect!")
# 解析内容
rec=self._str2dict(self.ws.recv())
# 如果类型为成功上线
if rec['cmd'] == 'onlineSet':
print('joined!')
self.onlinelist=rec['nicks']
return [rec['nicks'],rec['users']]
# 如果类型为服务端发出错误
elif rec['cmd'] == 'warn':
print('server recv a error,reconnecting...')
print(rec['text'])
raise
# 未知原因
else:
print('??? error!')
print(rec)
raise
except:
print("error,reconnect in "+str(self.reconn_time)+"s")
time.sleep(self.reconn_time)
print("reconnecting.....")
python = sys.executable
os.execl(python, python, * sys.argv)
# 发送hc的断开链接请求
def disconnect(self):
try:
self.onlinelist=[]
self.ws.send('{"cmd":"disconnect","cmdKey":"what"}')
except:
print("error,reconnect in "+str(self.reconn_time)+"s")
time.sleep(self.reconn_time)
print("reconnecting.....")
self.restart_program()
def getonlinelist(self):
return self.onlinelist
def send(self,text):
try:
self.ws.send('{"cmd":"chat","text":"'+text+'"}')
pass
except:
print("error,reconnect in "+str(self.reconn_time)+"s")
time.sleep(self.reconn_time)
print("reconnecting.....")
self.restart_program()
def sendme(self,text):
try:
self.sendcmd('{"cmd":"emote","text":"'+text+'"}')
except:
print("error,reconnect in "+str(self.reconn_time)+"s")
time.sleep(self.reconn_time)
print("reconnecting.....")
self.restart_program()
def sendw(self,nick,text):
try:
self.sendcmd('{"cmd":"whisper","nick":"'+nick+'","text":"'+text+'"}')
except:
print("error,reconnect in "+str(self.reconn_time)+"s")
time.sleep(self.reconn_time)
print("reconnecting.....")
self.restart_program()
def sendcmd(self,cmddict):
try:
self.ws.send(cmddict)
pass
except:
print("error,reconnect in "+str(self.reconn_time)+"s")
time.sleep(self.reconn_time)
print("reconnecting.....")
self.restart_program()
def changechannel(self,channel):
try:
self.channel=channel
self.reconnect()
except:
print("error,reconnect in "+str(self.reconn_time)+"s")
time.sleep(self.reconn_time)
print("reconnecting.....")
self.restart_program()
def changenick(self,nick):
try:
self.nick=nick
self.sendcmd('{"cmd":"changenick","nick":"'+nick+'"}')
pass
except:
print("error,restart in "+str(self.reconn_time)+"s")
time.sleep(self.reconn_time)
print("restarting.....")
self.restart_program()
def changecolor(self,color):
try:
self.color=color
self.sendcmd('{"cmd":"changecolor","color":"'+color+'"}')
pass
except:
print("error,restart in "+str(self.reconn_time)+"s")
time.sleep(self.reconn_time)
print("restarting.....")
self.restart_program()
def changepasswd(self,passwd):
try:
self.passwd=passwd
self.reconnect()
except:
print("error,restart in "+str(self.reconn_time)+"s")
time.sleep(self.reconn_time)
print("restarting.....")
self.restart_program()
def _choosemsg(self,msgstr):
try:
msgdict=self._str2dict(msgstr)
# 新用户
if msgdict['cmd'] == 'onlineRemove':
del self.onlinelist[self.onlinelist.index(msgdict['nick'])]
return [5,'!userleft',msgdict['nick']]
elif msgdict['cmd'] == 'onlineAdd':
self.onlinelist.append(msgdict['nick'])
return [5,'!useradd',msgdict['nick']]
elif msgdict['cmd'] == 'chat':
return [0,msgdict['nick'],msgdict['text']]
elif msgdict['cmd'] == 'emote':
return [1,msgdict['nick'],msgdict['text'][(2+len(msgdict['nick'])):]]
elif msgdict['cmd'] == 'chat':
return [0,msgdict['nick'],msgdict['text']]
elif msgdict['cmd'] == 'info':
if msgdict.__contains__('type'):
if msgdict['type'] == 'whisper' :
if msgdict['text'][0:3] != 'You':
return [2,msgdict['text'].split(' ')[0],msgdict['text'][len(msgdict['text'].split(' ')[0])+12:]]
else:
return [5,'!echo',msgdict['text']]
else:
return [4,'!info',msgdict['text']]
elif msgdict['cmd'] == 'warn':
return [3,'!warn',msgdict['text']]
else:
return [5,'not found',msgstr]
except:
print("error,restart in "+str(self.reconn_time)+"s")
time.sleep(self.reconn_time)
print("restarting.....")
self.restart_program()
def recvreal(self):
xyz=self.ws.recv()
self._choosemsg(xyz)
return self._str2dict(xyz)
def recv(self):
return self._choosemsg(self.ws.recv())