From 74d271fb4e1a46ee56030621965a3ffbb4a27704 Mon Sep 17 00:00:00 2001 From: Phoenix Isaac Pereira Date: Wed, 13 Nov 2024 13:48:13 +1030 Subject: [PATCH] Revert "feat(admin): add admin-only commands for bot configuration" This reverts commit 78967067d2bb1521b19b007ffffda2b965e549c9. --- .example.env | 2 - src/commands/admin_commands.py | 128 --------------------------------- src/commands/gemini.py | 12 ---- src/main.py | 5 +- 4 files changed, 1 insertion(+), 146 deletions(-) delete mode 100644 src/commands/admin_commands.py diff --git a/.example.env b/.example.env index 5e6fcdb..39dcef4 100644 --- a/.example.env +++ b/.example.env @@ -6,5 +6,3 @@ TENOR_API_KEY="TENOR_API_KEY" GEMINI_API_KEY="GEMINI_API_KEY" REQUESTS_PER_MINUTE=3 LIMIT_WINDOW=60 -ADMIN_USERS=User1,User2,User3 - diff --git a/src/commands/admin_commands.py b/src/commands/admin_commands.py deleted file mode 100644 index 5faf69d..0000000 --- a/src/commands/admin_commands.py +++ /dev/null @@ -1,128 +0,0 @@ -import os -import logging -from discord import app_commands, Interaction - -# Retrieve the list of admin usernames from the .env file -ADMIN_USERS = os.getenv("ADMIN_USERS", "").split(",") - - -class AdminCommands(app_commands.Group): - def __init__(self, gemini_bot): - super().__init__(name="admin", description="Admin commands for DuckBot setup.") - - # Add subgroups to the main admin group - self.set = SetSubGroup(self.check_admin) - self.reset = ResetSubGroup(self.check_admin, gemini_bot) - - # Register subgroups - self.add_command(self.set) - self.add_command(self.reset) - - async def check_admin(self, interaction: Interaction) -> bool: - user_name = interaction.user.name - logging.info(f"Checking admin status for user: {user_name}") - - if user_name in ADMIN_USERS: - logging.info(f"User {user_name} is authorized.") - return True - else: - logging.warning(f"User {user_name} is not authorized.") - return False - - async def interaction_check(self, interaction: Interaction) -> bool: - """Restrict all admin commands visibility to authorized users only.""" - is_admin = await self.check_admin(interaction) - if is_admin: - return True - await interaction.response.send_message("Unauthorized", ephemeral=True) - return False - - @app_commands.command( - name="log-info", description="Display all current environment variables." - ) - async def log_info(self, interaction: Interaction): - """Command to log and display all relevant environment variables.""" - if not await self.check_admin(interaction): - await interaction.response.send_message("Unauthorized", ephemeral=True) - return - - # Collect environment variable values - guild_id = os.getenv("GUILD_ID", "Not Set") - skullboard_channel_id = os.getenv("SKULLBOARD_CHANNEL_ID", "Not Set") - required_reactions = os.getenv("REQUIRED_REACTIONS", "Not Set") - tenor_api_key = os.getenv("TENOR_API_KEY", "Not Set") - gemini_api_key = os.getenv("GEMINI_API_KEY", "Not Set") - - # Construct a formatted message for environment variables - config_message = ( - "**Current Environment Variables:**\n" - f"Guild ID: `{guild_id}`\n" - f"Skullboard Channel ID: `{skullboard_channel_id}`\n" - f"Required Reactions: `{required_reactions}`\n" - ) - - await interaction.response.send_message(config_message, ephemeral=True) - - -class SetSubGroup(app_commands.Group): - def __init__(self, check_admin): - super().__init__( - name="set", description="Set configuration values for DuckBot." - ) - self.check_admin = check_admin - - @app_commands.command(name="guild-id", description="Set the guild ID for DuckBot.") - async def set_guild_id(self, interaction: Interaction, guild_id: str): - if not await self.check_admin(interaction): - await interaction.response.send_message("Unauthorized", ephemeral=True) - return - os.environ["GUILD_ID"] = guild_id - await interaction.response.send_message( - f"Guild ID set to {guild_id}.", ephemeral=True - ) - - @app_commands.command( - name="skullboard-channel-id", description="Set the Skullboard channel ID." - ) - async def set_skullboard_channel_id( - self, interaction: Interaction, channel_id: str - ): - if not await self.check_admin(interaction): - await interaction.response.send_message("Unauthorized", ephemeral=True) - return - os.environ["SKULLBOARD_CHANNEL_ID"] = channel_id - await interaction.response.send_message( - f"Skullboard channel ID set to {channel_id}.", ephemeral=True - ) - - @app_commands.command( - name="required-reactions", description="Set required reactions for Skullboard." - ) - async def set_required_reactions(self, interaction: Interaction, reactions: int): - if not await self.check_admin(interaction): - await interaction.response.send_message("Unauthorized", ephemeral=True) - return - os.environ["REQUIRED_REACTIONS"] = str(reactions) - await interaction.response.send_message( - f"Required reactions set to {reactions}.", ephemeral=True - ) - - -class ResetSubGroup(app_commands.Group): - def __init__(self, check_admin, gemini_bot): - super().__init__(name="reset", description="Reset specific DuckBot settings.") - self.check_admin = check_admin - self.gemini_bot = gemini_bot - - @app_commands.command(name="chat-history", description="Reset Gemini chat history.") - async def reset_chat_history(self, interaction: Interaction): - if not await self.check_admin(interaction): - await interaction.response.send_message("Unauthorized", ephemeral=True) - return - - # Call the method to reset Gemini chat history - self.gemini_bot.clear_chat_history() - - await interaction.response.send_message( - "Gemini chat history has been reset.", ephemeral=True - ) diff --git a/src/commands/gemini.py b/src/commands/gemini.py index 9d518dd..79ad99c 100644 --- a/src/commands/gemini.py +++ b/src/commands/gemini.py @@ -347,18 +347,6 @@ async def query( return response_embeds - def clear_chat_history(self): - """Clears all conversation history for the chat context.""" - try: - # Clear the stored chat history if maintained within self.chat - if hasattr(self, "chat"): - self.chat.history = [] - logging.info("Gemini chat history has been successfully reset.") - else: - logging.warning("No chat history found to clear.") - except Exception as e: - logging.error(f"Error while resetting Gemini chat history: {e}") - async def upload_or_return_file_ref(attachment) -> (File, Errors): """Uploads the image to the Google Gemini Project diff --git a/src/main.py b/src/main.py index c06855f..55b840b 100644 --- a/src/main.py +++ b/src/main.py @@ -20,7 +20,7 @@ from dotenv import load_dotenv from constants.colours import LIGHT_YELLOW -from commands import gemini, skullboard, help_menu, admin_commands +from commands import gemini, skullboard, help_menu from utils import time # Load environment variables from .env file @@ -69,9 +69,6 @@ def __init__(self): api_key=GEMINI_API_KEY, ) - self.admin_commands = admin_commands.AdminCommands(gemini_bot=self.gemini_model) - self.tree.add_command(self.admin_commands, guild=Object(GUILD_ID)) - async def setup_hook(self): # Dynamically load all command groups from the commands directory for _, module_name, _ in pkgutil.iter_modules(["src/commands"]):