This repository was archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
feat: ✨ trigger the bot by mentioning it #81
Draft
HarHarLinks
wants to merge
17
commits into
imbev:master
Choose a base branch
from
HarHarLinks:call-by-mention
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
33776ba
add bot trigger: mention
HarHarLinks d6a3a34
fix api.args() for mentions
HarHarLinks e73f24b
Fix messagemath.mention.
2d63cbe
Refactor messagematch.command
57147ec
docs: 📝 Add match.mention() docs
HarHarLinks 9a2e924
refactor MessageMatch.mention()
HarHarLinks 809186e
refactor command() into mention()
HarHarLinks c2414d3
add tests for mention()
HarHarLinks 25c8ce8
add comments to mention related class privates
HarHarLinks 45599af
fix None exception in args
HarHarLinks 3a5d621
correctly strip mention from command
HarHarLinks 6b8a0d9
fix command() list index out of range
HarHarLinks 2e380d5
refactor: :recycle: Refactor MessageMatch class
deecc36
Fix errors introduced by commit 2e380d53
cbd843d
test: :white_check_mark: Fix messagematch tests
708eb2b
Enable failing tests
a2bde24
refactor: :recycle: Change order of if statements to improve readabil…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,17 +82,27 @@ def __init__(self, room, event, bot, prefix="") -> None: | |
| """ | ||
| super().__init__(room, event, bot) | ||
| self._prefix = prefix | ||
| bot_user = self.room.users[self.room.own_user_id] | ||
| # in case another user uses the same display name, we need to regard the disambiguation if present | ||
| self._disambiguated_name = bot_user.disambiguated_name | ||
| # still we allow to mention without disambiguation if typing manually for now | ||
| self._display_name = bot_user.display_name | ||
| # element generates a "pill" from formatted (HTML) links to matrix.to/#/@matrix:id | ||
| # this isn't really specced but still the status quo | ||
| self._pill = f'<a href="https://matrix.to/#/{self.room.own_user_id}">' | ||
| self._body_without_prefix = None | ||
|
|
||
| def command(self, command=None): | ||
|
|
||
| """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}\">" | ||
HarHarLinks marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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):] | ||
|
|
||
| if self.mention(): | ||
| body = self._body_without_mention | ||
| elif self.prefix(): | ||
| body = self._body_without_prefix | ||
| else: | ||
| 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]: | ||
| """ | ||
| Parameters | ||
| ---------- | ||
|
|
@@ -108,22 +118,16 @@ def command(self, command=None): | |
| Returns the string after the prefix and before the first space if no arg is passed to this method. | ||
| """ | ||
|
|
||
| # we cache this part | ||
| if self._body_without_prefix is None: | ||
| if not self.mention(): | ||
| if self._prefix == self.event.body[0:len(self._prefix)]: | ||
| self._body_without_prefix = self.event.body[len(self._prefix):] | ||
| else: | ||
| self._body_without_prefix = self.event.body | ||
|
|
||
| split_body = self._body_without_prefix.split() | ||
| if len(split_body) < 1: | ||
| split_body = [""] | ||
| 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: | ||
| return False | ||
| else: | ||
| return "" | ||
|
|
||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
| if command is not None: | ||
| return split_body[0] == command | ||
| if command: | ||
| return self._split_body[0] == command | ||
| else: | ||
| return split_body[0] | ||
| return self._split_body[0] | ||
|
|
||
| def prefix(self): | ||
| """ | ||
|
|
@@ -134,7 +138,7 @@ def prefix(self): | |
| Returns True if the message begins with the prefix, and False otherwise. If there is no prefix specified during the creation of this MessageMatch object, then return True. | ||
| """ | ||
|
|
||
| return self.event.body.startswith(self._prefix) | ||
| return self.event.body[self._mention_id_length:].startswith(self._prefix) | ||
|
|
||
| def mention(self): | ||
| """ | ||
|
|
@@ -145,25 +149,11 @@ def mention(self): | |
| Returns True if the message begins with the bot's username, MXID, or pill targeting the MXID, and False otherwise. | ||
| """ | ||
|
|
||
| body = self.event.body | ||
| for id in [self._disambiguated_name, self._display_name, self.room.own_user_id]: | ||
| if body.startswith(id): | ||
| body_ = body[len(id):] | ||
| # the match needs to end here, otherwise someone else is mentioned | ||
| # this isn't perfect but probably the best effort | ||
| if body_[0] in [' ', ':']: | ||
| self._body_without_prefix = body_[1:].strip() | ||
| return True | ||
|
|
||
| # pills on the other hand are a clearer case thanks to HTML tags which include delimiters | ||
| body = self.event.formatted_body | ||
| if body is not None and body.startswith(self._pill): | ||
| # remove the first half of the pill | ||
| body = body[len(self._pill):] | ||
| # find pill end + trailing delimiter + maybe whitespace | ||
| self._body_without_prefix = body[body.index('</a>')+5:].strip() | ||
| return True | ||
|
|
||
| for id in [self._own_disambiguated_name, self._own_display_name, self._own_user_id]: | ||
| if self.event.body.startswith(id): | ||
| self._mention_id_length = len(id) | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
| def args(self): | ||
|
|
@@ -172,12 +162,10 @@ def args(self): | |
| Returns | ||
| ------- | ||
| list | ||
| Returns a list of strings that are the "words" of the message, except for the first "word", which would be the command. | ||
| Returns a list of strings that are the "words" of the message, except for the first "word", which would be the prefix/mention + command. | ||
| """ | ||
|
|
||
| if self._body_without_prefix is None: | ||
| self.command() # calculate _body_without_prefix | ||
| return self._body_without_prefix.split()[1:] | ||
| return self._split_body[1:] | ||
|
|
||
| def contains(self, string): | ||
| """ | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.