-
Notifications
You must be signed in to change notification settings - Fork 1
/
Twitch Bot.py
132 lines (109 loc) · 2.59 KB
/
Twitch Bot.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
import socket
import pyautogui
import threading
SERVER = "irc.twitch.tv"
PORT = 6667
PASS = ""
BOT = "TwitchBot"
CHANNEL = "frosthazard"
message = ""
irc = socket.socket()
irc.connect((SERVER, PORT))
irc.send(("PASS " + PASS + "\n" +
"NICK " + BOT + "\n" +
"JOIN #" + CHANNEL + "\n").encode())
def gamecontrol():
global message
while True:
if message != "":
if "omhoog" == message.lower():
pyautogui.keyDown('up')
message = ""
pyautogui.keyUp('up')
elif "beneden" == message.lower():
pyautogui.keyDown('down')
message = ""
pyautogui.keyUp('down')
elif "links" == message.lower():
pyautogui.keyDown('left')
message = ""
pyautogui.keyUp('left')
elif "rechts" == message.lower():
pyautogui.keyDown('right')
message = ""
pyautogui.keyUp('right')
elif "aknop" == message.lower():
pyautogui.keyDown('s')
message = ""
pyautogui.keyUp('s')
elif "bknop" == message.lower():
pyautogui.keyDown('a')
message = ""
pyautogui.keyUp('a')
elif "start" == message.lower():
pyautogui.keyDown('enter')
message = ""
pyautogui.keyUp('enter')
else:
message = ""
pass
else:
pass
def twitch():
def joinchat():
Loading = True
while Loading:
readbufferJoin = irc.recv(1024)
readbufferJoin = readbufferJoin.decode()
for line in readbufferJoin.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!")
seandMessage(irc, "Bot joined the chat room")
return False
else:
return True
def seandMessage(irc, message):
messageTemp = "PRIVMSG #" + CHANNEL + " :" + message
irc.send((messageTemp + "\n").encode())
def getUser(line):
separate = line.split(":", 2)
user = separate[1].split("!", 1)[0]
return user
def getMessage(line):
global message
try:
message = (line.split(":",2))[2]
except:
message = ""
return message
def Console(line):
if "PRIVMSG" in line:
return False
else:
return True
joinchat()
while True:
try:
readbuffer = irc.recv(1024).decode()
except:
readbuffer = ""
for line in readbuffer.split("\r\n"):
if line == "":
continue
elif "PING" in line and Console(line):
msgg = "PONG tmi.twitch.tv\r\n".encode()
irc.send(msgg)
print(msgg)
continue
else:
user = getUser(line)
message = getMessage(line)
print(user + ": " + message)
if __name__ == '__main__':
t1 = threading.Thread(target = twitch)
t1.start()
t2 = threading.Thread(target = gamecontrol)
t2.start()