Rehash function should work now

This commit is contained in:
Hash Borgir 2024-02-16 20:13:13 -07:00
parent 669fd361d4
commit e9a853c31a

137
funguy.py
View File

@ -6,82 +6,79 @@ import logging
import importlib import importlib
import simplematrixbotlib as botlib import simplematrixbotlib as botlib
from dotenv import load_dotenv from dotenv import load_dotenv
import time
import sys import sys
from plugins.config import FunguyConfig from plugins.config import FunguyConfig
# Setup logging class FunguyBot:
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO) 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) # Load environment variables from .env file
PLUGINS_DIR = "plugins" load_dotenv()
PLUGINS = {}
def load_plugins(): # Bot configuration settings
for plugin_file in os.listdir(PLUGINS_DIR): self.PLUGINS_DIR = "plugins"
if plugin_file.endswith(".py"): self.PLUGINS = {}
plugin_name = os.path.splitext(plugin_file)[0] self.config = FunguyConfig()
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}")
def reload_plugins(): # Load plugins
global PLUGINS self.load_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()
def rehash_config(config): # Bot configuration settings
del config self.MATRIX_URL = os.getenv("MATRIX_URL")
config = FunguyConfig() 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 if __name__ == "__main__":
load_dotenv() bot = FunguyBot()
bot.bot.run()
# 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()