change socket reading method
parent
20f97a4e8a
commit
98efc6a871
76
main.py
76
main.py
|
@ -286,41 +286,45 @@ class IRCBot():
|
|||
|
||||
def ping_pong(self):
|
||||
while True:
|
||||
sleep(2)
|
||||
response = self.s.recv(8192).decode("UTF-8")
|
||||
if not response:
|
||||
char = self.s.recv(1).decode("UTF-8")
|
||||
if not char:
|
||||
exit(f"{self.nick}: no response from IRC server")
|
||||
split = response.split("\r\n")
|
||||
for line in split:
|
||||
line = line.strip()
|
||||
if line.startswith("PING"):
|
||||
pong = "PONG " + line[5:]
|
||||
self.sendline(pong)
|
||||
continue
|
||||
channel_search = channel_re.search(line)
|
||||
if not channel_search:
|
||||
continue
|
||||
name_search = name_re.search(line)
|
||||
channel = channel_search.group(1)
|
||||
if name_search:
|
||||
name = name_search.group(1)
|
||||
else:
|
||||
name = None
|
||||
if name and not channel.startswith("#"):
|
||||
channel = name
|
||||
try:
|
||||
message_body = line[line.index(" :") + 2:]
|
||||
except Exception as e:
|
||||
print(e)
|
||||
message_body = ""
|
||||
for command, callback in self.commands:
|
||||
if line.lower().endswith(command):
|
||||
result = callback(channel, name)
|
||||
if result:
|
||||
self.send(channel, result)
|
||||
if message_body:
|
||||
for callback in self.searchers:
|
||||
callback(message_body)
|
||||
line = ""
|
||||
while char:
|
||||
if char == "\n":
|
||||
break
|
||||
elif char != "\r":
|
||||
line += char
|
||||
char = self.s.recv(1).decode("UTF-8")
|
||||
line = line.strip()
|
||||
if line.startswith("PING"):
|
||||
pong = "PONG " + line[5:]
|
||||
self.sendline(pong)
|
||||
continue
|
||||
channel_search = channel_re.search(line)
|
||||
if not channel_search:
|
||||
continue
|
||||
channel = channel_search.group(1)
|
||||
name_search = name_re.search(line)
|
||||
if name_search:
|
||||
name = name_search.group(1)
|
||||
else:
|
||||
name = None
|
||||
if name and not channel.startswith("#"):
|
||||
channel = name
|
||||
try:
|
||||
message_body = line[line.index(" :") + 2:]
|
||||
except Exception as e:
|
||||
print(e)
|
||||
message_body = ""
|
||||
for command, callback in self.commands:
|
||||
if line.lower().endswith(command):
|
||||
result = callback(channel, name)
|
||||
if result:
|
||||
self.send(channel, result)
|
||||
if message_body:
|
||||
for callback in self.searchers:
|
||||
callback(message_body)
|
||||
|
||||
def run():
|
||||
bot = IRCBot(
|
||||
|
@ -329,7 +333,7 @@ def run():
|
|||
helptext,
|
||||
[ # endswith commands
|
||||
("!help", post_help),
|
||||
("!rollcall", post_help)
|
||||
("!rollcall", post_help),
|
||||
("!trivia", post_question),
|
||||
("!aitrivia", post_ai_question),
|
||||
("!trscores", post_top_scores),
|
||||
|
@ -346,7 +350,7 @@ def run():
|
|||
)
|
||||
|
||||
while True:
|
||||
sleep(2)
|
||||
sleep(0.5)
|
||||
bot.ping_pong()
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Reference in New Issue