Add register_decorator method #3

Merged
CSDUMMI merged 2 commits from feat-register-decorator into main 2023-03-29 17:51:47 +00:00
CSDUMMI commented 2023-03-29 15:01:06 +00:00 (Migrated from gitlab.com)

Allow registering a predicate as a decorator for reuse.

 def on_help(event):
   return type(event) is NotificationEvent and event.get_latest_status().text.find("help") != -1

bot.register_decorator(on_help)

@bot.on_help()

Predicates can also receive additional positional and keyword arguments, such as can be seen in the examples/advanced.py bot:

def on_command(command, event):
    return type(event) is NotificationEvent and event.get_latest_status().text.find(command) != -1

bot.register_decorator(on_command)


@bot.on_command("/help")
def get_help(event):
    print("/help")
    return """Hi, I'm an example bot.
    I can greet you with /greet.
    """

This is an advanced feature that I believe will make it a lot easier to write complicated bots with reusable predicates.

Allow registering a predicate as a decorator for reuse. ```python def on_help(event): return type(event) is NotificationEvent and event.get_latest_status().text.find("help") != -1 bot.register_decorator(on_help) @bot.on_help() ``` Predicates can also receive additional positional and keyword arguments, such as can be seen in the `examples/advanced.py` bot: ```python def on_command(command, event): return type(event) is NotificationEvent and event.get_latest_status().text.find(command) != -1 bot.register_decorator(on_command) @bot.on_command("/help") def get_help(event): print("/help") return """Hi, I'm an example bot. I can greet you with /greet. """ ``` This is an advanced feature that I believe will make it a lot easier to write complicated bots with reusable predicates.
CSDUMMI commented 2023-03-29 15:01:06 +00:00 (Migrated from gitlab.com)

requested review from @emacsen

requested review from @emacsen
CSDUMMI commented 2023-03-29 15:08:57 +00:00 (Migrated from gitlab.com)

assigned to @CSDUMMI

assigned to @CSDUMMI
CSDUMMI (Migrated from gitlab.com) changed target branch from master to main 2023-03-29 17:08:21 +00:00
CSDUMMI commented 2023-03-29 17:09:23 +00:00 (Migrated from gitlab.com)

restored source branch feat-register-decorator

restored source branch `feat-register-decorator`
CSDUMMI commented 2023-03-29 17:51:36 +00:00 (Migrated from gitlab.com)

added 3 commits

  • 9517345d...e48183d0 - 2 commits from branch main
  • e1c6b831 - Merge branch 'main' into 'feat-register-decorator'

Compare with previous version

added 3 commits <ul><li>9517345d...e48183d0 - 2 commits from branch <code>main</code></li><li>e1c6b831 - Merge branch &#39;main&#39; into &#39;feat-register-decorator&#39;</li></ul> [Compare with previous version](/babka_net/mastobot/-/merge_requests/1/diffs?diff_id=642218963&start_sha=9517345dd589bd362d743273c9a2aa4395c52732)
CSDUMMI commented 2023-03-29 17:51:47 +00:00 (Migrated from gitlab.com)

mentioned in commit cbf650c573

mentioned in commit cbf650c573482ec8a8900c59ea72567cd9fa2ce0
CSDUMMI (Migrated from gitlab.com) merged commit cbf650c573 into main 2023-03-29 17:51:48 +00:00
Sign in to join this conversation.
No reviewers
No labels
bug
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
babka/matzobot!3
No description provided.