FunguyBot/funguy.py

88 lines
2.6 KiB
Python
Raw Normal View History

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
import time
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
# Setup logging
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)
2024-02-14 05:39:17 +00:00
# Load plugins (defined before plugin load/reload functions)
2024-02-12 22:08:57 +00:00
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 = {}
2024-02-13 03:44:49 +00:00
# Unload modules from sys.modules
for plugin_name in list(sys.modules.keys()):
if plugin_name.startswith(PLUGINS_DIR + "."):
del sys.modules[plugin_name]
2024-02-12 22:08:57 +00:00
load_plugins()
2024-02-14 05:39:17 +00:00
def rehash_config(config):
del config
config = FunguyConfig()
# Load environment variables from .env file
load_dotenv()
# Load plugins
2024-02-12 22:08:57 +00:00
load_plugins()
2024-02-14 05:39:17 +00:00
# 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)
2024-02-12 22:08:57 +00:00
@bot.listener.on_message_event
async def handle_commands(room, message):
match = botlib.MessageMatch(room, message, bot, config.prefix)
2024-02-12 22:08:57 +00:00
if match.is_not_from_this_bot() and match.prefix() and match.command("reload"):
2024-02-14 05:39:17 +00:00
if str(message.sender) == config.admin_user:
2024-02-12 22:08:57 +00:00
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)
2024-02-14 05:39:17 +00:00
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.")
2024-02-12 22:08:57 +00:00
bot.run()