From 98efc6a8710bffc09ca28ba555ebde5c79381598 Mon Sep 17 00:00:00 2001 From: nebula Date: Sun, 15 Dec 2024 15:32:00 -0600 Subject: [PATCH] change socket reading method --- main.py | 76 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/main.py b/main.py index 277b441..ec6fb59 100644 --- a/main.py +++ b/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__":