Merge pull request 'refactor: extract duplicate command hash logic into _compute_command_hash (#31)' (#65) from ai/major-domo-v2-31 into next-release

Reviewed-on: #65
This commit is contained in:
cal 2026-03-07 03:28:28 +00:00
commit 26a22aae4f

104
bot.py
View File

@ -236,43 +236,45 @@ class SBABot(commands.Bot):
f"❌ Failed to initialize background tasks: {e}", exc_info=True f"❌ Failed to initialize background tasks: {e}", exc_info=True
) )
def _compute_command_hash(self) -> str:
"""Compute a hash of the current command tree for change detection."""
commands_data = []
for cmd in self.tree.get_commands():
# Handle different command types properly
cmd_dict = {}
cmd_dict["name"] = cmd.name
cmd_dict["type"] = type(cmd).__name__
# Add description if available (most command types have this)
if hasattr(cmd, "description"):
cmd_dict["description"] = cmd.description # type: ignore
# Add parameters for Command objects
if isinstance(cmd, discord.app_commands.Command):
cmd_dict["parameters"] = [
{
"name": param.name,
"description": param.description,
"required": param.required,
"type": str(param.type),
}
for param in cmd.parameters
]
elif isinstance(cmd, discord.app_commands.Group):
# For groups, include subcommands
cmd_dict["subcommands"] = [subcmd.name for subcmd in cmd.commands]
commands_data.append(cmd_dict)
commands_data.sort(key=lambda x: x["name"])
return hashlib.sha256(
json.dumps(commands_data, sort_keys=True).encode()
).hexdigest()
async def _should_sync_commands(self) -> bool: async def _should_sync_commands(self) -> bool:
"""Check if commands have changed since last sync.""" """Check if commands have changed since last sync."""
try: try:
# Create hash of current command tree current_hash = self._compute_command_hash()
commands_data = []
for cmd in self.tree.get_commands():
# Handle different command types properly
cmd_dict = {}
cmd_dict["name"] = cmd.name
cmd_dict["type"] = type(cmd).__name__
# Add description if available (most command types have this)
if hasattr(cmd, "description"):
cmd_dict["description"] = cmd.description # type: ignore
# Add parameters for Command objects
if isinstance(cmd, discord.app_commands.Command):
cmd_dict["parameters"] = [
{
"name": param.name,
"description": param.description,
"required": param.required,
"type": str(param.type),
}
for param in cmd.parameters
]
elif isinstance(cmd, discord.app_commands.Group):
# For groups, include subcommands
cmd_dict["subcommands"] = [subcmd.name for subcmd in cmd.commands]
commands_data.append(cmd_dict)
# Sort for consistent hashing
commands_data.sort(key=lambda x: x["name"])
current_hash = hashlib.sha256(
json.dumps(commands_data, sort_keys=True).encode()
).hexdigest()
# Compare with stored hash # Compare with stored hash
hash_file = ".last_command_hash" hash_file = ".last_command_hash"
@ -292,39 +294,7 @@ class SBABot(commands.Bot):
async def _save_command_hash(self): async def _save_command_hash(self):
"""Save current command hash for future comparison.""" """Save current command hash for future comparison."""
try: try:
# Create hash of current command tree (same logic as _should_sync_commands) current_hash = self._compute_command_hash()
commands_data = []
for cmd in self.tree.get_commands():
# Handle different command types properly
cmd_dict = {}
cmd_dict["name"] = cmd.name
cmd_dict["type"] = type(cmd).__name__
# Add description if available (most command types have this)
if hasattr(cmd, "description"):
cmd_dict["description"] = cmd.description # type: ignore
# Add parameters for Command objects
if isinstance(cmd, discord.app_commands.Command):
cmd_dict["parameters"] = [
{
"name": param.name,
"description": param.description,
"required": param.required,
"type": str(param.type),
}
for param in cmd.parameters
]
elif isinstance(cmd, discord.app_commands.Group):
# For groups, include subcommands
cmd_dict["subcommands"] = [subcmd.name for subcmd in cmd.commands]
commands_data.append(cmd_dict)
commands_data.sort(key=lambda x: x["name"])
current_hash = hashlib.sha256(
json.dumps(commands_data, sort_keys=True).encode()
).hexdigest()
# Save hash to file # Save hash to file
with open(".last_command_hash", "w") as f: with open(".last_command_hash", "w") as f: