from __future__ import annotations from typing import Any, Dict, List, Optional from core.logger import get_logger class Plugin: name: str = "" version: str = "0.1.0" author: str = "" description: str = "" dependencies: List[str] = [] category: str = "" conflicts: List[str] = [] config_schema: Dict[str, Any] = {} config_version: str = "1.0.0" min_core_version: str = "" max_core_version: str = "" def __init__(self, context: "PluginContext") -> None: self.context = context self.log = get_logger(f"plugin.{self.name or self.__class__.__name__}") self.db = context.database.plugin_db(self.name) plugin_cfg = context.config.get_plugin_config(self.name) self.commands = context.command_builder.with_owner( self.name, owner_type="plugin", prefix=plugin_cfg.get("command_prefix"), ) self.sandbox = context.sandbox.for_plugin(self.name) async def on_load(self) -> None: return None async def on_unload(self) -> None: return None async def on_enable(self) -> None: return None async def on_disable(self) -> None: return None async def on_message(self, event: Any) -> None: return None async def on_message_new(self, event: Any) -> None: return None async def on_edit(self, event: Any) -> None: return None async def on_message_edit(self, event: Any) -> None: return None async def on_delete(self, event: Any) -> None: return None async def on_message_delete(self, event: Any) -> None: return None async def on_message_read(self, event: Any) -> None: return None async def on_message_sent(self, event: Any) -> None: return None async def on_command(self, event: Any) -> None: return None async def on_inline_query(self, event: Any) -> None: return None async def on_callback_query(self, event: Any) -> None: return None async def on_chat_action(self, event: Any) -> None: return None async def on_chat_update(self, event: Any) -> None: return None async def on_typing(self, event: Any) -> None: return None async def on_recording(self, event: Any) -> None: return None async def on_user_update(self, event: Any) -> None: return None async def on_contact_update(self, event: Any) -> None: return None async def on_status_update(self, event: Any) -> None: return None def get_config(self) -> Dict[str, Any]: return self.context.config.get_plugin_config(self.name) def set_config(self, data: Dict[str, Any]) -> None: self.context.config.set_plugin_config(self.name, data) def get_secret(self, key: str) -> Any: cfg = self.get_config().get("secrets", {}) value = cfg.get(key) return self.context.config.decrypt_value(value) if value else None def set_secret(self, key: str, value: str) -> None: cfg = self.get_config() secrets = cfg.setdefault("secrets", {}) secrets[key] = self.context.config.encrypt_value(value) self.set_config(cfg) def get_service(self, name: str) -> Optional[Any]: return self.context.bus.get_service(name) def register_service(self, name: str, service: Any) -> None: self.context.bus.register_service(name, service) def register_command(self, *args, **kwargs) -> Any: return self.commands.command(*args, **kwargs) class PluginContext: def __init__(self, app: "OverUB", plugin_name: str) -> None: from core.config import PluginConfigProxy self.app = app self.config = PluginConfigProxy(app.config, plugin_name) self.bus = app.bus self.database = app.database self.cache = app.cache self.rate_limiter = app.rate_limiter self.commands = app.commands self.command_builder = app.command_builder self.sandbox = app.sandbox from core.http import RestrictedSession self.http = RestrictedSession(app.http, app.sandbox.allow_network) self.events = app.events self.permissions = app.permissions