From e9a853c31a6e87c567dd179b9826b1bbd5fa7d1a Mon Sep 17 00:00:00 2001 From: Hash Borgir Date: Fri, 16 Feb 2024 20:13:13 -0700 Subject: [PATCH] Rehash function should work now --- funguy.py | 137 ++++++++++++++++++++++++++---------------------------- 1 file changed, 67 insertions(+), 70 deletions(-) diff --git a/funguy.py b/funguy.py index 65598dd..288788b 100755 --- a/funguy.py +++ b/funguy.py @@ -6,82 +6,79 @@ import logging import importlib import simplematrixbotlib as botlib from dotenv import load_dotenv -import time import sys from plugins.config import FunguyConfig -# Setup logging -logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO) +class FunguyBot: + def __init__(self): + # Setup logging + logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO) -# Load plugins (defined before plugin load/reload functions) -PLUGINS_DIR = "plugins" -PLUGINS = {} + # Load environment variables from .env file + load_dotenv() -def load_plugins(): - for plugin_file in os.listdir(PLUGINS_DIR): - if plugin_file.endswith(".py"): - plugin_name = os.path.splitext(plugin_file)[0] - try: - module = importlib.import_module(f"{PLUGINS_DIR}.{plugin_name}") - PLUGINS[plugin_name] = module - logging.info(f"Loaded plugin: {plugin_name}") - except Exception as e: - logging.error(f"Error loading plugin {plugin_name}: {e}") + # Bot configuration settings + self.PLUGINS_DIR = "plugins" + self.PLUGINS = {} + self.config = FunguyConfig() -def reload_plugins(): - global PLUGINS - PLUGINS = {} - # Unload modules from sys.modules - for plugin_name in list(sys.modules.keys()): - if plugin_name.startswith(PLUGINS_DIR + "."): - del sys.modules[plugin_name] - load_plugins() + # Load plugins + self.load_plugins() -def rehash_config(config): - del config - config = FunguyConfig() + # Bot configuration settings + self.MATRIX_URL = os.getenv("MATRIX_URL") + self.MATRIX_USER = os.getenv("MATRIX_USER") + self.MATRIX_PASS = os.getenv("MATRIX_PASS") + + # Get credentials from env + creds = botlib.Creds(self.MATRIX_URL, self.MATRIX_USER, self.MATRIX_PASS) + + # Bot initialization + self.bot = botlib.Bot(creds, self.config) + + def load_plugins(self): + for plugin_file in os.listdir(self.PLUGINS_DIR): + if plugin_file.endswith(".py"): + plugin_name = os.path.splitext(plugin_file)[0] + try: + module = importlib.import_module(f"{self.PLUGINS_DIR}.{plugin_name}") + self.PLUGINS[plugin_name] = module + logging.info(f"Loaded plugin: {plugin_name}") + except Exception as e: + logging.error(f"Error loading plugin {plugin_name}: {e}") + + async def reload_plugins(self, room_id): + self.PLUGINS = {} + # Unload modules from sys.modules + for plugin_name in list(sys.modules.keys()): + if plugin_name.startswith(self.PLUGINS_DIR + "."): + del sys.modules[plugin_name] + self.load_plugins() + await self.bot.api.send_text_message(room_id, "Plugins reloaded successfully") + + async def rehash_config(self, room_id): + del self.config + self.config = FunguyConfig() + await self.bot.api.send_text_message(room_id, "Config rehashed") + + async def handle_commands(self, room, message): + match = botlib.MessageMatch(room, message, self.bot, self.config.prefix) + if match.is_not_from_this_bot() and match.prefix() and match.command("reload"): + if str(message.sender) == self.config.admin_user: + await self.reload_plugins(room.room_id) + else: + await self.bot.api.send_text_message(room.room_id, "You are not authorized to reload plugins.") + + for plugin_name, plugin_module in self.PLUGINS.items(): + await plugin_module.handle_command(room, message, self.bot, self.config.prefix, self.config) + + if match.is_not_from_this_bot() and match.prefix() and match.command("rehash"): + if str(message.sender) == self.config.admin_user: + await self.rehash_config(room.room_id) + else: + await self.bot.api.send_text_message(room.room_id, "You are not authorized to rehash config.") -# Load environment variables from .env file -load_dotenv() - -# Load plugins -load_plugins() - -# Bot configuration settings -MATRIX_URL = os.getenv("MATRIX_URL") -MATRIX_USER = os.getenv("MATRIX_USER") -MATRIX_PASS = os.getenv("MATRIX_PASS") - -# Get credentials from env -creds = botlib.Creds(MATRIX_URL, MATRIX_USER, MATRIX_PASS) - -# Bot configuration -config = FunguyConfig() -bot = botlib.Bot(creds, config) - -@bot.listener.on_message_event -async def handle_commands(room, message): - match = botlib.MessageMatch(room, message, bot, config.prefix) - if match.is_not_from_this_bot() and match.prefix() and match.command("reload"): - if str(message.sender) == config.admin_user: - reload_plugins() - await bot.api.send_text_message(room.room_id, "Plugins reloaded successfully") - else: - await bot.api.send_text_message(room.room_id, "You are not authorized to reload plugins.") - - for plugin_name, plugin_module in PLUGINS.items(): - await plugin_module.handle_command(room, message, bot, config.prefix, config) - - - if match.is_not_from_this_bot() and match.prefix() and match.command("rehash"): - if str(message.sender) == config.admin_user: - rehash_config(config) - await bot.api.send_text_message(room.room_id, "Config rehashed") - else: - await bot.api.send_text_message(room.room_id, "You are not authorized to reload plugins.") - - - - -bot.run() +if __name__ == "__main__": + bot = FunguyBot() + bot.bot.run()