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.
This commit is contained in:
Kumi 2023-11-09 12:29:04 +01:00
parent 2e6c07eb22
commit 72340095f9
Signed by: kumi
GPG key ID: ECBCC9082395383F
2 changed files with 7 additions and 3 deletions

View file

@ -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("!"):

View file

@ -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)