Files
FunguyBot/plugins/help.py
T

76 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Plugin for dynamically aggregating help from all loaded plugins.
"""
import logging
import simplematrixbotlib as botlib
async def handle_command(room, message, bot, prefix, config):
match = botlib.MessageMatch(room, message, bot, prefix)
if not (match.is_not_from_this_bot() and match.prefix() and match.command("help")):
return
args = match.args()
plugins = getattr(bot, "plugins", {})
if not plugins:
await bot.api.send_text_message(room.room_id, "No plugins are currently loaded.")
return
# If a specific plugin is requested
if args:
plugin_name = args[0].strip()
if plugin_name in plugins:
plugin = plugins[plugin_name]
help_html = getattr(plugin, "__help__", None)
if help_html:
await bot.api.send_markdown_message(room.room_id, help_html)
else:
# Fallback: docstring first line
doc = getattr(plugin, "__doc__", "No description available.")
first_line = doc.strip().split("\n")[0] if doc else "No description available."
await bot.api.send_text_message(
room.room_id, f"No detailed help for '{plugin_name}'. {first_line}"
)
return
else:
await bot.api.send_text_message(
room.room_id, f"Plugin '{plugin_name}' not found. Available: {', '.join(sorted(plugins.keys()))}"
)
return
# Aggregate help from all plugins
help_parts = []
for pname in sorted(plugins.keys()):
plugin = plugins[pname]
help_html = getattr(plugin, "__help__", None)
if help_html:
help_parts.append(help_html)
else:
# Minimal fallback using docstring
doc = getattr(plugin, "__doc__", "No description.")
first_line = doc.strip().split("\n")[0] if doc else "No description."
help_parts.append(
f"<details><summary><strong>!{pname}</strong></summary><p>{first_line}</p></details>"
)
# Append bot credits
credits = """
<details><summary><strong>🌟 Funguy Bot Credits</strong></summary>
<p>
<strong>🧙‍♂️ Creator & Developer</strong>: HB is the author of 🍄Funguy Bot🍄. (@hashborgir:mozilla.org)<br>
<strong>🚀 Development Context</strong>: Created during recovery from two-level cervical spinal surgery (CDA Cervical Discectomy and Disc Arthroplasty)<br>
<br>
<strong>Join our Matrix Room</strong>: <a href="https://matrix.to/#/#selfhosting:mozilla.org">Selfhosting | Security | Sysadmin | Homelab | Programming</a>
</p>
</details>
"""
help_parts.append(credits)
master = "<details><summary><strong>🍄 Funguy Bot Commands</strong> (click to expand)</summary><br>"
master += "\n".join(help_parts)
master += "</details>"
await bot.api.send_markdown_message(room.room_id, master)
logging.info("Sent dynamic help from %d plugins", len(plugins))