From 51bde671493d0fa4644603714d9890915787cec0 Mon Sep 17 00:00:00 2001 From: hibobmaster Date: Fri, 14 Apr 2023 18:34:31 +0800 Subject: [PATCH 1/2] remove unused --- bot.py | 18 ------------------ main.py | 3 --- 2 files changed, 21 deletions(-) diff --git a/bot.py b/bot.py index d910957..825c834 100644 --- a/bot.py +++ b/bot.py @@ -653,21 +653,3 @@ class Bot: async def sync_forever(self, timeout=30000, full_state=True) -> None: await self.client.sync_forever(timeout=timeout, full_state=full_state) - - # Sync encryption keys with the server - async def sync_encryption_key(self) -> None: - if self.client.should_upload_keys: - await self.client.keys_upload() - - # Trust own devices - async def trust_own_devices(self) -> None: - await self.client.sync(timeout=30000, full_state=True) - for device_id, olm_device in self.client.device_store[ - self.user_id].items(): - logger.debug("My other devices are: " - f"device_id={device_id}, " - f"olm_device={olm_device}.") - logger.info("Setting up trust for my own " - f"device {device_id} and session key " - f"{olm_device.keys['ed25519']}.") - self.client.verify_device(olm_device) diff --git a/main.py b/main.py index 4b3a73e..113322b 100644 --- a/main.py +++ b/main.py @@ -46,9 +46,6 @@ async def main(): ) await matrix_bot.login() - await matrix_bot.sync_encryption_key() - - # await matrix_bot.trust_own_devices() await matrix_bot.sync_forever(timeout=30000, full_state=True) From bbd45e78fa9bf35b999fd5c4092d9b9dcc8371df Mon Sep 17 00:00:00 2001 From: hibobmaster Date: Fri, 14 Apr 2023 19:03:34 +0800 Subject: [PATCH 2/2] prevent blocking execution flow --- bot.py | 43 +++++++++++++++++++------------------------ main.py | 3 ++- send_message.py | 4 ++-- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/bot.py b/bot.py index 825c834..3944cf0 100644 --- a/bot.py +++ b/bot.py @@ -147,7 +147,6 @@ class Bot: if bard_token is not None: self.bardbot = Bardbot(self.bard_token) - def __del__(self): try: loop = asyncio.get_event_loop() @@ -156,7 +155,6 @@ class Bot: asyncio.set_event_loop(loop) loop.run_until_complete(self._close()) - async def _close(self): await self.session.close() @@ -196,13 +194,13 @@ class Bot: prompt = n.group(1) if self.api_key != '': try: - await self.chat(room_id, - reply_to_event_id, - prompt, - sender_id, - raw_user_message - ) - + asyncio.create_task(self.chat(room_id, + reply_to_event_id, + prompt, + sender_id, + raw_user_message + ) + ) except Exception as e: logger.error(e, exc_info=True) await send_room_message(self.client, room_id, reply_message=str(e)) @@ -214,12 +212,13 @@ class Bot: if m: prompt = m.group(1) try: - await self.gpt( + asyncio.create_task(self.gpt( room_id, reply_to_event_id, prompt, sender_id, raw_user_message ) + ) except Exception as e: logger.error(e, exc_info=True) await send_room_message(self.client, room_id, reply_message=str(e)) @@ -231,14 +230,14 @@ class Bot: prompt = b.group(1) # raw_content_body used for construct formatted_body try: - await self.bing( + asyncio.create_task(self.bing( room_id, reply_to_event_id, prompt, sender_id, raw_user_message ) - + ) except Exception as e: logger.error(e, exc_info=True) await send_room_message(self.client, room_id, reply_message=str(e)) @@ -249,7 +248,7 @@ class Bot: if i: prompt = i.group(1) try: - await self.pic(room_id, prompt) + asyncio.create_task(self.pic(room_id, prompt)) except Exception as e: logger.error(e, exc_info=True) await send_room_message(self.client, room_id, reply_message=str(e)) @@ -260,23 +259,22 @@ class Bot: if b: prompt = b.group(1) try: - await self.bard( + asyncio.create_task(self.bard( room_id, reply_to_event_id, prompt, sender_id, raw_user_message ) + ) except Exception as e: logger.error(e, exc_info=True) await send_room_message(self.client, room_id, reply_message={e}) - - # help command h = self.help_prog.match(content_body) if h: - await self.help(room_id) + asyncio.create_task(self.help(room_id)) # message_callback decryption_failure event async def decryption_failure(self, room: MatrixRoom, event: MegolmEvent) -> None: @@ -528,7 +526,6 @@ class Bot: except Exception as e: raise Exception(e) - try: text = text.strip() await send_room_message(self.client, room_id, reply_message=text, @@ -549,7 +546,6 @@ class Bot: except Exception as e: raise Exception(e) - try: text = text.strip() await send_room_message(self.client, room_id, reply_message=text, @@ -569,8 +565,7 @@ class Bot: raise Exception("Timeout error") except Exception as e: raise Exception(e) - - + try: text = text.strip() await send_room_message(self.client, room_id, reply_message=text, @@ -586,16 +581,16 @@ class Bot: response = await asyncio.to_thread(self.bardbot.ask, prompt) except Exception as e: raise Exception(e) - + try: content = str(response['content']).strip() await send_room_message(self.client, room_id, reply_message=content, - reply_to_event_id="", sender_id=sender_id, user_message=raw_user_message, markdown_formatted=self.markdown_formatted) + reply_to_event_id="", sender_id=sender_id, user_message=raw_user_message, markdown_formatted=self.markdown_formatted) except Exception as e: logger.error(e, exc_info=True) - # !pic command + async def pic(self, room_id, prompt): try: await self.client.room_typing(room_id, timeout=180000) diff --git a/main.py b/main.py index 113322b..4853a82 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,8 @@ import asyncio import json import os - +import signal +from functools import partial from bot import Bot from log import getlogger diff --git a/send_message.py b/send_message.py index 2bd2bc9..3f714c3 100644 --- a/send_message.py +++ b/send_message.py @@ -13,8 +13,8 @@ async def send_room_message(client: AsyncClient, NORMAL_BODY = content = {"msgtype": "m.text", "body": reply_message, } if reply_to_event_id == '': if markdown_formatted: - # only format message contains multiline codes - if re.search(r"```", reply_message) is not None: + # only format message contains multiline codes, *, | + if re.search(r"```|\*|\|", reply_message) is not None: content = { "msgtype": "m.text", "body": reply_message,