Rehash function should work now
This commit is contained in:
parent
669fd361d4
commit
e9a853c31a
107
funguy.py
107
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
|
||||
|
||||
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 = {}
|
||||
|
||||
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}")
|
||||
|
||||
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()
|
||||
|
||||
def rehash_config(config):
|
||||
del config
|
||||
config = FunguyConfig()
|
||||
|
||||
|
||||
# Load environment variables from .env file
|
||||
load_dotenv()
|
||||
|
||||
# Bot configuration settings
|
||||
self.PLUGINS_DIR = "plugins"
|
||||
self.PLUGINS = {}
|
||||
self.config = FunguyConfig()
|
||||
|
||||
# Load plugins
|
||||
load_plugins()
|
||||
self.load_plugins()
|
||||
|
||||
# Bot configuration settings
|
||||
MATRIX_URL = os.getenv("MATRIX_URL")
|
||||
MATRIX_USER = os.getenv("MATRIX_USER")
|
||||
MATRIX_PASS = os.getenv("MATRIX_PASS")
|
||||
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(MATRIX_URL, MATRIX_USER, MATRIX_PASS)
|
||||
creds = botlib.Creds(self.MATRIX_URL, self.MATRIX_USER, self.MATRIX_PASS)
|
||||
|
||||
# Bot configuration
|
||||
config = FunguyConfig()
|
||||
bot = botlib.Bot(creds, config)
|
||||
# Bot initialization
|
||||
self.bot = botlib.Bot(creds, self.config)
|
||||
|
||||
@bot.listener.on_message_event
|
||||
async def handle_commands(room, message):
|
||||
match = botlib.MessageMatch(room, message, bot, config.prefix)
|
||||
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) == config.admin_user:
|
||||
reload_plugins()
|
||||
await bot.api.send_text_message(room.room_id, "Plugins reloaded successfully")
|
||||
if str(message.sender) == self.config.admin_user:
|
||||
await self.reload_plugins(room.room_id)
|
||||
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)
|
||||
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) == config.admin_user:
|
||||
rehash_config(config)
|
||||
await bot.api.send_text_message(room.room_id, "Config rehashed")
|
||||
if str(message.sender) == self.config.admin_user:
|
||||
await self.rehash_config(room.room_id)
|
||||
else:
|
||||
await bot.api.send_text_message(room.room_id, "You are not authorized to reload plugins.")
|
||||
await self.bot.api.send_text_message(room.room_id, "You are not authorized to rehash config.")
|
||||
|
||||
|
||||
|
||||
|
||||
bot.run()
|
||||
if __name__ == "__main__":
|
||||
bot = FunguyBot()
|
||||
bot.bot.run()
|
||||
|
Loading…
Reference in New Issue
Block a user