Skip to content

Commit

Permalink
Add Plugin Method to format a user highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
leofah committed Dec 6, 2022
1 parent 32d7489 commit 4fe5dbc
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
6 changes: 4 additions & 2 deletions PLUGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ must be awaited.

Example:
```python
HELP_DESC = ("!echo\t\t\t-\tEcho back the given string\n")
HELP_DESC = "!echo\t\t\t-\tEcho back the given string\n"

async def register_to(plugin):

Expand All @@ -41,7 +41,9 @@ async def register_to(plugin):
args = plugin.extract_args(event)
args.pop(0)

await plugin.send_text(event.source['sender'] + ': ' + ' '.join(args))
await plugin.send_html(
await plugin.format_user_highlight(event.sender) + ": " + " ".join(args)
)

# Add a command handler waiting for the echo command
echo_handler = plugin.CommandHandler("echo", echo_callback)
Expand Down
2 changes: 1 addition & 1 deletion cyberbot/matrixroom.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async def add_plugin(self, pluginname):
await plugin.load()

async def remove_plugin(self, pluginname):
self.log.info(f"Removeing plugin {pluginname} from room")
self.log.info(f"Removing plugin {pluginname} from room")
# no need for lock as only yielding point of control flow is await statement, which is last
c = self.bot.conn.cursor()
# we put this before the if block to be able to remove plugins that are left by accident
Expand Down
6 changes: 6 additions & 0 deletions cyberbot/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@ async def stop_task(self, task):
# =============================================
# Plugin helper functions (misc)
# ==============================================
async def format_user_highlight(self, user_id, display_name=None):
"""Format a hightlight to reference a user in a room. This message needs to be sent as html"""
if display_name == None:
display_name = (await self.client.get_displayname(user_id)).displayname
return f'<a href="https://matrix.to/#/{user_id}">{display_name}</a>'

@staticmethod
def extract_args(event):
return shlex.split(event.source["content"]["body"])
Expand Down
4 changes: 3 additions & 1 deletion plugins_examples/echo_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ async def echo_callback(room, event):
args = plugin.extract_args(event)
args.pop(0)

await plugin.send_text(event.source["sender"] + ": " + " ".join(args))
await plugin.send_html(
await plugin.format_user_highlight(event.sender) + ": " + " ".join(args)
)

# Add a command handler waiting for the echo command
echo_handler = plugin.CommandHandler("echo", echo_callback)
Expand Down
2 changes: 1 addition & 1 deletion plugins_examples/invite_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ async def invite_callback(room, event):
elif args[0] == "new":
args.pop(0)
try:
invitor = (await room.client.get_displayname(event.sender)).displayname
invitor = await plugin.get_sender_name(event)
except:
invitor = "Unknown Invitor"
await handle_new(invitor, args)
Expand Down

0 comments on commit 4fe5dbc

Please sign in to comment.