Funguy Plugin Enable/Disable feature per room, save to conf file

This commit is contained in:
Hash Borgir 2024-03-03 15:01:44 -07:00
parent 5d6ad98303
commit 41c59618cc
3 changed files with 69 additions and 6 deletions

View File

@ -10,3 +10,7 @@ blocklist = []
admin_user = "@hashborgir:mozilla.org"
prefix = "!"
config_file = "funguy.conf"
[plugins.disabled]
"!uFhErnfpYhhlauJsNK:matrix.org" = [ "youtube-preview",]
"!vYcfWXpPvxeQvhlFdV:matrix.org" = []

View File

@ -1,5 +1,8 @@
#!/usr/bin/env python
# funguy.py
"""
Funguy Bot Class
"""
import os
import logging
@ -8,22 +11,23 @@ import simplematrixbotlib as botlib
from dotenv import load_dotenv
import time
import sys
import toml
from plugins.config import FunguyConfig
class FunguyBot:
bot = None
config = None
def __init__(self):
self.PLUGINS_DIR = "plugins"
self.PLUGINS = {}
self.config = None
self.bot = None
self.disabled_plugins = {} # Dictionary to store disabled plugins for each room
self.load_dotenv()
self.setup_logging()
self.load_plugins()
self.load_config()
self.load_disabled_plugins()
def load_dotenv(self):
load_dotenv()
@ -54,6 +58,21 @@ class FunguyBot:
def load_config(self):
self.config = FunguyConfig()
def load_disabled_plugins(self):
if os.path.exists('funguy.conf'):
with open('funguy.conf', 'r') as f:
config_data = toml.load(f)
self.disabled_plugins = config_data.get('plugins', {}).get('disabled', {})
def save_disabled_plugins(self):
existing_config = {}
if os.path.exists('funguy.conf'):
with open('funguy.conf', 'r') as f:
existing_config = toml.load(f)
existing_config['plugins'] = {'disabled': self.disabled_plugins}
with open('funguy.conf', 'w') as f:
toml.dump(existing_config, f)
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"):
@ -63,7 +82,32 @@ class FunguyBot:
else:
await self.bot.api.send_text_message(room.room_id, "You are not authorized to reload plugins.")
if match.is_not_from_this_bot() and match.prefix() and match.command("disable"):
if str(message.sender) == self.config.admin_user:
args = match.args()
if len(args) != 2:
await self.bot.api.send_text_message(room.room_id, "Usage: !disable <plugin> <room_id>")
else:
plugin_name, room_id = args
await self.disable_plugin(room_id, plugin_name)
await self.bot.api.send_text_message(room.room_id, f"Plugin '{plugin_name}' disabled for room '{room_id}'")
else:
await self.bot.api.send_text_message(room.room_id, "You are not authorized to disable plugins.")
if match.is_not_from_this_bot() and match.prefix() and match.command("enable"):
if str(message.sender) == self.config.admin_user:
args = match.args()
if len(args) != 2:
await self.bot.api.send_text_message(room.room_id, "Usage: !enable <plugin> <room_id>")
else:
plugin_name, room_id = args
await self.enable_plugin(room_id, plugin_name)
await self.bot.api.send_text_message(room.room_id, f"Plugin '{plugin_name}' enabled for room '{room_id}'")
else:
await self.bot.api.send_text_message(room.room_id, "You are not authorized to enable plugins.")
for plugin_name, plugin_module in self.PLUGINS.items():
if plugin_name not in self.disabled_plugins.get(room.room_id, []):
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"):
@ -77,6 +121,18 @@ class FunguyBot:
del self.config
self.config = FunguyConfig()
async def disable_plugin(self, room_id, plugin_name):
if room_id not in self.disabled_plugins:
self.disabled_plugins[room_id] = []
if plugin_name not in self.disabled_plugins[room_id]:
self.disabled_plugins[room_id].append(plugin_name)
self.save_disabled_plugins()
async def enable_plugin(self, room_id, plugin_name):
if room_id in self.disabled_plugins and plugin_name in self.disabled_plugins[room_id]:
self.disabled_plugins[room_id].remove(plugin_name)
self.save_disabled_plugins()
def run(self):
MATRIX_URL = os.getenv("MATRIX_URL")
MATRIX_USER = os.getenv("MATRIX_USER")
@ -93,3 +149,6 @@ class FunguyBot:
if __name__ == "__main__":
bot = FunguyBot()
bot.run()

View File

@ -32,7 +32,7 @@ async def fetch_youtube_info(youtube_url):
async def handle_command(room, message, bot, prefix, config):
match = botlib.MessageMatch(room, message, bot, prefix)
if match.is_not_from_this_bot() and re.search(r'youtube\.com/watch\?v=', message.body) and room.room_id != '!uFhErnfpYhhlauJsNK:matrix.org':
if match.is_not_from_this_bot() and re.search(r'youtube\.com/watch\?v=', message.body): # and room.room_id != '!uFhErnfpYhhlauJsNK:matrix.org':
logging.info("YouTube link detected")
video_id_match = re.search(r'youtube\.com/watch\?v=([^\s]+)', message.body)
if video_id_match: