2024-02-12 22:08:57 +00:00
|
|
|
#!/usr/bin/env python
|
2024-02-14 05:39:17 +00:00
|
|
|
# funguy.py
|
2024-02-12 22:08:57 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import importlib
|
|
|
|
import simplematrixbotlib as botlib
|
|
|
|
from dotenv import load_dotenv
|
2024-02-13 03:44:49 +00:00
|
|
|
import sys
|
2024-02-14 05:39:17 +00:00
|
|
|
from plugins.config import FunguyConfig
|
2024-02-12 22:08:57 +00:00
|
|
|
|
2024-02-17 03:13:13 +00:00
|
|
|
class FunguyBot:
|
|
|
|
def __init__(self):
|
|
|
|
# Setup logging
|
|
|
|
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)
|
|
|
|
|
|
|
|
# Load environment variables from .env file
|
|
|
|
load_dotenv()
|
|
|
|
|
|
|
|
# Bot configuration settings
|
|
|
|
self.PLUGINS_DIR = "plugins"
|
|
|
|
self.PLUGINS = {}
|
|
|
|
self.config = FunguyConfig()
|
|
|
|
|
|
|
|
# Load plugins
|
|
|
|
self.load_plugins()
|
|
|
|
|
|
|
|
# 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.")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
bot = FunguyBot()
|
|
|
|
bot.bot.run()
|