Browse Source

per room config

master
Hendrik Langer 2 years ago
parent
commit
d1e27be587
  1. 34
      matrix_pygmalion_bot/core.py

34
matrix_pygmalion_bot/core.py

@ -12,6 +12,7 @@ import aiofiles.os
import magic import magic
from PIL import Image from PIL import Image
import re import re
import json
from .helpers import Event from .helpers import Event
from .chatlog import BotChatHistory from .chatlog import BotChatHistory
@ -38,6 +39,9 @@ class Callbacks(object):
async def message_cb(self, room: MatrixRoom, event: RoomMessageText) -> None: async def message_cb(self, room: MatrixRoom, event: RoomMessageText) -> None:
if not hasattr(event, 'body'): if not hasattr(event, 'body'):
return 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 relates_to = None
if 'm.relates_to' in event.source["content"]: if 'm.relates_to' in event.source["content"]:
relates_to = event.source["content"]['m.relates_to']["event_id"] relates_to = event.source["content"]['m.relates_to']["event_id"]
@ -95,7 +99,7 @@ class Callbacks(object):
return return
elif event.body.startswith('!begin'): elif event.body.startswith('!begin'):
self.bot.chat_history.room(room.display_name).clear() 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.write_conf2(self.bot.name)
await self.bot.send_message(self.client, room.room_id, self.bot.greeting) await self.bot.send_message(self.client, room.room_id, self.bot.greeting)
return return
@ -197,8 +201,9 @@ class ChatBot(object):
self.scenario = None self.scenario = None
self.greeting = None self.greeting = None
self.events = [] self.events = []
self.tick = 0 self.global_tick = 0
self.chat_history = None self.chat_history = None
self.room_config = {}
if STORE_PATH and not os.path.isdir(STORE_PATH): if STORE_PATH and not os.path.isdir(STORE_PATH):
os.mkdir(STORE_PATH) os.mkdir(STORE_PATH)
@ -213,10 +218,12 @@ class ChatBot(object):
try: try:
while True: while True:
await asyncio.sleep(60) await asyncio.sleep(60)
for room_id in self.room_config.keys():
for event in self.events: for event in self.events:
event.loop(self, self.tick) event.loop(self, self.room_config[room_id]["tick"])
self.tick += 1 self.room_config[room_id]["tick"] += 1
if self.tick % 10 == 0: self.global_tick += 1
if self.global_tick % 10 == 0:
await self.write_conf2(self.name) await self.write_conf2(self.name)
finally: finally:
await self.write_conf2(self.name) await self.write_conf2(self.name)
@ -259,18 +266,14 @@ class ChatBot(object):
self.not_synced = False self.not_synced = False
async def read_conf2(self, section): async def read_conf2(self, section):
config2 = configparser.ConfigParser() if not os.path.isfile("bot.conf2"):
config2.read('bot.conf2') return
if config2.has_section(section) and config2.has_option(section, 'current_tick'): with open("bot.conf2", "r") as f:
self.tick = int(config2[section]['current_tick']) self.room_config = json.load(f)
async def write_conf2(self, section): async def write_conf2(self, section):
config2 = configparser.ConfigParser() with open("bot.conf2", "w") as f:
config2.read('bot.conf2') json.dump(self.room_config, f)
config2[section] = {}
config2[section]['current_tick'] = str(self.tick)
with open('bot.conf2', 'w') as configfile:
config2.write(configfile)
async def send_message(self, client, room_id, message, reply_to=None, original_message=None): async def send_message(self, client, room_id, message, reply_to=None, original_message=None):
content={"msgtype": "m.text", "body": message} content={"msgtype": "m.text", "body": message}
@ -358,7 +361,6 @@ async def main() -> None:
if config.has_option('DEFAULT', 'runpod_api_key'): if config.has_option('DEFAULT', 'runpod_api_key'):
bot.runpod_api_key = config['DEFAULT']['runpod_api_key'] bot.runpod_api_key = config['DEFAULT']['runpod_api_key']
await bot.read_conf2(section) await bot.read_conf2(section)
#await bot.write_conf2(section)
bots.append(bot) bots.append(bot)
await bot.login() await bot.login()
print("gather") print("gather")

Loading…
Cancel
Save