-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlionel_bot.py
159 lines (128 loc) · 4.71 KB
/
lionel_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
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
# importing the requests library
import requests
import json
import socket, string
import time
import re
import threading
import chat_plays_go as cpg
import queue
import time
CHANNEL = "t4rquin"
USERNAME = # Your bots nick
OAUTH = # OAuth
class Chatbot:
def __init__(self):
# Some basic variables used to configure the bot
self.server = "irc.chat.twitch.tv" # Server
self.channel = "#" + CHANNEL # Channel
self.botnick = USERNAME
self.password = OAUTH
self.connected = False
self.start = time.time()
self.streamers = []
self.connect()
self.last_ping = time.time()
self.cpg_flag = 1
if self.cpg_flag:
self.cpgUi = cpg.ChatPlaysGoUi()
# self.joinchan()
def connect(self):
self.ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.ircsock.connect((self.server, 6667)) # Here we connect to the server using the port 6667
self.send_data("PASS " + self.password)
self.send_data("NICK "+ self.botnick)
def send_data(self,msg):
msg = msg + "\r\n"
self.ircsock.send(msg.encode())
def check_buffer(self):
buffer = self.ircsock.recv(2048)
buffer = buffer.decode()
buffer = buffer.rstrip("\r\n")
msg = buffer.split()
if not self.connected:
if buffer.endswith("End of /NAMES list"):
print("Connected to channel %s" % self.channel)
self.connected = True
if not msg:
print("ERROR in channel %s" % self.channel)
self.connected = False
self.connect()
# self.joinchan()
elif msg[0] == "PING":
self.send_data("PONG %s" % msg[1])
print("PONG %s" % self.channel)
def ping(self): # respond to server Pings.
self.send_data("PONG :pingis\n")
def sendmsg(self, msg): # sends messages to the channel.
self.send_data("PRIVMSG "+ self.channel +" :"+ msg +"\n")
def joinchan(self, chan): # join channel(s).
self.send_data("JOIN "+ "#" + chan +"\n")
print("Joining channel: " + chan)
def partchan(self, chan): # part channel(s).
self.send_data("PART "+ "#" + chan +"\n")
def whisper(self, msg, user): # whisper a user
self.send_data("PRIVMSG " + user + ' :' + msg.strip('\n\r') + '\n')
def getUserAndMessage(self, buffer):
msg = buffer.split()
user = re.findall("(?<=@)(.*)(?=.tmi.twitch)", msg[0])
message = (" ").join(msg[3:])
return user[0], message[1:]
def parseMessage(self, buffer):
user, msg = self.getUserAndMessage(buffer)
print(user + "::" + msg)
if self.cpg_flag:
list_coords = self.cpgUi.getCoordsFromMsg(msg)
if list_coords:
self.cpgUi.addToHighlightQueue(list_coords)
if self.cpgUi.isCoordinate(msg) or self.cpgUi.isCommand(msg):
self.cpgUi.addToQueue(user, msg)
def bot_main(self):
self.joinchan(self.channel)
# start infinite loop to continually check for and receive new info from server
while 1:
if time.time() - self.last_ping > 500:
print("sending ping")
self.send_data("PING :tmi.twitch.tv")
self.last_ping = time.time()
try:
buffer = self.ircsock.recv(2048)
buffer = buffer.decode()
buffer = buffer.rstrip("\r\n")
msg = buffer.split()
except:
continue
try:
self.parseMessage(buffer)
except:
pass
if buffer.endswith(" :End of /NAMES list"):
buffer.rstrip(" :End of /NAMES list")
msg = buffer.split()
print("Connected to channel %s" % msg[-5])
elif msg[0] == "PING":
self.send_data("PONG %s" % msg[1])
print("PONG %s" % msg[1])
self.last_ping = time.time()
#print(time.time() - self.start)
def sendmsg_main(self):
time.sleep(1)
while (1):
msg = self.cpgUi.getChatbotMsgFromQueue()
if msg:
self.sendmsg(msg)
time.sleep(0.2)
def time_elapsed(self):
return time.time() - self.start
def run_bot(self):
if self.cpg_flag:
cb_sendmsg_thread = threading.Thread(target=cb.sendmsg_main)
cb_sendmsg_thread.start()
self.bot_main()
if __name__ == "__main__":
time.sleep(1)
cb = Chatbot()
cb_thread = threading.Thread(target=cb.run_bot)
cb_thread.start()
if cb.cpg_flag:
cb.cpgUi.run()