Funguy class refactor
This commit is contained in:
parent
784409cef6
commit
c996168543
67
funguy.py
67
funguy.py
@ -6,35 +6,31 @@ 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
|
||||||
|
|
||||||
class FunguyBot:
|
class FunguyBot:
|
||||||
|
|
||||||
|
bot = None
|
||||||
|
config = None
|
||||||
|
|
||||||
def __init__(self):
|
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_DIR = "plugins"
|
||||||
self.PLUGINS = {}
|
self.PLUGINS = {}
|
||||||
self.config = FunguyConfig()
|
self.config = None
|
||||||
|
self.bot = None
|
||||||
# Load plugins
|
self.load_dotenv()
|
||||||
|
self.setup_logging()
|
||||||
self.load_plugins()
|
self.load_plugins()
|
||||||
|
self.load_config()
|
||||||
|
|
||||||
# Bot configuration settings
|
def load_dotenv(self):
|
||||||
self.MATRIX_URL = os.getenv("MATRIX_URL")
|
load_dotenv()
|
||||||
self.MATRIX_USER = os.getenv("MATRIX_USER")
|
|
||||||
self.MATRIX_PASS = os.getenv("MATRIX_PASS")
|
|
||||||
|
|
||||||
# Get credentials from env
|
def setup_logging(self):
|
||||||
creds = botlib.Creds(self.MATRIX_URL, self.MATRIX_USER, self.MATRIX_PASS)
|
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)
|
||||||
|
logging.getLogger().setLevel(logging.INFO)
|
||||||
# Bot initialization
|
|
||||||
self.bot = botlib.Bot(creds, self.config)
|
|
||||||
|
|
||||||
def load_plugins(self):
|
def load_plugins(self):
|
||||||
for plugin_file in os.listdir(self.PLUGINS_DIR):
|
for plugin_file in os.listdir(self.PLUGINS_DIR):
|
||||||
@ -47,25 +43,23 @@ class FunguyBot:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Error loading plugin {plugin_name}: {e}")
|
logging.error(f"Error loading plugin {plugin_name}: {e}")
|
||||||
|
|
||||||
async def reload_plugins(self, room_id):
|
def reload_plugins(self):
|
||||||
self.PLUGINS = {}
|
self.PLUGINS = {}
|
||||||
# Unload modules from sys.modules
|
# Unload modules from sys.modules
|
||||||
for plugin_name in list(sys.modules.keys()):
|
for plugin_name in list(sys.modules.keys()):
|
||||||
if plugin_name.startswith(self.PLUGINS_DIR + "."):
|
if plugin_name.startswith(self.PLUGINS_DIR + "."):
|
||||||
del sys.modules[plugin_name]
|
del sys.modules[plugin_name]
|
||||||
self.load_plugins()
|
self.load_plugins()
|
||||||
await self.bot.api.send_text_message(room_id, "Plugins reloaded successfully")
|
|
||||||
|
|
||||||
async def rehash_config(self, room_id):
|
def load_config(self):
|
||||||
del self.config
|
|
||||||
self.config = FunguyConfig()
|
self.config = FunguyConfig()
|
||||||
await self.bot.api.send_text_message(room_id, "Config rehashed")
|
|
||||||
|
|
||||||
async def handle_commands(self, room, message):
|
async def handle_commands(self, room, message):
|
||||||
match = botlib.MessageMatch(room, message, self.bot, self.config.prefix)
|
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 match.is_not_from_this_bot() and match.prefix() and match.command("reload"):
|
||||||
if str(message.sender) == self.config.admin_user:
|
if str(message.sender) == self.config.admin_user:
|
||||||
await self.reload_plugins(room.room_id)
|
self.reload_plugins()
|
||||||
|
await self.bot.api.send_text_message(room.room_id, "Plugins reloaded successfully")
|
||||||
else:
|
else:
|
||||||
await self.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 reload plugins.")
|
||||||
|
|
||||||
@ -74,11 +68,28 @@ class FunguyBot:
|
|||||||
|
|
||||||
if match.is_not_from_this_bot() and match.prefix() and match.command("rehash"):
|
if match.is_not_from_this_bot() and match.prefix() and match.command("rehash"):
|
||||||
if str(message.sender) == self.config.admin_user:
|
if str(message.sender) == self.config.admin_user:
|
||||||
await self.rehash_config(room.room_id)
|
self.rehash_config()
|
||||||
|
await self.bot.api.send_text_message(room.room_id, "Config rehashed")
|
||||||
else:
|
else:
|
||||||
await self.bot.api.send_text_message(room.room_id, "You are not authorized to rehash config.")
|
await self.bot.api.send_text_message(room.room_id, "You are not authorized to reload plugins.")
|
||||||
|
|
||||||
|
def rehash_config(self):
|
||||||
|
del self.config
|
||||||
|
self.config = FunguyConfig()
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
MATRIX_URL = os.getenv("MATRIX_URL")
|
||||||
|
MATRIX_USER = os.getenv("MATRIX_USER")
|
||||||
|
MATRIX_PASS = os.getenv("MATRIX_PASS")
|
||||||
|
creds = botlib.Creds(MATRIX_URL, MATRIX_USER, MATRIX_PASS)
|
||||||
|
self.bot = botlib.Bot(creds, self.config)
|
||||||
|
|
||||||
|
@self.bot.listener.on_message_event
|
||||||
|
async def wrapper_handle_commands(room, message):
|
||||||
|
await self.handle_commands(room, message)
|
||||||
|
|
||||||
|
self.bot.run()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
bot = FunguyBot()
|
bot = FunguyBot()
|
||||||
bot.bot.run()
|
bot.run()
|
||||||
|
Loading…
Reference in New Issue
Block a user