Fixed a bug that crashed the bot when someone replied to a message with an = symbol...
authorGeorgios Atheridis <atheridis@tutamail.com>
Sat, 11 Jun 2022 13:22:13 +0000 (13:22 +0000)
committerGeorgios Atheridis <atheridis@tutamail.com>
Sat, 11 Jun 2022 13:22:13 +0000 (13:22 +0000)
aptbot/bot.py
aptbot/constants.py
aptbot/main.py
pyproject.toml
setup.py

index 32d851efa43890dd6675f06668e34e13392c6a58..6cc5539ae2032cefe0c28c9c25952d67429c7c8f 100644 (file)
@@ -100,14 +100,14 @@ class Bot(ABCBot):
             self._send_command(command)
 
     @staticmethod
-    def _replace_escaped_space_in_tags(tag_value: str) -> str:
+    def _replace_escaped_characters_in_tags(tag_value: str) -> str:
         new_tag_value = ""
         ignore_next = False
         for i in range(len(tag_value)):
             if ignore_next:
                 ignore_next = False
                 continue
-            if not tag_value[i] == "\\":
+            if tag_value[i] != "\\":
                 new_tag_value += tag_value[i]
                 ignore_next = False
                 continue
@@ -118,13 +118,15 @@ class Bot(ABCBot):
                 new_tag_value += "\\"
             elif tag_value[i + 1] == "s":
                 new_tag_value += " "
+            elif tag_value[i + 1] == ":":
+                new_tag_value += ";"
             ignore_next = True
         return new_tag_value
 
     @staticmethod
     def _parse_message(received_msg: str) -> Message:
         split = re.search(
-            r"(?:@(.+)\s)?:(?:(?:(\w+)!\w+@\w+\.)?.+)\s(\w+)\s(?:\#(\w+)|\*)\s:?(.+)?",
+            r"(?:@(.+)\s)?:(?:(?:(\w+)!\w+@\w+\.)?.+)\s(\w+)\s(?:\#(\w+)|\*)\s?:?(.+)?",
             received_msg,
         )
 
@@ -134,9 +136,12 @@ class Bot(ABCBot):
         tags = {}
         if split[1]:
             for tag in split[1].split(";"):
-                tag_name, tag_value = tag.split("=")
-                if tag.split("=")[0] == "reply-parent-msg-body":
-                    tag_value = Bot._replace_escaped_space_in_tags(tag.split("=")[1])
+                split_tag = tag.split("=")
+                tag_name = split_tag[0]
+                tag_value = "=".join(split_tag[1:])
+                if split_tag[0] == "reply-parent-msg-body":
+                    tag_value = Bot._replace_escaped_characters_in_tags(tag_value)
+                # Remove extra whitespace
                 tags[tag_name] = " ".join(tag_value.split())
 
         nick = split[2] if split[2] else ""
@@ -163,6 +168,9 @@ class Bot(ABCBot):
         if received_msg == "PING :tmi.twitch.tv":
             self._send_command("PONG :tmi.twitch.tv")
             return Message()
+        elif received_msg == ":tmi.twitch.tv RECONNECT":
+            logger.warning("Restarting twitch connection")
+            self._restart_connection()
         elif not received_msg:
             return Message()
         return Bot._parse_message(received_msg)
@@ -213,7 +221,7 @@ class Bot(ABCBot):
 
     def _restart_connection(self):
         self.disconnect()
-        time.sleep(5)
+        time.sleep(2)
         self._connect()
         self.join_channels(self._connected_channels)
         time.sleep(2)
index 6916bad7adbeda38cea038aba82c5077ecca3c15..3e0b812393394d403d58e4ec26d2cf67a6f345bb 100644 (file)
@@ -1,4 +1,5 @@
 import os
+import sys
 
 if os.name == "posix":
     if "XDG_CONFIG_HOME" in os.environ and "XDG_CACHE_HOME" in os.environ:
@@ -39,7 +40,7 @@ LOGGING_DICT = {
     "handlers": {
         "console": {
             "class": "logging.StreamHandler",
-            "level": "DEBUG",
+            "level": "INFO",
             "formatter": "simple",
             "stream": "ext://sys.stdout",
         },
index bd164e2ecb574c3d8658b052577bf161f540bffc..0b2d38180f059aedbff2903cc119e60c74194227 100644 (file)
@@ -16,8 +16,14 @@ from dotenv import load_dotenv
 from . import args_logic
 from .args import parse_arguments
 from .bot import Bot, Message
-from .constants import (CONFIG_LOGS, CONFIG_PATH, LOCALHOST, LOGGING_DICT,
-                        MAIN_FILE_NAME, PORT)
+from .constants import (
+    CONFIG_LOGS,
+    CONFIG_PATH,
+    LOCALHOST,
+    LOGGING_DICT,
+    MAIN_FILE_NAME,
+    PORT,
+)
 
 logging.config.dictConfig(LOGGING_DICT)
 logger = logging.getLogger(__name__)
@@ -181,6 +187,8 @@ def listener():
     )
     message_loop.start()
     s = socket.socket()
+    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+    logger.debug("setsockopt")
     s.bind((LOCALHOST, PORT))
     s.listen(5)
 
index ca288129ef7351c9b9b6e8b153e3adb1483354d0..83ebb5efa8f5173bb5a7eb847e2256d4619934b6 100644 (file)
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
 
 [project]
 name = "aptbot"
-version = "0.2.1"
+version = "0.2.2"
 description = "A chatbot for twitch.tv"
 readme = "README.md"
 authors = [{ name = "Georgios Atheridis", email = "atheridis@tutamail.com" }]
index fa8aaef8f768cf03ce4a540bc5493dfc962afec5..09a39c5ef9b467b0d3bbd62ac8993d2dc932d626 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
 
 setuptools.setup(
     name="aptbot",
-    version="0.2.1",
+    version="0.2.2",
     author="Georgios Atheridis",
     author_email="atheridis@tutamail.com",
     description="A chatbot for twitch.tv",