fix: Properly implement slash commands - Use app_commands for slash command support - Add proper command group structure - Handle command registration and syncing

This commit is contained in:
pacnpal
2024-11-15 00:45:13 +00:00
parent cb1394f413
commit ba71081687
3 changed files with 162 additions and 153 deletions

View File

@@ -2,7 +2,7 @@
from __future__ import annotations
import discord
from redbot.core import commands, Config, data_manager
from redbot.core import commands, Config, data_manager, app_commands
from pathlib import Path
import logging
import asyncio
@@ -29,13 +29,7 @@ from videoarchiver.exceptions import (
logger = logging.getLogger('VideoArchiver')
def setup(bot):
"""Load the VideoArchiver cog."""
cog = VideoArchiver(bot)
asyncio.create_task(bot.add_cog(cog))
return cog
class VideoArchiver(VideoArchiverCommands):
class VideoArchiver(commands.Cog):
"""Archive videos from Discord channels"""
def __init__(self, bot: commands.Bot) -> None:
@@ -82,8 +76,13 @@ class VideoArchiver(VideoArchiverCommands):
self.update_checker = UpdateChecker(self.bot, self.config_manager)
self.processor = VideoProcessor(self.bot, self.config_manager, self.components)
# Initialize base class last
super().__init__(self.bot, self.config_manager, self.update_checker, self.processor)
# Initialize commands handler
self.commands_handler = VideoArchiverCommands(
self.bot,
self.config_manager,
self.update_checker,
self.processor
)
# Initialize components for existing guilds
for guild in self.bot.guilds:
@@ -124,6 +123,10 @@ class VideoArchiver(VideoArchiverCommands):
# Wait for initialization to complete
await asyncio.wait_for(self.ready.wait(), timeout=30)
# Add commands to the bot
for command in self.commands_handler.videoarchiver.commands:
self.bot.tree.add_command(command)
except asyncio.TimeoutError:
await self._cleanup()
raise ProcessingError("Cog initialization timed out")
@@ -133,6 +136,10 @@ class VideoArchiver(VideoArchiverCommands):
async def cog_unload(self) -> None:
"""Clean up when cog is unloaded"""
# Remove commands from the bot
for command in self.commands_handler.videoarchiver.commands:
self.bot.tree.remove_command(command.name)
await self._cleanup()
async def _cleanup(self) -> None: