From 72340095f97b93091b7fe863a23605437f7a3463 Mon Sep 17 00:00:00 2001 From: Kumi Date: Thu, 9 Nov 2023 12:29:04 +0100 Subject: [PATCH] Fix bot command prefix recognition and handle ignore bot commands. - Fixed bot command prefix recognition to include prefixes starting with an asterisk (= edited messages) - Added handling of ignoring bot commands in the '_last_n_messages' method. --- src/gptbot/callbacks/message.py | 2 +- src/gptbot/classes/bot.py | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/gptbot/callbacks/message.py b/src/gptbot/callbacks/message.py index 5f5a176..d5c8fa4 100644 --- a/src/gptbot/callbacks/message.py +++ b/src/gptbot/callbacks/message.py @@ -33,7 +33,7 @@ async def message_callback(room: MatrixRoom | str, event: RoomMessageText | Mego if event.sender == bot.matrix_client.user_id: bot.logger.log("Message is from bot itself - ignoring") - elif event.body.startswith("!gptbot"): + elif event.body.startswith("!gptbot") or event.body.startswith("* !gptbot"): await bot.process_command(room, event) elif event.body.startswith("!"): diff --git a/src/gptbot/classes/bot.py b/src/gptbot/classes/bot.py index 9800581..101d17d 100644 --- a/src/gptbot/classes/bot.py +++ b/src/gptbot/classes/bot.py @@ -195,7 +195,7 @@ class GPTBot: return user_id - async def _last_n_messages(self, room: str | MatrixRoom, n: Optional[int]): + async def _last_n_messages(self, room: str | MatrixRoom, n: Optional[int], ignore_bot_commands: bool = True): messages = [] n = n or self.max_messages room_id = room.room_id if isinstance(room, MatrixRoom) else room @@ -233,7 +233,7 @@ class GPTBot: if event.body.startswith("!gptbot ignoreolder"): break if (not event.body.startswith("!")) or ( - event.body.startswith("!gptbot") + event.body.startswith("!gptbot") and not ignore_bot_commands ): messages.append(event) @@ -319,6 +319,10 @@ class GPTBot: f"Received command {event.body} from {event.sender} in room {room.room_id}", "debug", ) + + if event.body.startswith("* "): + event.body = event.body[2:] + command = event.body.split()[1] if event.body.split()[1:] else None await COMMANDS.get(command, COMMANDS[None])(room, event, self)