Added immediate deferral for all slash commands that perform async operations, preventing the "Unknown interaction" error

Improved the interaction response handling system to better manage both deferred and non-deferred responses
Enhanced error handling around interaction responses
Added proper fallback mechanisms for response handling
This commit is contained in:
pacnpal
2024-11-16 03:12:22 +00:00
parent e1d65ffcd0
commit 6aeb87fb40

View File

@@ -102,9 +102,9 @@ class VideoArchiver(GroupCog):
if ctx.interaction.response.is_done(): if ctx.interaction.response.is_done():
# Use followup for deferred responses # Use followup for deferred responses
if embed: if embed:
await ctx.followup.send(content=content, embed=embed, wait=True) await ctx.followup.send(content=content, embed=embed)
else: else:
await ctx.followup.send(content=content, wait=True) await ctx.followup.send(content=content)
else: else:
# Use regular response for non-deferred interactions # Use regular response for non-deferred interactions
if embed: if embed:
@@ -115,9 +115,9 @@ class VideoArchiver(GroupCog):
# If we get here, the interaction was already responded to # If we get here, the interaction was already responded to
# Use followup as a fallback # Use followup as a fallback
if embed: if embed:
await ctx.followup.send(content=content, embed=embed, wait=True) await ctx.followup.send(content=content, embed=embed)
else: else:
await ctx.followup.send(content=content, wait=True) await ctx.followup.send(content=content)
else: else:
# Regular command response # Regular command response
if embed: if embed:
@@ -290,6 +290,10 @@ class VideoArchiver(GroupCog):
async def set_archive_channel(self, ctx: Context, channel: discord.TextChannel): async def set_archive_channel(self, ctx: Context, channel: discord.TextChannel):
"""Set the channel where archived videos will be stored.""" """Set the channel where archived videos will be stored."""
try: try:
# Defer the response immediately for slash commands
if hasattr(ctx, 'interaction') and ctx.interaction:
await ctx.defer()
await self.config_manager.update_setting( await self.config_manager.update_setting(
ctx.guild.id, "archive_channel", channel.id ctx.guild.id, "archive_channel", channel.id
) )
@@ -305,6 +309,10 @@ class VideoArchiver(GroupCog):
async def set_log_channel(self, ctx: Context, channel: discord.TextChannel): async def set_log_channel(self, ctx: Context, channel: discord.TextChannel):
"""Set the channel where log messages will be sent.""" """Set the channel where log messages will be sent."""
try: try:
# Defer the response immediately for slash commands
if hasattr(ctx, 'interaction') and ctx.interaction:
await ctx.defer()
await self.config_manager.update_setting( await self.config_manager.update_setting(
ctx.guild.id, "log_channel", channel.id ctx.guild.id, "log_channel", channel.id
) )
@@ -320,6 +328,10 @@ class VideoArchiver(GroupCog):
async def add_enabled_channel(self, ctx: Context, channel: discord.TextChannel): async def add_enabled_channel(self, ctx: Context, channel: discord.TextChannel):
"""Add a channel to monitor for videos.""" """Add a channel to monitor for videos."""
try: try:
# Defer the response immediately for slash commands
if hasattr(ctx, 'interaction') and ctx.interaction:
await ctx.defer()
enabled_channels = await self.config_manager.get_setting( enabled_channels = await self.config_manager.get_setting(
ctx.guild.id, "enabled_channels" ctx.guild.id, "enabled_channels"
) )
@@ -343,6 +355,10 @@ class VideoArchiver(GroupCog):
async def remove_enabled_channel(self, ctx: Context, channel: discord.TextChannel): async def remove_enabled_channel(self, ctx: Context, channel: discord.TextChannel):
"""Remove a channel from video monitoring.""" """Remove a channel from video monitoring."""
try: try:
# Defer the response immediately for slash commands
if hasattr(ctx, 'interaction') and ctx.interaction:
await ctx.defer()
enabled_channels = await self.config_manager.get_setting( enabled_channels = await self.config_manager.get_setting(
ctx.guild.id, "enabled_channels" ctx.guild.id, "enabled_channels"
) )
@@ -366,6 +382,10 @@ class VideoArchiver(GroupCog):
async def set_video_format(self, ctx: Context, format: str): async def set_video_format(self, ctx: Context, format: str):
"""Set the video format for archived videos.""" """Set the video format for archived videos."""
try: try:
# Defer the response immediately for slash commands
if hasattr(ctx, 'interaction') and ctx.interaction:
await ctx.defer()
format = format.lower() format = format.lower()
if format not in ["mp4", "webm", "mkv"]: if format not in ["mp4", "webm", "mkv"]:
await self._handle_response(ctx, "Invalid format. Please use mp4, webm, or mkv.") await self._handle_response(ctx, "Invalid format. Please use mp4, webm, or mkv.")
@@ -384,6 +404,10 @@ class VideoArchiver(GroupCog):
async def set_video_quality(self, ctx: Context, quality: int): async def set_video_quality(self, ctx: Context, quality: int):
"""Set the video quality for archived videos.""" """Set the video quality for archived videos."""
try: try:
# Defer the response immediately for slash commands
if hasattr(ctx, 'interaction') and ctx.interaction:
await ctx.defer()
if not 144 <= quality <= 4320: if not 144 <= quality <= 4320:
await self._handle_response(ctx, "Quality must be between 144 and 4320.") await self._handle_response(ctx, "Quality must be between 144 and 4320.")
return return
@@ -401,6 +425,10 @@ class VideoArchiver(GroupCog):
async def set_max_file_size(self, ctx: Context, size: int): async def set_max_file_size(self, ctx: Context, size: int):
"""Set the maximum file size for archived videos.""" """Set the maximum file size for archived videos."""
try: try:
# Defer the response immediately for slash commands
if hasattr(ctx, 'interaction') and ctx.interaction:
await ctx.defer()
if not 1 <= size <= 100: if not 1 <= size <= 100:
await self._handle_response(ctx, "Size must be between 1 and 100 MB.") await self._handle_response(ctx, "Size must be between 1 and 100 MB.")
return return
@@ -418,6 +446,10 @@ class VideoArchiver(GroupCog):
async def set_message_duration(self, ctx: Context, hours: int): async def set_message_duration(self, ctx: Context, hours: int):
"""Set how long to keep archived messages.""" """Set how long to keep archived messages."""
try: try:
# Defer the response immediately for slash commands
if hasattr(ctx, 'interaction') and ctx.interaction:
await ctx.defer()
if not 0 <= hours <= 168: if not 0 <= hours <= 168:
await self._handle_response(ctx, "Duration must be between 0 and 168 hours (1 week).") await self._handle_response(ctx, "Duration must be between 0 and 168 hours (1 week).")
return return
@@ -435,6 +467,10 @@ class VideoArchiver(GroupCog):
async def set_message_template(self, ctx: Context, template: str): async def set_message_template(self, ctx: Context, template: str):
"""Set the template for archived messages.""" """Set the template for archived messages."""
try: try:
# Defer the response immediately for slash commands
if hasattr(ctx, 'interaction') and ctx.interaction:
await ctx.defer()
if not any( if not any(
ph in template for ph in ["{author}", "{channel}", "{original_message}"] ph in template for ph in ["{author}", "{channel}", "{original_message}"]
): ):
@@ -459,6 +495,10 @@ class VideoArchiver(GroupCog):
async def set_concurrent_downloads(self, ctx: Context, count: int): async def set_concurrent_downloads(self, ctx: Context, count: int):
"""Set the number of concurrent downloads allowed.""" """Set the number of concurrent downloads allowed."""
try: try:
# Defer the response immediately for slash commands
if hasattr(ctx, 'interaction') and ctx.interaction:
await ctx.defer()
if not 1 <= count <= 5: if not 1 <= count <= 5:
await self._handle_response(ctx, "Concurrent downloads must be between 1 and 5.") await self._handle_response(ctx, "Concurrent downloads must be between 1 and 5.")
return return
@@ -476,7 +516,7 @@ class VideoArchiver(GroupCog):
async def show_settings(self, ctx: Context): async def show_settings(self, ctx: Context):
"""Show current archiver settings.""" """Show current archiver settings."""
try: try:
# Defer the response immediately to prevent interaction timeout # Defer the response immediately for slash commands
if hasattr(ctx, 'interaction') and ctx.interaction: if hasattr(ctx, 'interaction') and ctx.interaction:
await ctx.defer() await ctx.defer()
@@ -492,6 +532,9 @@ class VideoArchiver(GroupCog):
@guild_only() @guild_only()
async def show_queue(self, ctx: Context): async def show_queue(self, ctx: Context):
"""Show the current video processing queue.""" """Show the current video processing queue."""
# Defer the response immediately for slash commands
if hasattr(ctx, 'interaction') and ctx.interaction:
await ctx.defer()
await self.processor.show_queue_details(ctx) await self.processor.show_queue_details(ctx)
async def cog_command_error(self, ctx: Context, error: Exception) -> None: async def cog_command_error(self, ctx: Context, error: Exception) -> None: