From 179005a562a8683d036c732cab9473797e078585 Mon Sep 17 00:00:00 2001 From: Kumi Date: Fri, 23 Aug 2024 19:06:49 +0200 Subject: [PATCH] fix: add room check to prevent processing errors Updated the method to include a room parameter, ensuring that message processing functions only when a room is provided. This prevents errors when trying to download and process media files, improving stability and avoiding unnecessary exceptions. --- src/gptbot/classes/ai/openai.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/gptbot/classes/ai/openai.py b/src/gptbot/classes/ai/openai.py index ffe910a..2aabbb0 100644 --- a/src/gptbot/classes/ai/openai.py +++ b/src/gptbot/classes/ai/openai.py @@ -19,6 +19,7 @@ from nio import ( RoomMessageFile, RoomMessageImage, RoomMessageVideo, + Event, ) from ..logging import Logger @@ -169,7 +170,11 @@ class OpenAI(BaseAI): return False async def prepare_messages( - self, event, messages: List[Dict[str, str]], system_message=None + self, + event: Event, + messages: List[Dict[str, str]], + system_message=None, + room=None, ) -> List[Any]: chat_messages = [] @@ -201,7 +206,7 @@ class OpenAI(BaseAI): else "user" ) if message == event or (not message.event_id == event.event_id): - if self.room_uses_stt(event.room_id): + if room and self.room_uses_stt(room): try: download = await self.bot.download_file( message.url, raise_error=True @@ -319,9 +324,9 @@ class OpenAI(BaseAI): ) except Exception as e: - if isinstance(e, DownloadException): + if room and isinstance(e, DownloadException): self.bot.send_message( - event.room_id, + room, f"Could not process image due to download error: {e.args[0]}", True, ) @@ -379,9 +384,9 @@ class OpenAI(BaseAI): ) except Exception as e: - if isinstance(e, DownloadException): + if room and isinstance(e, DownloadException): self.bot.send_message( - event.room_id, + room, f"Could not process video due to download error: {e.args[0]}", True, )