From 3f9ce82d4afbf71d07d3b485abe1d191fe98cac1 Mon Sep 17 00:00:00 2001 From: Georgios Atheridis Date: Sat, 2 Apr 2022 07:32:46 +0300 Subject: [PATCH] allowing main for each channel to be initiated, without needed a message from chat --- aptbot/__main__.py | 50 ++++++++++++++++++++++++++++++++++++++-------- aptbot/bot.py | 3 +-- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/aptbot/__main__.py b/aptbot/__main__.py index e91adc9..40a5879 100644 --- a/aptbot/__main__.py +++ b/aptbot/__main__.py @@ -16,8 +16,7 @@ from aptbot import * load_dotenv() -def loop(bot: aptbot.bot.Bot, modules: dict[str, ModuleType]): - load_modules(modules) +def handle_message(bot: aptbot.bot.Bot, modules: dict[str, ModuleType]): while True: messages = bot.receive_messages() for message in messages: @@ -38,7 +37,43 @@ def loop(bot: aptbot.bot.Bot, modules: dict[str, ModuleType]): else: method.daemon = True method.start() - time.sleep(0.01) + time.sleep(0.1) + + +def update_channel_events(bot: aptbot.bot.Bot, message: aptbot.bot.Message, modules: dict[str, ModuleType]): + try: + method = Thread( + target=modules[message.channel].main, + args=(bot, message, ) + ) + except KeyError: + pass + else: + method.daemon = True + method.start() + + +def start(bot: aptbot.bot.Bot, modules: dict[str, ModuleType]): + load_modules(modules) + message_handler_thread = Thread( + target=handle_message, + args=(bot, modules, ) + ) + message_handler_thread.daemon = True + message_handler_thread.start() + while True: + for channel, _ in modules: + update_channel_thread = Thread( + target=update_channel_events, + args=( + bot, + aptbot.bot.Message({}, "", None, channel, ""), + modules, + ) + ) + update_channel_thread.daemon = True + update_channel_thread.start() + time.sleep(10) def load_modules(modules: dict[str, ModuleType]): @@ -83,15 +118,14 @@ def initialize(bot: aptbot.bot.Bot): def listener(): NICK = os.getenv("APTBOT_NICK") - OAUTH = os.getenv("APTBOT_OAUTH") - CLIENT_ID = os.getenv("APTBOT_CLIENT_ID") - if NICK and OAUTH and CLIENT_ID: - bot = aptbot.bot.Bot(NICK, OAUTH, CLIENT_ID) + OAUTH = os.getenv("APTBOT_PASS") + if NICK and OAUTH: + bot = aptbot.bot.Bot(NICK, OAUTH) else: sys.exit(1) bot.connect() modules = {} - message_loop = Thread(target=loop, args=(bot, modules, )) + message_loop = Thread(target=start, args=(bot, modules, )) message_loop.daemon = True message_loop.start() s = socket.socket() diff --git a/aptbot/bot.py b/aptbot/bot.py index 85b018c..a7aee76 100644 --- a/aptbot/bot.py +++ b/aptbot/bot.py @@ -30,13 +30,12 @@ class Message: class Bot: - def __init__(self, nick: str, oauth_token: str, client_id: str): + def __init__(self, nick: str, oauth_token: str): self._irc = socket.socket() self._server = "irc.chat.twitch.tv" self._port = 6667 self._nick = nick self._oauth_token = oauth_token - self._client_id = client_id self._connected_channels = [] def send_command(self, command: str): -- 2.30.2