-
Notifications
You must be signed in to change notification settings - Fork 4
/
poke.py
116 lines (100 loc) · 2.9 KB
/
poke.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
import socket
import pyautogui
import threading
# Currently only plays the game from Twitch chat, we dont need that
# Instead get the info from the API
# credentials
SERVER = "irc.twitch.tv"
PORT = 6667
PASS = "oauth:4hl7eubwdid2lh7cuchht73vumapxd"
BOT = "TwitchPokeBot"
CHANNEL = "epixxar"
OWNER = "epixxar"
message = ""
irc = socket.socket()
irc.connect((SERVER, PORT))
irc.send(("PASS " + PASS + "\n" +
"NICK " + BOT + "\n" +
"JOIN #" + CHANNEL + "\n" ).encode())
#twitch requires a bytes-like object, encode is used for that
BINDINGS = {
'up': 'up',
'down': 'down',
'left': 'left',
'right': 'right',
'abut': 'x',
'bbut': 'z',
'ltrig': 'a',
'rtrig': 's',
'start': 'return',
'select': 'backspace'
}
def game_control():
global message
while True:
for key in BINDINGS:
if message == key:
print(f"Simulating ${BINDINGS[key]}")
pyautogui.keyDown(BINDINGS[key])
message = ''
pyautogui.keyUp(BINDINGS[key])
else:
pass
def twitch():
def join_chat():
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 = loading_complete(line)
def loading_complete(line):
if("End of /NAMES list" in line):
print("Bot has joined " + CHANNEL + "'s channel")
send_message(irc, "Joined to chat room")
return False
else:
return True
def send_message(irc, message):
message_temp = "PRIVMSG #" + CHANNEL + " :" + message
irc.send((message_temp + "\n").encode())
def get_user(line):
separate = line.split(":", 2)
user = separate[1].split("!", 1)[0]
return user
def get_message(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
join_chat()
while True:
try:
read_buffer = irc.recv(1024).decode()
except:
read_buffer = ""
for line in read_buffer.split("\r\n"):
if line == "":
continue
elif "PING" in line and console(line):
msg = "PONG tmi.twitch.tv\r\n".encode()
irc.send(msg)
print(msg)
continue
else:
user = get_user(line)
message = get_message(line)
print(user + ": "+ message)
if __name__ == "__main__":
t1 = threading.Thread(target = twitch)
t1.start()
t2 = threading.Thread(target = game_control)
t2.start()