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

Commit 2d63cbe

Browse files
author
krazykirby99999
committed
Refactor messagematch.command
1 parent e73f24b commit 2d63cbe

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

simplematrixbotlib/match.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,26 +111,26 @@ def command(self, command=None):
111111
elif self.mention():
112112
# the order is important here!
113113
# 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>')]
114+
id_matched = False
115+
for id in (self._disambiguated_name, self._display_name, self.room.own_user_id):
116+
if self.event.body.startswith(id) and not id_matched:
117+
self._body_without_prefix = self.event.body[len(id):]
118+
id_matched = True
119+
120+
if self.event.formatted_body.startswith(self._pill) and not id_matched:
121+
name = self.event.formatted_body[len():self.event.formatted_body.index('</a>')]
122122
self._body_without_prefix = self.event.body[len(name):]
123+
123124
# mentioning may include a : (colon) as inserted by Element when clicking on a user
124125
if self._body_without_prefix.startswith(':'):
125126
self._body_without_prefix = self._body_without_prefix[1:]
127+
126128
# trim leading whitespace after the mention
127129
self._body_without_prefix = self._body_without_prefix.strip()
130+
128131
else:
129132
self._body_without_prefix = self.event.body
130133

131-
if not self._body_without_prefix:
132-
return []
133-
134134
if command:
135135
return self._body_without_prefix.split()[0] == command
136136
else:

0 commit comments

Comments
 (0)