From bcb564fc3695469bfd3a4ea113ff5322bd35b4d9 Mon Sep 17 00:00:00 2001 From: Cyberes Date: Mon, 8 Apr 2024 12:13:36 -0600 Subject: [PATCH] fix bot replying multiple times --- README.md | 4 ++-- matrix_gpt/__init__.py | 2 +- matrix_gpt/callbacks.py | 9 ++++++--- matrix_gpt/{matrix.py => matrix_helper.py} | 0 4 files changed, 9 insertions(+), 6 deletions(-) rename matrix_gpt/{matrix.py => matrix_helper.py} (100%) diff --git a/README.md b/README.md index 369d944..fd78789 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,8 @@ Use `!matrixgpt` to view the bot's help. The bot also responds to `!bots`.
- Don't try to use two bots in the same thread. -- You can DM a bot for a private chat. Don't use the trigger prefix in a DM. -- The bot will move its read marker when a new message is sent in the room. +- You can DM a bot for a private chat. +- The bot will move its read marker whenever a message is sent in the room.
diff --git a/matrix_gpt/__init__.py b/matrix_gpt/__init__.py index 261eeec..26aada3 100644 --- a/matrix_gpt/__init__.py +++ b/matrix_gpt/__init__.py @@ -1 +1 @@ -from .matrix import MatrixClientHelper \ No newline at end of file +from .matrix_helper import MatrixClientHelper \ No newline at end of file diff --git a/matrix_gpt/callbacks.py b/matrix_gpt/callbacks.py index 1fd0f66..d7039ee 100644 --- a/matrix_gpt/callbacks.py +++ b/matrix_gpt/callbacks.py @@ -7,7 +7,7 @@ from nio import (AsyncClient, InviteMemberEvent, MatrixRoom, MegolmEvent, RoomMe from .chat_functions import check_authorized, is_thread, check_command_prefix from .config import global_config from .handle_actions import do_reply_msg, do_reply_threaded_msg, do_join_channel, sound_off -from .matrix import MatrixClientHelper +from .matrix_helper import MatrixClientHelper class MatrixBotCallbacks: @@ -16,6 +16,7 @@ class MatrixBotCallbacks: self.client: AsyncClient = client.client self.logger = logging.getLogger('MatrixGPT').getChild('MatrixBotCallbacks') self.startup_ts = time.time() * 1000 + self.seen_messages = set() async def handle_message(self, room: MatrixRoom, requestor_event: RoomMessageText) -> None: """ @@ -23,7 +24,6 @@ class MatrixBotCallbacks: """ # Mark all messages as read. mark_read_task = asyncio.create_task(self.client.room_read_markers(room.room_id, requestor_event.event_id, requestor_event.event_id)) - msg = requestor_event.body.strip().strip('\n') if msg == "** Unable to decrypt: The sender's device has not sent us the keys for this message. **": self.logger.debug(f'Unable to decrypt event "{requestor_event.event_id} in room {room.room_id}') @@ -36,6 +36,9 @@ class MatrixBotCallbacks: self.logger.debug(f'Message from {requestor_event.sender} in {room.room_id} --> "{msg}"') await sound_off(room, requestor_event, self.client_helper) return + if requestor_event.event_id in self.seen_messages: + return + self.seen_messages.add(requestor_event.event_id) command_activated, sent_command_prefix, command_info = check_command_prefix(msg) if not command_activated and is_thread(requestor_event): @@ -43,7 +46,7 @@ class MatrixBotCallbacks: self.logger.debug(f'Message from {requestor_event.sender} in {room.room_id} --> "{msg}"') # Start the task in the background and don't wait for it here or else we'll block everything. task = asyncio.create_task(do_reply_threaded_msg(self.client_helper, room, requestor_event)) - elif (command_activated or room.member_count == 2) and not is_thread(requestor_event): + elif command_activated and not is_thread(requestor_event): # Everything else self.logger.debug(f'Message from {requestor_event.sender} in {room.room_id} --> "{msg}"') allowed_to_chat = command_info.allowed_to_chat + global_config['allowed_to_chat'] diff --git a/matrix_gpt/matrix.py b/matrix_gpt/matrix_helper.py similarity index 100% rename from matrix_gpt/matrix.py rename to matrix_gpt/matrix_helper.py