@@ -82,6 +82,10 @@ def __init__(self, room, event, bot, prefix="") -> None:
8282 """
8383 super ().__init__ (room , event , bot )
8484 self ._prefix = prefix
85+ bot_user = self .room .users [self .room .own_user_id ]
86+ self ._display_name = bot_user .display_name
87+ self ._disambiguated_name = bot_user .disambiguated_name
88+ self ._pill = f'<a href="https://matrix.to/#/{ self .room .own_user_id } ">'
8589
8690 def command (self , command = None ):
8791 """
@@ -101,6 +105,21 @@ def command(self, command=None):
101105
102106 if self ._prefix == self .event .body [0 :len (self ._prefix )]:
103107 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 :]
104123 else :
105124 body_without_prefix = self .event .body
106125
@@ -123,6 +142,22 @@ def prefix(self):
123142
124143 return self .event .body .startswith (self ._prefix )
125144
145+ def mention (self ):
146+ """
147+
148+ Returns
149+ -------
150+ boolean
151+ Returns True if the message begins with the bot's username, MXID, or pill targeting the MXID, and False otherwise.
152+ """
153+
154+ for i in [self ._display_name , self ._disambiguated_name , self .room .own_user_id , self ._pill ]:
155+ body = self .event .formatted_body or self .event .body
156+ if body .startswith (i ):
157+ return True
158+
159+ return False
160+
126161 def args (self ):
127162 """
128163
0 commit comments