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

feat: ✨ trigger the bot by mentioning it #81

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions simplematrixbotlib/match.py
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
Expand Down Expand Up @@ -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}:"
This conversation was marked as resolved.
Show resolved Hide resolved
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:]
Copy link
Contributor Author

@HarHarLinks HarHarLinks Nov 30, 2021

Choose a reason for hiding this comment

The 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?
why create member variables that are only ever used 2 lines later?
mention() is executed twice unnecessarily

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mention() sets ._mention_id_length, which is neccesary for calculating ._body_without_mention

Copy link

@ghost ghost Dec 28, 2021

Choose a reason for hiding this comment

The 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
Expand All @@ -102,7 +105,7 @@ def __init__(self, room, event, bot, prefix="") -> None:
body = self.event.body
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use distinct member variables? they can't both apply at once

Copy link

Choose a reason for hiding this comment

The 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
----------
Expand All @@ -119,12 +122,15 @@ def command(self, command="") -> Union[bool, str]:
"""

if not (self._body_without_prefix and self._body_without_mention):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use len(self._split_body) instead

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]
Expand All @@ -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]:
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps _own_display_name_colon could be removed.

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):
Expand Down