-
Notifications
You must be signed in to change notification settings - Fork 16
feat: ✨ trigger the bot by mentioning it #81
base: master
Are you sure you want to change the base?
Changes from 1 commit
33776ba
d6a3a34
e73f24b
2d63cbe
57147ec
9a2e924
809186e
c2414d3
25c8ce8
45599af
3a5d621
6b8a0d9
2e380d5
deecc36
cbd843d
708eb2b
a2bde24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| from typing import Union, Optional | ||
|
|
||
| class Match: | ||
| """ | ||
| Class with methods to filter events | ||
|
|
@@ -84,15 +86,16 @@ def __init__(self, room, event, bot, prefix="") -> None: | |
| self._prefix = prefix | ||
|
|
||
| """Forms of identification""" | ||
| self._own_user_id = f"@{self._bot.creds.username}:{self._bot.creds.homeserver.replace("https://","").replace("http://","")}" | ||
| self._own_nio_user = self.room.users[own_user_id] | ||
| self._own_disambiguated_name = own_nio_user.disambiguated_name | ||
| self._own_display_name = own_nio_user.display_name | ||
| self._own_pill = f"<a href=\"https://matrix.to/#/{self.room.own_user_id}\">" | ||
| self._own_user_id = room.own_user_id | ||
| self._own_nio_user = self.room.users[self._own_user_id] | ||
| self._own_disambiguated_name = self._own_nio_user.disambiguated_name | ||
| self._own_display_name = self._own_nio_user.display_name | ||
| self._own_display_name_colon = f"{self._own_display_name}:" | ||
| self._own_pill = f"<a href=\"https://matrix.to/#/{self._own_user_id}\">" | ||
|
|
||
| self.mention() # Set self._mention_id_length | ||
| self._body_without_prefix = self.event.body[len(self._prefix):] | ||
| self._body_without_mention = self.event.body[len(self._mention_id_length):] | ||
| self._body_without_mention = self.event.body[self._mention_id_length:] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why initialize these here while it's unknown whether prefix or mention is used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mention() sets There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it may be a good idea to look at bot libraries for other networks to see how they handle matching. |
||
|
|
||
| if self.mention(): | ||
| body = self._body_without_mention | ||
|
|
@@ -102,7 +105,7 @@ def __init__(self, room, event, bot, prefix="") -> None: | |
| body = self.event.body | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why use distinct member variables? they can't both apply at once There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you reword this? |
||
| self._split_body = body.split() | ||
|
|
||
| def command(self, command="") -> Union[bool, str]: | ||
| def command(self, command: Optional[str] = None) -> Union[bool, str]: | ||
| """ | ||
| Parameters | ||
| ---------- | ||
|
|
@@ -119,12 +122,15 @@ def command(self, command="") -> Union[bool, str]: | |
| """ | ||
|
|
||
| if not (self._body_without_prefix and self._body_without_mention): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
| if command: | ||
| """Body is empty after removing prefix or mention""" | ||
| if command is None: | ||
| return "" | ||
| elif command: | ||
| return False | ||
| else: | ||
| return "" | ||
| return True | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
|
|
||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
| if command: | ||
| if command is not None: | ||
| return self._split_body[0] == command | ||
| else: | ||
| return self._split_body[0] | ||
|
|
@@ -149,11 +155,12 @@ def mention(self): | |
| Returns True if the message begins with the bot's username, MXID, or pill targeting the MXID, and False otherwise. | ||
| """ | ||
|
|
||
| for id in [self._own_disambiguated_name, self._own_display_name, self._own_user_id]: | ||
| for id in [self._own_disambiguated_name, self._own_display_name, self._own_user_id, self._own_display_name_colon]: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will always match _own_display_name and never _own_display_name_colon There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps |
||
| if self.event.body.startswith(id): | ||
| self._mention_id_length = len(id) | ||
| self._mention_id_length = len(id)+1 | ||
| return True | ||
|
|
||
| self._mention_id_length = 0 | ||
|
|
||
| return False | ||
|
|
||
| def args(self): | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.