feat: Refactor command groups for TetoCog to improve structure and organization

This commit is contained in:
Slipstream 2025-05-27 21:39:21 -06:00
parent eef0d99817
commit 208cdb5948
Signed by: slipstream
GPG Key ID: 13E498CE010AC6FD

View File

@ -81,6 +81,17 @@ import os
import aiohttp
class TetoCog(commands.Cog):
# Define command groups at class level
ame_group = app_commands.Group(
name="ame",
description="Main command group for Ame-chan AI."
)
model_subgroup = app_commands.Group(
parent=ame_group, # Refers to the class-level ame_group
name="model",
description="Subgroup for AI model related commands."
)
def __init__(self, bot: commands.Bot):
self.bot = bot
self._api_endpoint = "https://openrouter.ai/api/v1/chat/completions" # Default endpoint
@ -94,11 +105,6 @@ class TetoCog(commands.Cog):
self.tavily_max_results = int(os.getenv("TAVILY_DEFAULT_MAX_RESULTS", "5"))
self._allow_web_search = bool(self.tavily_api_key) # Enable web search if API key is available
# Define the /ame command group
self.ame_group = app_commands.Group(name="ame", description="Main command group for Ame-chan AI.") # Slightly changed description
# Define the /ame model subgroup
self.model_subgroup = app_commands.Group(parent=self.ame_group, name="model", description="Subgroup for AI model related commands.") # Slightly changed description
async def _execute_shell_command(self, command: str) -> str:
"""Executes a shell command and returns its output, limited to first 5 lines."""
try:
@ -537,20 +543,20 @@ class TetoCog(commands.Cog):
await channel.send(f"**Teto AI conversation failed! TwT**\n{e}")
log.error(f"[TETO DEBUG] Exception during AI reply: {e}")
@self.ame_group.command(name="set_ai_model", description="Sets the AI model for Ame-chan.")
@model_subgroup.command(name="set", description="Sets the AI model for Ame-chan.")
@app_commands.describe(model_name="The name of the AI model to use.")
async def set_ai_model(self, interaction: discord.Interaction, model_name: str): # Ensuring self is the first param
async def set_ai_model(self, interaction: discord.Interaction, model_name: str):
self._ai_model = model_name
await interaction.response.send_message(f"Ame-chan's AI model set to: {model_name} desu~", ephemeral=True)
@self.ame_group.command(name="set_api_endpoint", description="Sets the API endpoint for Ame-chan.")
@ame_group.command(name="set_api_endpoint", description="Sets the API endpoint for Ame-chan.")
@app_commands.describe(endpoint_url="The URL of the API endpoint.")
async def set_api_endpoint(self, interaction: discord.Interaction, endpoint_url: str): # Ensuring self is the first param
async def set_api_endpoint(self, interaction: discord.Interaction, endpoint_url: str):
self._api_endpoint = endpoint_url
await interaction.response.send_message(f"Ame-chan's API endpoint set to: {endpoint_url} desu~", ephemeral=True)
@self.ame_group.command(name="clear_chat_history", description="Clears the chat history for the current channel.")
async def clear_chat_history(self, interaction: discord.Interaction): # Ensuring self is the first param
@ame_group.command(name="clear_chat_history", description="Clears the chat history for the current channel.")
async def clear_chat_history(self, interaction: discord.Interaction):
channel_id = interaction.channel_id
if channel_id in _teto_conversations:
del _teto_conversations[channel_id]
@ -558,14 +564,14 @@ class TetoCog(commands.Cog):
else:
await interaction.response.send_message("No chat history found for this channel desu~", ephemeral=True)
@self.ame_group.command(name="toggle_shell_command", description="Toggles Ame-chan's ability to run shell commands.")
async def toggle_shell_command(self, interaction: discord.Interaction): # Ensuring self is the first param
@ame_group.command(name="toggle_shell_command", description="Toggles Ame-chan's ability to run shell commands.")
async def toggle_shell_command(self, interaction: discord.Interaction):
self._allow_shell_commands = not self._allow_shell_commands
status = "enabled" if self._allow_shell_commands else "disabled"
await interaction.response.send_message(f"Ame-chan's shell command ability is now {status} desu~", ephemeral=True)
@self.ame_group.command(name="toggle_web_search", description="Toggles Ame-chan's ability to search the web.")
async def toggle_web_search(self, interaction: discord.Interaction): # Ensuring self is the first param
@ame_group.command(name="toggle_web_search", description="Toggles Ame-chan's ability to search the web.")
async def toggle_web_search(self, interaction: discord.Interaction):
if not self.tavily_api_key or not self.tavily_client:
await interaction.response.send_message("Web search is not available because the Tavily API key is not configured. Please set the TAVILY_API_KEY environment variable.", ephemeral=True)
return
@ -574,9 +580,9 @@ class TetoCog(commands.Cog):
status = "enabled" if self._allow_web_search else "disabled"
await interaction.response.send_message(f"Ame-chan's web search ability is now {status} desu~", ephemeral=True)
@self.ame_group.command(name="web_search", description="Search the web using Tavily API.")
@ame_group.command(name="web_search", description="Search the web using Tavily API.")
@app_commands.describe(query="The search query to look up online.")
async def web_search_command(self, interaction: discord.Interaction, query: str): # Ensuring self is the first param
async def web_search_command(self, interaction: discord.Interaction, query: str):
if not self.tavily_api_key or not self.tavily_client:
await interaction.response.send_message("Web search is not available because the Tavily API key is not configured. Please set the TAVILY_API_KEY environment variable.", ephemeral=True)
return
@ -610,8 +616,8 @@ class TetoCog(commands.Cog):
except Exception as e:
await interaction.followup.send(f"❌ Error performing web search: {str(e)}")
@self.model_subgroup.command(name="get", description="Gets the current AI model for Ame-chan.")
async def get_ai_model(self, interaction: discord.Interaction): # Ensuring self is the first param
@model_subgroup.command(name="get", description="Gets the current AI model for Ame-chan.")
async def get_ai_model(self, interaction: discord.Interaction):
await interaction.response.send_message(f"Ame-chan's current AI model is: {self._ai_model} desu~", ephemeral=True)
# Context menu command must be defined at module level
@ -656,6 +662,7 @@ async def teto_context_menu_ai_reply(interaction: discord.Interaction, message:
async def setup(bot: commands.Bot):
cog = TetoCog(bot)
await bot.add_cog(cog)
bot.tree.add_command(cog.ame_group) # Register the command group
bot.tree.add_command(teto_context_menu_ai_reply)
# bot.tree.add_command(cog.ame_group) # No longer needed if groups are class variables; discovery should handle it.
# Ensure the context menu is still added if it's not part of the cog's auto-discovery
bot.tree.add_command(teto_context_menu_ai_reply) # This is a module-level command, so it needs to be added.
print("TetoCog loaded! desu~")