Skip to content

Commit c964c08

Browse files
committed
Fix for custom prefix per commands (might need some optimization)
1 parent d5d337e commit c964c08

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

matrix/bot.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,30 @@ async def _build_context(self, matrix_room: MatrixRoom, event: Event) -> Context
133133
room = self.get_room(matrix_room.room_id)
134134
ctx = Context(bot=self, room=room, event=event)
135135

136-
if parts := ctx.body[len(self.prefix) :].split():
136+
if ctx.body.startswith(self.prefix):
137+
prefix = self.prefix
138+
else:
139+
prefix = next(
140+
(cmd.prefix for cmd in self._commands.values() if cmd.prefix and ctx.body.startswith(cmd.prefix)),
141+
None,
142+
)
143+
144+
if prefix is None:
145+
return ctx
146+
147+
if parts := ctx.body[len(prefix):].split():
137148
cmd_name = parts[0]
138149
cmd = self._commands.get(cmd_name)
139150

151+
if cmd and cmd.prefix:
152+
if not ctx.body.startswith(cmd.prefix):
153+
return ctx
154+
140155
if not cmd:
141156
raise CommandNotFoundError(cmd_name)
142157

143-
prefix = cmd.prefix or self.prefix
144-
if not prefix or not ctx.body.startswith(prefix):
145-
return ctx
146-
147158
ctx.command = cmd
159+
148160
return ctx
149161

150162
async def on_message(self, room: MatrixRoom, event: Event) -> None:

0 commit comments

Comments
 (0)