Updated plugins. AI plugin updated
This commit is contained in:
@@ -1,35 +1,64 @@
|
||||
"""
|
||||
This plugin provides a command for the admin to load a plugin
|
||||
Plugin for providing a command for the admin to load a plugin.
|
||||
"""
|
||||
# plugins/load_plugin.py
|
||||
|
||||
import os
|
||||
import logging
|
||||
import importlib
|
||||
import simplematrixbotlib as botlib
|
||||
|
||||
# Dictionary to store loaded plugins
|
||||
PLUGINS = {}
|
||||
|
||||
async def load_plugin(plugin_name):
|
||||
"""
|
||||
Asynchronously loads a plugin.
|
||||
|
||||
Args:
|
||||
plugin_name (str): The name of the plugin to load.
|
||||
|
||||
Returns:
|
||||
bool: True if the plugin is loaded successfully, False otherwise.
|
||||
"""
|
||||
try:
|
||||
# Import the plugin module
|
||||
module = importlib.import_module(f"plugins.{plugin_name}")
|
||||
# Add the plugin module to the PLUGINS dictionary
|
||||
PLUGINS[plugin_name] = module
|
||||
logging.info(f"Loaded plugin: {plugin_name}")
|
||||
return True
|
||||
except Exception as e:
|
||||
# Log an error if the plugin fails to load
|
||||
logging.error(f"Error loading plugin {plugin_name}: {e}")
|
||||
return False
|
||||
|
||||
async def handle_command(room, message, bot, prefix, config):
|
||||
"""
|
||||
Asynchronously handles the command to load a plugin.
|
||||
|
||||
Args:
|
||||
room (Room): The Matrix room where the command was invoked.
|
||||
message (RoomMessage): The message object containing the command.
|
||||
bot (MatrixBot): The Matrix bot instance.
|
||||
prefix (str): The command prefix.
|
||||
config (dict): The bot's configuration.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
match = botlib.MessageMatch(room, message, bot, prefix)
|
||||
if match.is_not_from_this_bot() and match.prefix() and match.command("load"):
|
||||
# Check if the sender is the admin
|
||||
if str(message.sender) == config.admin_user:
|
||||
args = match.args()
|
||||
if len(args) != 1:
|
||||
# Send usage message if the command format is incorrect
|
||||
await bot.api.send_text_message(room.room_id, "Usage: !load <plugin>")
|
||||
else:
|
||||
plugin_name = args[0]
|
||||
# Check if the plugin is not already loaded
|
||||
if plugin_name not in PLUGINS:
|
||||
# Load the plugin
|
||||
success = await load_plugin(plugin_name)
|
||||
if success:
|
||||
await bot.api.send_text_message(room.room_id, f"Plugin '{plugin_name}' loaded successfully")
|
||||
@@ -38,4 +67,5 @@ async def handle_command(room, message, bot, prefix, config):
|
||||
else:
|
||||
await bot.api.send_text_message(room.room_id, f"Plugin '{plugin_name}' is already loaded")
|
||||
else:
|
||||
# Send unauthorized message if the sender is not the admin
|
||||
await bot.api.send_text_message(room.room_id, "You are not authorized to load plugins.")
|
||||
|
Reference in New Issue
Block a user