Skip to content
This repository was archived by the owner on May 22, 2024. It is now read-only.

Commit d6a3a34

Browse files
committed
fix api.args() for mentions
1 parent 33776ba commit d6a3a34

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

simplematrixbotlib/match.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def __init__(self, room, event, bot, prefix="") -> None:
8686
self._display_name = bot_user.display_name
8787
self._disambiguated_name = bot_user.disambiguated_name
8888
self._pill = f'<a href="https://matrix.to/#/{self.room.own_user_id}">'
89+
self._body_without_prefix = None
8990

9091
def command(self, command=None):
9192
"""
@@ -103,33 +104,37 @@ def command(self, command=None):
103104
Returns the string after the prefix and before the first space if no arg is passed to this method.
104105
"""
105106

106-
if self._prefix == self.event.body[0:len(self._prefix)]:
107-
body_without_prefix = self.event.body[len(self._prefix):]
108-
elif self.mention():
109-
# the order is important here!
110-
# note: we assume that body and formatted_body, if present, match as a workaraound of cleaning html
111-
if self.event.body.startswith(self._disambiguated_name):
112-
body_without_prefix = self.event.body[len(self._disambiguated_name):]
113-
elif self.event.body.startswith(self._display_name):
114-
body_without_prefix = self.event.body[len(self._display_name):]
115-
elif self.event.body.startswith(self.room.own_user_id):
116-
body_without_prefix = self.event.body[len(self.room.own_user_id):]
117-
elif self.event.formatted_body.startswith(self._pill):
118-
name = self.event.formatted_body[len(self._pill):self.event.formatted_body.index('</a>')]
119-
body_without_prefix = body_without_prefix = self.event.body[len(name):]
120-
# mentioning may include a : (colon) as inserted by Element when clicking on a user
121-
if body_without_prefix.startswith(':'):
122-
body_without_prefix = body_without_prefix[1:]
123-
else:
124-
body_without_prefix = self.event.body
125-
126-
if not body_without_prefix:
127-
return []
107+
# we cache this part
108+
if not self._body_without_prefix:
109+
if self._prefix == self.event.body[0:len(self._prefix)]:
110+
self._body_without_prefix = self.event.body[len(self._prefix):]
111+
elif self.mention():
112+
# the order is important here!
113+
# note: we assume that body and formatted_body, if present, match as a workaraound of cleaning html
114+
if self.event.body.startswith(self._disambiguated_name):
115+
self._body_without_prefix = self.event.body[len(self._disambiguated_name):]
116+
elif self.event.body.startswith(self._display_name):
117+
self._body_without_prefix = self.event.body[len(self._display_name):]
118+
elif self.event.body.startswith(self.room.own_user_id):
119+
self._body_without_prefix = self.event.body[len(self.room.own_user_id):]
120+
elif self.event.formatted_body.startswith(self._pill):
121+
name = self.event.formatted_body[len(self._pill):self.event.formatted_body.index('</a>')]
122+
self._body_without_prefix = self.event.body[len(name):]
123+
# mentioning may include a : (colon) as inserted by Element when clicking on a user
124+
if self._body_without_prefix.startswith(':'):
125+
self._body_without_prefix = self._body_without_prefix[1:]
126+
# trim leading whitespace after the mention
127+
self._body_without_prefix = self._body_without_prefix.strip()
128+
else:
129+
self._body_without_prefix = self.event.body
130+
131+
if not self._body_without_prefix:
132+
return []
128133

129134
if command:
130-
return body_without_prefix.split()[0] == command
135+
return self._body_without_prefix.split()[0] == command
131136
else:
132-
return body_without_prefix.split()[0]
137+
return self._body_without_prefix.split()[0]
133138

134139
def prefix(self):
135140
"""
@@ -167,7 +172,7 @@ def args(self):
167172
Returns a list of strings that are the "words" of the message, except for the first "word", which would be the command.
168173
"""
169174

170-
return self.event.body.split()[1:]
175+
return self._body_without_prefix.split()[1:]
171176

172177
def contains(self, string):
173178
"""

0 commit comments

Comments
 (0)