From d1e27be58797f5992d790d606f392bf925eec4e9 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Sun, 2 Apr 2023 20:46:47 +0200 Subject: [PATCH] per room config --- matrix_pygmalion_bot/core.py | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/matrix_pygmalion_bot/core.py b/matrix_pygmalion_bot/core.py index 436d533..62b5692 100644 --- a/matrix_pygmalion_bot/core.py +++ b/matrix_pygmalion_bot/core.py @@ -12,6 +12,7 @@ import aiofiles.os import magic from PIL import Image import re +import json from .helpers import Event from .chatlog import BotChatHistory @@ -38,6 +39,9 @@ class Callbacks(object): async def message_cb(self, room: MatrixRoom, event: RoomMessageText) -> None: if not hasattr(event, 'body'): return + if not room.room_id in self.bot.room_config: + self.bot.room_config[room.room_id] = {} + self.bot.room_config[room.room_id]["tick"] = 0 relates_to = None if 'm.relates_to' in event.source["content"]: relates_to = event.source["content"]['m.relates_to']["event_id"] @@ -95,7 +99,7 @@ class Callbacks(object): return elif event.body.startswith('!begin'): self.bot.chat_history.room(room.display_name).clear() - self.bot.tick = 0 + self.bot.room_config[room.room_id]["tick"] = 0 await self.bot.write_conf2(self.bot.name) await self.bot.send_message(self.client, room.room_id, self.bot.greeting) return @@ -197,8 +201,9 @@ class ChatBot(object): self.scenario = None self.greeting = None self.events = [] - self.tick = 0 + self.global_tick = 0 self.chat_history = None + self.room_config = {} if STORE_PATH and not os.path.isdir(STORE_PATH): os.mkdir(STORE_PATH) @@ -213,10 +218,12 @@ class ChatBot(object): try: while True: await asyncio.sleep(60) - for event in self.events: - event.loop(self, self.tick) - self.tick += 1 - if self.tick % 10 == 0: + for room_id in self.room_config.keys(): + for event in self.events: + event.loop(self, self.room_config[room_id]["tick"]) + self.room_config[room_id]["tick"] += 1 + self.global_tick += 1 + if self.global_tick % 10 == 0: await self.write_conf2(self.name) finally: await self.write_conf2(self.name) @@ -259,18 +266,14 @@ class ChatBot(object): self.not_synced = False async def read_conf2(self, section): - config2 = configparser.ConfigParser() - config2.read('bot.conf2') - if config2.has_section(section) and config2.has_option(section, 'current_tick'): - self.tick = int(config2[section]['current_tick']) + if not os.path.isfile("bot.conf2"): + return + with open("bot.conf2", "r") as f: + self.room_config = json.load(f) async def write_conf2(self, section): - config2 = configparser.ConfigParser() - config2.read('bot.conf2') - config2[section] = {} - config2[section]['current_tick'] = str(self.tick) - with open('bot.conf2', 'w') as configfile: - config2.write(configfile) + with open("bot.conf2", "w") as f: + json.dump(self.room_config, f) async def send_message(self, client, room_id, message, reply_to=None, original_message=None): content={"msgtype": "m.text", "body": message} @@ -358,7 +361,6 @@ async def main() -> None: if config.has_option('DEFAULT', 'runpod_api_key'): bot.runpod_api_key = config['DEFAULT']['runpod_api_key'] await bot.read_conf2(section) - #await bot.write_conf2(section) bots.append(bot) await bot.login() print("gather")