From: Georgios Atheridis Date: Sat, 11 Jun 2022 13:22:13 +0000 (+0000) Subject: Fixed a bug that crashed the bot when someone replied to a message with an = symbol... X-Git-Url: https://git.atheridis.org/?a=commitdiff_plain;h=296c2b730b342268c7b92725d9954fe9eef1ec12;p=personal%2Faptbot.git Fixed a bug that crashed the bot when someone replied to a message with an = symbol. Implemented :tmi.twitch.tv RECONNECT to restart the bot --- diff --git a/aptbot/bot.py b/aptbot/bot.py index 32d851e..6cc5539 100644 --- a/aptbot/bot.py +++ b/aptbot/bot.py @@ -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) diff --git a/aptbot/constants.py b/aptbot/constants.py index 6916bad..3e0b812 100644 --- a/aptbot/constants.py +++ b/aptbot/constants.py @@ -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", }, diff --git a/aptbot/main.py b/aptbot/main.py index bd164e2..0b2d381 100644 --- a/aptbot/main.py +++ b/aptbot/main.py @@ -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) diff --git a/pyproject.toml b/pyproject.toml index ca28812..83ebb5e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" }] diff --git a/setup.py b/setup.py index fa8aaef..09a39c5 100644 --- 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",