-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitchplaystoontown.py
368 lines (326 loc) · 7.86 KB
/
twitchplaystoontown.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
import socket
import pyautogui
import threading
import time
import json
from auth import login
SERVER = "irc.twitch.tv"
PORT = 6667
PASS = login()
BOT = "PeachBot"
CHANNEL = "itspeachtea"
OWNER = "itspeachtea"
messageList = []
inTasks = False
inGags = False
irc = socket.socket()
irc.connect((SERVER, PORT))
irc.send((
"PASS "+ PASS + "\n" +
"NICK " + BOT + "\n" +
"JOIN #" + CHANNEL + "\n"
).encode())
def gameControl():
monitorSize = pyautogui.size()
sinceLastInput = time.time()
with open('gags.json') as f:
gags = json.load(f)
with open('unsafeBounds.json') as f:
unsafeBounds = json.load(f)
with open('movement.json') as f:
movement = json.load(f)
with open('keys.json') as f:
keys = json.load(f)
with open('mouseLocations.json') as f:
mouseLocations = json.load(f)
with open('mouse.json') as f:
mouseDirections = json.load(f)
def holdKey(key, seconds):
seconds = min(seconds, 30.0)
pyautogui.keyDown(key)
time.sleep(seconds)
pyautogui.keyUp(key)
def controlMovement(args, size, message):
direction = args[0]
if(size > 0 and direction in movement["inputs"]):
key = movement["inputs"][direction]["key"]
if(size == 2):
try:
t = float(args[1])
except:
t = 0.0
if(t > 0):
newThread = threading.Thread(target = holdKey, args=(key, t))
newThread.start()
return True
elif(size == 1):
pyautogui.keyDown(key)
pyautogui.keyUp(key)
return True
return False
def controlMovement(args, size, message):
direction = message
t = 0
if(size > 1):
try:
t = float(args[size-1])
except:
t = 0
if(t > 0):
direction = " ".join(args[:(size-1)])
if(direction in movement["inputs"]):
key = movement["inputs"][direction]["key"]
if(t > 0):
newThread = threading.Thread(target = holdKey, args=(key, t))
newThread.start()
return True
else:
pyautogui.keyDown(key)
pyautogui.keyUp(key)
return True
return False
def useGag(args, size, message):
if(size > 1):
gag = " ".join(args[1:])
if(gag in gags):
coordX = gags[gag]["use"]["x"]
coordY = gags[gag]["use"]["y"]
pyautogui.moveTo(coordX, coordY)
clickMouse()
return True
return False
def buyGag(args, size, message):
if(size > 1):
try:
x = int(args[size-1])
except:
x = -1
if(x >= 0):
gag = " ".join(args[1:(size-1)])
else:
gag = " ".join(args[1:])
x = 1
if(gag in gags):
coordX = gags[gag]["buy"]["x"]
coordY = gags[gag]["buy"]["y"]
for i in range(x):
pyautogui.moveTo(coordX, coordY)
clickMouse()
return True
return False
def controlShow(args, size, message):
if(size == 2):
name = args[1]
if(name == "gags"):
showGags()
return True
elif(name == "tasks"):
showTasks()
return True
return False
def controlHide(args, size, message):
if(size == 2):
name = args[1]
if(name == "gags"):
hideGags()
return True
elif(name == "tasks"):
hideTasks()
return True
return False
def showGags():
global inGags
global inTasks
if (inGags):
hideGags()
if (inTasks):
hideTasks()
pyautogui.keyDown('home')
inGags = True
def showTasks():
global inGags
global inTasks
if (inTasks):
hideTasks()
if (inGags):
hideGags()
pyautogui.keyDown('end')
inTasks = True
def hideGags():
global inGags
if(inGags):
pyautogui.keyUp('home')
inGags = False
def hideTasks():
global inTasks
if(inTasks):
pyautogui.keyUp('end')
inTasks = False
def inUnsafeClickBounds():
posx, posy = pyautogui.position()
for bound in unsafeBounds:
if (posx >= bound["xMin"] and
posx <= bound["xMax"] and
posy >= bound["yMin"] and
posy <= bound["yMax"]):
return True
return False
def controlMouse(args, size, message):
direction = " ".join(args[1:])
x = -1
y = -1
t = 1
if(size > 2):
try:
x = min(int(args[size - 2]), 1919)
except:
x = -1
try:
y = min(int(args[size - 1]), 1079)
except:
y = -1
if(x > 0 and y > 0):
direction = " ".join(args[1:(size - 2)])
elif(y > 0):
direction = " ".join(args[1:(size - 1)])
t = y
else:
return False
if(direction == "click"):
for i in range(min(t, 15)):
clickMouse()
return True
elif(direction == "to"):
if(x > 0 and y > 0):
pyautogui.moveTo(x, y)
return True
elif(direction == "hold"):
mouseDown()
return True
elif(direction == "release"):
mouseUp()
return True
elif(direction in mouseDirections["inputs"]):
speed = t * mouseDirections["speed"]
mouseDirX = speed * mouseDirections["inputs"][direction]["x"]
mouseDirY = speed * mouseDirections["inputs"][direction]["y"]
if(mouseDirX == 0):
mouseDirX = None
if(mouseDirY == 0):
mouseDirY = None
pyautogui.moveRel(mouseDirX, mouseDirY)
posx, posy = pyautogui.position()
print(str(posx)+", "+str(posy))
return True
return False
def mouseDown():
if(inUnsafeClickBounds()):
print("WARNING: unsafe click detected")
return False
else:
pyautogui.mouseDown()
return True
def mouseUp():
pyautogui.mouseUp();
def clickMouse():
if(mouseDown()):
mouseUp()
def keyPress(message):
key = keys["inputs"][message]["key"]
pyautogui.keyDown(key)
pyautogui.keyUp(key)
def mouseLocation(message):
location = mouseLocations["inputs"][message]
pyautogui.moveTo(location['x'], location['y'])
clickMouse()
def typeMessage(message):
pyautogui.typewrite(message, interval=0.01)
pyautogui.keyDown("enter")
pyautogui.keyUp("enter")
while True:
if(len(messageList) > 0):
sinceLastInput = time.time()
timeoutStalling = False
msg = messageList.pop(0)
user = msg["user"]
message = msg["message"].lower()
args = message.split(" ")
argsLength = len(args)
command = args[0]
print("reading " + message)
if(controlMovement(args, argsLength, message)):
pass
elif(command == "use" and useGag(args, argsLength, message)):
pass
elif(command == "buy" and buyGag(args, argsLength, message)):
pass
elif(command == "mouse" and controlMouse(args, argsLength, message)):
pass
elif (command == "show" and controlShow(args, argsLength, message)):
pass
elif (command == "hide" and controlHide(args, argsLength, message)):
pass
elif(message in keys["inputs"]):
keyPress(message)
elif(message in mouseLocations["inputs"]):
mouseLocation(message)
else:
typeMessage(message)
else:
if(time.time() - sinceLastInput >= 30.0):
messageList.append({"user": "itspeachtea", "message": "jump"})
def twitch():
def joinchat():
Loading = True
while Loading:
readbuffer_join = irc.recv(1024)
readbuffer_join = readbuffer_join.decode()
for line in readbuffer_join.split("\n")[0:-1]:
print(line)
Loading = loadingComplete(line)
def loadingComplete(line):
if("End of /NAMES list" in line):
print("Bot has joined " + CHANNEL + "'s channel!")
sendMessage("room joined")
return False
else:
return True
def sendMessage(message):
messageTemp = "PRIVMSG #" + CHANNEL + " :" + message + "\n"
irc.send(messageTemp.encode())
def getMessageDict(line):
lineParts = line.split(":")
message = lineParts[2]
user = lineParts[1].split("!")[0]
messageDict = {
"user": user,
"message": message
}
return messageDict
def ifPing(line):
return "PING" in line and "PRIVMSG" not in line
joinchat()
while True:
try:
readbuffer = irc.recv(1024).decode()
except:
readbuffer = ""
for line in readbuffer.split("\r\n"):
print(line)
if line == "":
continue
elif(ifPing(line)):
msg = "PONG tmi.twitch.tv\r\n".encode()
irc.send(msg)
print(msg)
continue
else:
msg = getMessageDict(line)
if(msg["user"] != "nightbot"):
print(msg["user"] +": " +msg["message"])
messageList.append(msg)
if __name__ == '__main__':
thread1 = threading.Thread(target = twitch)
thread2 = threading.Thread(target = gameControl)
thread1.start()
thread2.start()