fix bot replying multiple times

This commit is contained in:
Cyberes 2024-04-08 12:13:36 -06:00
parent 27616867f5
commit bcb564fc36
4 changed files with 9 additions and 6 deletions

View File

@ -32,8 +32,8 @@ Use `!matrixgpt` to view the bot's help. The bot also responds to `!bots`.
<br>
- 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.
<br>

View File

@ -1 +1 @@
from .matrix import MatrixClientHelper
from .matrix_helper import MatrixClientHelper

View File

@ -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']