first commit

This commit is contained in:
2026-04-29 17:41:10 -05:00
parent f53104d947
commit 0fe6bd7ea6
15 changed files with 1678 additions and 33 deletions

47
plugins/__init__.py Normal file
View File

@@ -0,0 +1,47 @@
import os
import importlib.util
import logging
logger = logging.getLogger(__name__)
def load_plugins(app=None):
"""
Automatically discover and load general plugins from plugins/.
Each plugin module can have optional hooks:
- register(app) for API routes
- register_commands() for bot commands
- register_tasks() for background tasks
- PLUGIN_NAME, PLUGIN_VERSION for metadata
"""
plugins_dir = os.path.dirname(__file__)
for filename in os.listdir(plugins_dir):
if filename.endswith('.py') and filename != '__init__.py':
module_name = filename[:-3] # remove .py
module_path = os.path.join(plugins_dir, filename)
try:
spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
# Optional metadata
name = getattr(module, 'PLUGIN_NAME', module_name)
version = getattr(module, 'PLUGIN_VERSION', 'unknown')
# Apply hooks if available
if app and hasattr(module, 'register'):
module.register(app)
logger.info(f"Loaded plugin routes: {name} v{version}")
if hasattr(module, 'register_commands'):
module.register_commands()
logger.info(f"Loaded plugin commands: {name} v{version}")
if hasattr(module, 'register_tasks'):
module.register_tasks()
logger.info(f"Loaded plugin tasks: {name} v{version}")
logger.info(f"Loaded plugin: {name} v{version}")
except Exception as e:
logger.error(f"Failed to load plugin {module_name}: {e}")

44
plugins/example_plugin.py Normal file
View File

@@ -0,0 +1,44 @@
"""
Example plugin demonstrating the plugin interface.
Plugins can:
- Define metadata: PLUGIN_NAME, PLUGIN_VERSION
- Register API routes with register(app)
- Register bot commands with register_commands()
- Register background tasks with register_tasks()
"""
PLUGIN_NAME = "example_plugin"
PLUGIN_VERSION = "1.0.0"
def register(app):
"""Register API routes."""
@app.route("/api/example_plugin", methods=["GET"])
def api_example():
return {"message": "Hello from example plugin!"}, 200
def register_commands():
"""Register bot commands."""
from bot.command_registry import register_module
import ai.parser as ai_parser
async def handle_example_plugin(message, session, parsed):
await message.channel.send("Example plugin command executed!")
def validate_example_plugin_json(data):
return [] # No validation errors
register_module("example_plugin", handle_example_plugin)
ai_parser.register_validator("example_plugin", validate_example_plugin_json)
def register_tasks():
"""Register background tasks."""
# Example: modify the background loop
from bot.bot import backgroundLoop
async def example_task():
print("Example plugin background task running")
# Note: This is a simple example; in practice, you'd need a better way to extend tasks
# For now, plugins can import and modify global state if needed
pass