From 96abea15ed6c1c0f1f545cb3d59b7f6fd618f637 Mon Sep 17 00:00:00 2001 From: Georgios Atheridis Date: Tue, 17 May 2022 15:44:47 +0000 Subject: [PATCH] black --- aptbot/__main__.py | 45 ++++++++++++++++++++++++++++++++------------- aptbot/bot.py | 36 ++++++++++++++++++------------------ 2 files changed, 50 insertions(+), 31 deletions(-) diff --git a/aptbot/__main__.py b/aptbot/__main__.py index 579e08d..bfedd57 100644 --- a/aptbot/__main__.py +++ b/aptbot/__main__.py @@ -25,12 +25,15 @@ def handle_message(bot: aptbot.bot.Bot, modules: dict[str, ModuleType]): # if message.command: # print( # f"#{message.channel} ({message.command.value}) | \ -# {message.nick}: {message.value}" + # {message.nick}: {message.value}" # ) try: method = Thread( target=modules[message.channel].main, - args=(bot, message, ) + args=( + bot, + message, + ), ) except KeyError: pass @@ -44,14 +47,20 @@ def start(bot: aptbot.bot.Bot, modules: dict[str, ModuleType]): load_modules(modules) message_handler_thread = Thread( target=handle_message, - args=(bot, modules, ) + args=( + bot, + modules, + ), ) message_handler_thread.daemon = True message_handler_thread.start() for channel in modules: update_channel = Thread( target=modules[channel].start, - args=(bot, aptbot.bot.Message({}, "", None, channel, ""), ) + args=( + bot, + aptbot.bot.Message({}, "", None, channel, ""), + ), ) update_channel.daemon = True update_channel.start() @@ -60,14 +69,15 @@ def start(bot: aptbot.bot.Bot, modules: dict[str, ModuleType]): def load_modules(modules: dict[str, ModuleType]): modules.clear() channels = [ - c for c in os.listdir(CONFIG_PATH) if os.path.isdir(os.path.join(CONFIG_PATH, c)) + c + for c in os.listdir(CONFIG_PATH) + if os.path.isdir(os.path.join(CONFIG_PATH, c)) ] - channels = filter(lambda x: not x.startswith('.'), channels) + channels = filter(lambda x: not x.startswith("."), channels) for channel in channels: account_path = os.path.join(CONFIG_PATH, f"{channel}") sys.path.append(account_path) - module_path = os.path.join( - account_path, f"main.py") + module_path = os.path.join(account_path, f"main.py") spec = importlib.util.spec_from_file_location( "main", module_path, @@ -89,11 +99,13 @@ def load_modules(modules: dict[str, ModuleType]): def initialize(bot: aptbot.bot.Bot): channels = [ - c for c in os.listdir(CONFIG_PATH) if os.path.isdir(os.path.join(CONFIG_PATH, c)) + c + for c in os.listdir(CONFIG_PATH) + if os.path.isdir(os.path.join(CONFIG_PATH, c)) ] - channels = filter(lambda x: not x.startswith('.'), channels) + channels = filter(lambda x: not x.startswith("."), channels) for channel in channels: - if not channel.startswith('.'): + if not channel.startswith("."): bot.join_channel(channel) @@ -106,7 +118,13 @@ def listener(): sys.exit(1) bot.connect() modules = {} - message_loop = Thread(target=start, args=(bot, modules, )) + message_loop = Thread( + target=start, + args=( + bot, + modules, + ), + ) message_loop.daemon = True message_loop.start() s = socket.socket() @@ -139,12 +157,13 @@ def listener(): time.sleep(1) -def send(func,): +def send(func): def inner(*args, **kwargs): s = socket.socket() s.connect((LOCALHOST, PORT)) func(s, *args, **kwargs) s.close() + return inner diff --git a/aptbot/bot.py b/aptbot/bot.py index bc0533a..17877d3 100644 --- a/aptbot/bot.py +++ b/aptbot/bot.py @@ -40,13 +40,12 @@ class Bot: self._connected_channels = [] def send_command(self, command: str): - # if "PASS" not in command: - # print(f"< {command}") + if "PASS" not in command: + print(f"< {command}") self._irc.send((command + "\r\n").encode()) def connect(self): self._irc.connect((self._server, self._port)) - time.sleep(1.5) self.send_command(f"PASS oauth:{self._oauth_token}") self.send_command(f"NICK {self._nick}") self.send_command(f"CAP REQ :twitch.tv/membership") @@ -87,37 +86,37 @@ class Bot: message = Message() value_start = received_msg.find( - ':', - received_msg.find(' ', received_msg.find(' ') + 1) + ":", received_msg.find(" ", received_msg.find(" ") + 1) ) if value_start != -1: message.value = received_msg[value_start:][1:] - received_msg = received_msg[:value_start - 1] + received_msg = received_msg[: value_start - 1] - parts = received_msg.split(' ') + parts = received_msg.split(" ") for part in parts: - if part.startswith('@'): + if part.startswith("@"): part = part[1:] - for tag in part.split(';'): - tag = tag.split('=') + for tag in part.split(";"): + tag = tag.split("=") try: message.tags[tag[0]] = tag[1] except IndexError: message.tags[tag[0]] = "" - elif part.startswith(':'): + elif part.startswith(":"): part = part[1:] - if '!' in part: - message.nick = part.split('!')[0] + if "!" in part: + message.nick = part.split("!")[0] elif part in set(command.value for command in Commands): message.command = Commands(part) - elif part.startswith('#'): + elif part.startswith("#"): part = part[1:] message.channel = part - message.value = ' '.join(message.value.split()) + message.value = " ".join(message.value.split()) if not message.tags.get("reply-parent-msg-body", None): + print(message) return message rep = message.tags["reply-parent-msg-body"] @@ -134,14 +133,15 @@ class Bot: if i + 1 == len(rep): new_rep += rep[i] break - if rep[i+1] == "\\": + if rep[i + 1] == "\\": new_rep += "\\" - elif rep[i+1] == "s": + elif rep[i + 1] == "s": new_rep += " " ignore_next = True - message.tags["reply-parent-msg-body"] = ' '.join(new_rep.split()) + message.tags["reply-parent-msg-body"] = " ".join(new_rep.split()) + print(message) return message def _handle_message(self, received_msg: str) -> Message: -- 2.30.2