This commit is contained in:
pacnpal
2024-11-15 02:22:29 +00:00
parent 403b208abd
commit a72e4118e3

View File

@@ -16,21 +16,19 @@ class VideoArchiverCommands(commands.Cog):
self.processor = processor self.processor = processor
super().__init__() super().__init__()
async def cog_before_invoke(self, ctx: commands.Context) -> None:
"""This hook is called before every command."""
# For hybrid commands, we'll handle the interaction response here
if isinstance(ctx.command, commands.HybridCommand) and ctx.interaction:
await ctx.defer()
# Core Video Archiver Commands # Core Video Archiver Commands
@commands.hybrid_command(name="va_settings") @commands.hybrid_command(name="va_settings")
@app_commands.guild_only() @app_commands.guild_only()
@commands.admin_or_permissions(administrator=True) @commands.admin_or_permissions(administrator=True)
async def va_settings(self, ctx: commands.Context): async def va_settings(self, ctx: commands.Context):
"""Show current video archiver settings""" """Show current video archiver settings"""
# Defer the response immediately
if ctx.interaction:
await ctx.interaction.response.defer()
embed = await self.config.format_settings_embed(ctx.guild) embed = await self.config.format_settings_embed(ctx.guild)
if ctx.interaction:
await ctx.interaction.followup.send(embed=embed)
else:
await ctx.send(embed=embed) await ctx.send(embed=embed)
@commands.hybrid_command(name="va_update") @commands.hybrid_command(name="va_update")
@@ -38,33 +36,17 @@ class VideoArchiverCommands(commands.Cog):
@checks.is_owner() @checks.is_owner()
async def va_update(self, ctx: commands.Context): async def va_update(self, ctx: commands.Context):
"""Update yt-dlp to the latest version""" """Update yt-dlp to the latest version"""
if ctx.interaction:
await ctx.interaction.response.defer()
success, message = await self.update_checker.update_yt_dlp() success, message = await self.update_checker.update_yt_dlp()
response = "" + message if success else "" + message await ctx.send("" + message if success else "" + message)
if ctx.interaction:
await ctx.interaction.followup.send(response)
else:
await ctx.send(response)
@commands.hybrid_command(name="va_toggleupdates") @commands.hybrid_command(name="va_toggleupdates")
@app_commands.guild_only() @app_commands.guild_only()
@commands.admin_or_permissions(administrator=True) @commands.admin_or_permissions(administrator=True)
async def va_toggleupdates(self, ctx: commands.Context): async def va_toggleupdates(self, ctx: commands.Context):
"""Toggle yt-dlp update notifications""" """Toggle yt-dlp update notifications"""
if ctx.interaction:
await ctx.interaction.response.defer()
state = await self.config.toggle_setting(ctx.guild.id, "disable_update_check") state = await self.config.toggle_setting(ctx.guild.id, "disable_update_check")
status = "disabled" if state else "enabled" status = "disabled" if state else "enabled"
response = f"Update notifications {status}" await ctx.send(f"Update notifications {status}")
if ctx.interaction:
await ctx.interaction.followup.send(response)
else:
await ctx.send(response)
# Role Management Commands # Role Management Commands
@commands.hybrid_command(name="var_add") @commands.hybrid_command(name="var_add")
@@ -72,75 +54,44 @@ class VideoArchiverCommands(commands.Cog):
@app_commands.describe(role="The role to allow (leave empty for @everyone)") @app_commands.describe(role="The role to allow (leave empty for @everyone)")
async def var_add(self, ctx: commands.Context, role: Optional[discord.Role] = None): async def var_add(self, ctx: commands.Context, role: Optional[discord.Role] = None):
"""Add a role that's allowed to trigger archiving""" """Add a role that's allowed to trigger archiving"""
if ctx.interaction:
await ctx.interaction.response.defer()
if not role: if not role:
# If no role is specified, clear the list to allow everyone # If no role is specified, clear the list to allow everyone
await self.config.update_setting(ctx.guild.id, "allowed_roles", []) await self.config.update_setting(ctx.guild.id, "allowed_roles", [])
response = "Allowed role set to @everyone (all users can trigger archiving)" await ctx.send("Allowed role set to @everyone (all users can trigger archiving)")
else: return
await self.config.add_to_list(ctx.guild.id, "allowed_roles", role.id)
response = f"Added {role.name} to allowed roles"
if ctx.interaction: await self.config.add_to_list(ctx.guild.id, "allowed_roles", role.id)
await ctx.interaction.followup.send(response) await ctx.send(f"Added {role.name} to allowed roles")
else:
await ctx.send(response)
@commands.hybrid_command(name="var_remove") @commands.hybrid_command(name="var_remove")
@app_commands.guild_only() @app_commands.guild_only()
@app_commands.describe(role="The role to remove") @app_commands.describe(role="The role to remove")
async def var_remove(self, ctx: commands.Context, role: discord.Role): async def var_remove(self, ctx: commands.Context, role: discord.Role):
"""Remove a role from allowed roles""" """Remove a role from allowed roles"""
if ctx.interaction:
await ctx.interaction.response.defer()
await self.config.remove_from_list(ctx.guild.id, "allowed_roles", role.id) await self.config.remove_from_list(ctx.guild.id, "allowed_roles", role.id)
response = f"Removed {role.name} from allowed roles" await ctx.send(f"Removed {role.name} from allowed roles")
if ctx.interaction:
await ctx.interaction.followup.send(response)
else:
await ctx.send(response)
@commands.hybrid_command(name="var_list") @commands.hybrid_command(name="var_list")
@app_commands.guild_only() @app_commands.guild_only()
async def var_list(self, ctx: commands.Context): async def var_list(self, ctx: commands.Context):
"""List all roles allowed to trigger archiving""" """List all roles allowed to trigger archiving"""
if ctx.interaction:
await ctx.interaction.response.defer()
roles = await self.config.get_setting(ctx.guild.id, "allowed_roles") roles = await self.config.get_setting(ctx.guild.id, "allowed_roles")
if not roles: if not roles:
response = "No roles are currently set (all users can trigger archiving)" await ctx.send("No roles are currently set (all users can trigger archiving)")
else: return
role_names = [ role_names = [
r.name if r else "@everyone" r.name if r else "@everyone"
for r in [ctx.guild.get_role(role_id) for role_id in roles] for r in [ctx.guild.get_role(role_id) for role_id in roles]
] ]
response = f"Allowed roles: {', '.join(role_names)}" await ctx.send(f"Allowed roles: {', '.join(role_names)}")
if ctx.interaction:
await ctx.interaction.followup.send(response)
else:
await ctx.send(response)
@commands.hybrid_command(name="va_concurrent") @commands.hybrid_command(name="va_concurrent")
@app_commands.guild_only() @app_commands.guild_only()
@app_commands.describe(count="Number of concurrent downloads (1-5)") @app_commands.describe(count="Number of concurrent downloads (1-5)")
async def va_concurrent(self, ctx: commands.Context, count: app_commands.Range[int, 1, 5]): async def va_concurrent(self, ctx: commands.Context, count: app_commands.Range[int, 1, 5]):
"""Set the number of concurrent downloads""" """Set the number of concurrent downloads"""
if ctx.interaction:
await ctx.interaction.response.defer()
await self.config.update_setting(ctx.guild.id, "concurrent_downloads", count) await self.config.update_setting(ctx.guild.id, "concurrent_downloads", count)
response = f"Concurrent downloads set to {count}" await ctx.send(f"Concurrent downloads set to {count}")
if ctx.interaction:
await ctx.interaction.followup.send(response)
else:
await ctx.send(response)
# Channel Configuration Commands # Channel Configuration Commands
@commands.hybrid_command(name="vac_archive") @commands.hybrid_command(name="vac_archive")
@@ -148,85 +99,46 @@ class VideoArchiverCommands(commands.Cog):
@app_commands.describe(channel="The archive channel") @app_commands.describe(channel="The archive channel")
async def vac_archive(self, ctx: commands.Context, channel: discord.TextChannel): async def vac_archive(self, ctx: commands.Context, channel: discord.TextChannel):
"""Set the archive channel""" """Set the archive channel"""
if ctx.interaction:
await ctx.interaction.response.defer()
await self.config.update_setting(ctx.guild.id, "archive_channel", channel.id) await self.config.update_setting(ctx.guild.id, "archive_channel", channel.id)
response = f"Archive channel set to {channel.mention}" await ctx.send(f"Archive channel set to {channel.mention}")
if ctx.interaction:
await ctx.interaction.followup.send(response)
else:
await ctx.send(response)
@commands.hybrid_command(name="vac_notify") @commands.hybrid_command(name="vac_notify")
@app_commands.guild_only() @app_commands.guild_only()
@app_commands.describe(channel="The notification channel") @app_commands.describe(channel="The notification channel")
async def vac_notify(self, ctx: commands.Context, channel: discord.TextChannel): async def vac_notify(self, ctx: commands.Context, channel: discord.TextChannel):
"""Set the notification channel (where archive messages appear)""" """Set the notification channel (where archive messages appear)"""
if ctx.interaction:
await ctx.interaction.response.defer()
await self.config.update_setting(ctx.guild.id, "notification_channel", channel.id) await self.config.update_setting(ctx.guild.id, "notification_channel", channel.id)
response = f"Notification channel set to {channel.mention}" await ctx.send(f"Notification channel set to {channel.mention}")
if ctx.interaction:
await ctx.interaction.followup.send(response)
else:
await ctx.send(response)
@commands.hybrid_command(name="vac_log") @commands.hybrid_command(name="vac_log")
@app_commands.guild_only() @app_commands.guild_only()
@app_commands.describe(channel="The log channel") @app_commands.describe(channel="The log channel")
async def vac_log(self, ctx: commands.Context, channel: discord.TextChannel): async def vac_log(self, ctx: commands.Context, channel: discord.TextChannel):
"""Set the log channel for error messages and notifications""" """Set the log channel for error messages and notifications"""
if ctx.interaction:
await ctx.interaction.response.defer()
await self.config.update_setting(ctx.guild.id, "log_channel", channel.id) await self.config.update_setting(ctx.guild.id, "log_channel", channel.id)
response = f"Log channel set to {channel.mention}" await ctx.send(f"Log channel set to {channel.mention}")
if ctx.interaction:
await ctx.interaction.followup.send(response)
else:
await ctx.send(response)
@commands.hybrid_command(name="vac_monitor") @commands.hybrid_command(name="vac_monitor")
@app_commands.guild_only() @app_commands.guild_only()
@app_commands.describe(channel="The channel to monitor (leave empty to monitor all channels)") @app_commands.describe(channel="The channel to monitor (leave empty to monitor all channels)")
async def vac_monitor(self, ctx: commands.Context, channel: Optional[discord.TextChannel] = None): async def vac_monitor(self, ctx: commands.Context, channel: Optional[discord.TextChannel] = None):
"""Add a channel to monitor for videos""" """Add a channel to monitor for videos"""
if ctx.interaction:
await ctx.interaction.response.defer()
if not channel: if not channel:
# If no channel is specified, clear the list to monitor all channels # If no channel is specified, clear the list to monitor all channels
await self.config.update_setting(ctx.guild.id, "monitored_channels", []) await self.config.update_setting(ctx.guild.id, "monitored_channels", [])
response = "Now monitoring all channels for videos" await ctx.send("Now monitoring all channels for videos")
else: return
await self.config.add_to_list(ctx.guild.id, "monitored_channels", channel.id)
response = f"Now monitoring {channel.mention} for videos"
if ctx.interaction: await self.config.add_to_list(ctx.guild.id, "monitored_channels", channel.id)
await ctx.interaction.followup.send(response) await ctx.send(f"Now monitoring {channel.mention} for videos")
else:
await ctx.send(response)
@commands.hybrid_command(name="vac_unmonitor") @commands.hybrid_command(name="vac_unmonitor")
@app_commands.guild_only() @app_commands.guild_only()
@app_commands.describe(channel="The channel to stop monitoring") @app_commands.describe(channel="The channel to stop monitoring")
async def vac_unmonitor(self, ctx: commands.Context, channel: discord.TextChannel): async def vac_unmonitor(self, ctx: commands.Context, channel: discord.TextChannel):
"""Remove a channel from monitoring""" """Remove a channel from monitoring"""
if ctx.interaction:
await ctx.interaction.response.defer()
await self.config.remove_from_list(ctx.guild.id, "monitored_channels", channel.id) await self.config.remove_from_list(ctx.guild.id, "monitored_channels", channel.id)
response = f"Stopped monitoring {channel.mention}" await ctx.send(f"Stopped monitoring {channel.mention}")
if ctx.interaction:
await ctx.interaction.followup.send(response)
else:
await ctx.send(response)
# Video Format Commands # Video Format Commands
@commands.hybrid_command(name="va_format") @commands.hybrid_command(name="va_format")
@@ -234,95 +146,47 @@ class VideoArchiverCommands(commands.Cog):
@app_commands.describe(format="The video format (e.g., mp4, webm)") @app_commands.describe(format="The video format (e.g., mp4, webm)")
async def va_format(self, ctx: commands.Context, format: Literal["mp4", "webm"]): async def va_format(self, ctx: commands.Context, format: Literal["mp4", "webm"]):
"""Set the video format""" """Set the video format"""
if ctx.interaction:
await ctx.interaction.response.defer()
await self.config.update_setting(ctx.guild.id, "video_format", format.lower()) await self.config.update_setting(ctx.guild.id, "video_format", format.lower())
response = f"Video format set to {format.lower()}" await ctx.send(f"Video format set to {format.lower()}")
if ctx.interaction:
await ctx.interaction.followup.send(response)
else:
await ctx.send(response)
@commands.hybrid_command(name="va_quality") @commands.hybrid_command(name="va_quality")
@app_commands.guild_only() @app_commands.guild_only()
@app_commands.describe(quality="Maximum video quality in pixels (e.g., 1080)") @app_commands.describe(quality="Maximum video quality in pixels (e.g., 1080)")
async def va_quality(self, ctx: commands.Context, quality: app_commands.Range[int, 144, 4320]): async def va_quality(self, ctx: commands.Context, quality: app_commands.Range[int, 144, 4320]):
"""Set the maximum video quality""" """Set the maximum video quality"""
if ctx.interaction:
await ctx.interaction.response.defer()
await self.config.update_setting(ctx.guild.id, "video_quality", quality) await self.config.update_setting(ctx.guild.id, "video_quality", quality)
response = f"Maximum video quality set to {quality}p" await ctx.send(f"Maximum video quality set to {quality}p")
if ctx.interaction:
await ctx.interaction.followup.send(response)
else:
await ctx.send(response)
@commands.hybrid_command(name="va_maxsize") @commands.hybrid_command(name="va_maxsize")
@app_commands.guild_only() @app_commands.guild_only()
@app_commands.describe(size="Maximum file size in MB") @app_commands.describe(size="Maximum file size in MB")
async def va_maxsize(self, ctx: commands.Context, size: app_commands.Range[int, 1, 100]): async def va_maxsize(self, ctx: commands.Context, size: app_commands.Range[int, 1, 100]):
"""Set the maximum file size""" """Set the maximum file size"""
if ctx.interaction:
await ctx.interaction.response.defer()
await self.config.update_setting(ctx.guild.id, "max_file_size", size) await self.config.update_setting(ctx.guild.id, "max_file_size", size)
response = f"Maximum file size set to {size}MB" await ctx.send(f"Maximum file size set to {size}MB")
if ctx.interaction:
await ctx.interaction.followup.send(response)
else:
await ctx.send(response)
@commands.hybrid_command(name="va_toggledelete") @commands.hybrid_command(name="va_toggledelete")
@app_commands.guild_only() @app_commands.guild_only()
async def va_toggledelete(self, ctx: commands.Context): async def va_toggledelete(self, ctx: commands.Context):
"""Toggle whether to delete local files after reposting""" """Toggle whether to delete local files after reposting"""
if ctx.interaction:
await ctx.interaction.response.defer()
state = await self.config.toggle_setting(ctx.guild.id, "delete_after_repost") state = await self.config.toggle_setting(ctx.guild.id, "delete_after_repost")
response = f"Delete after repost: {state}" await ctx.send(f"Delete after repost: {state}")
if ctx.interaction:
await ctx.interaction.followup.send(response)
else:
await ctx.send(response)
@commands.hybrid_command(name="va_duration") @commands.hybrid_command(name="va_duration")
@app_commands.guild_only() @app_commands.guild_only()
@app_commands.describe(hours="Duration in hours (0 for permanent)") @app_commands.describe(hours="Duration in hours (0 for permanent)")
async def va_duration(self, ctx: commands.Context, hours: app_commands.Range[int, 0, 720]): async def va_duration(self, ctx: commands.Context, hours: app_commands.Range[int, 0, 720]):
"""Set how long to keep archive messages""" """Set how long to keep archive messages"""
if ctx.interaction:
await ctx.interaction.response.defer()
await self.config.update_setting(ctx.guild.id, "message_duration", hours) await self.config.update_setting(ctx.guild.id, "message_duration", hours)
response = f"Archive message duration set to {hours} hours" await ctx.send(f"Archive message duration set to {hours} hours")
if ctx.interaction:
await ctx.interaction.followup.send(response)
else:
await ctx.send(response)
@commands.hybrid_command(name="va_template") @commands.hybrid_command(name="va_template")
@app_commands.guild_only() @app_commands.guild_only()
@app_commands.describe(template="Message template using {author}, {url}, and {original_message}") @app_commands.describe(template="Message template using {author}, {url}, and {original_message}")
async def va_template(self, ctx: commands.Context, template: str): async def va_template(self, ctx: commands.Context, template: str):
"""Set the archive message template""" """Set the archive message template"""
if ctx.interaction:
await ctx.interaction.response.defer()
await self.config.update_setting(ctx.guild.id, "message_template", template) await self.config.update_setting(ctx.guild.id, "message_template", template)
response = f"Archive message template set to:\n{template}" await ctx.send(f"Archive message template set to:\n{template}")
if ctx.interaction:
await ctx.interaction.followup.send(response)
else:
await ctx.send(response)
# Site Management Commands # Site Management Commands
@commands.hybrid_command(name="vas_enable") @commands.hybrid_command(name="vas_enable")
@@ -330,13 +194,11 @@ class VideoArchiverCommands(commands.Cog):
@app_commands.describe(sites="Sites to enable (leave empty for all sites)") @app_commands.describe(sites="Sites to enable (leave empty for all sites)")
async def vas_enable(self, ctx: commands.Context, *, sites: Optional[str] = None): async def vas_enable(self, ctx: commands.Context, *, sites: Optional[str] = None):
"""Enable specific sites""" """Enable specific sites"""
if ctx.interaction:
await ctx.interaction.response.defer()
if sites is None: if sites is None:
await self.config.update_setting(ctx.guild.id, "enabled_sites", []) await self.config.update_setting(ctx.guild.id, "enabled_sites", [])
response = "All sites enabled" await ctx.send("All sites enabled")
else: return
site_list = [s.strip().lower() for s in sites.split()] site_list = [s.strip().lower() for s in sites.split()]
# Verify sites are valid # Verify sites are valid
@@ -344,24 +206,20 @@ class VideoArchiverCommands(commands.Cog):
valid_sites = set(ie.IE_NAME.lower() for ie in ydl._ies if hasattr(ie, 'IE_NAME')) valid_sites = set(ie.IE_NAME.lower() for ie in ydl._ies if hasattr(ie, 'IE_NAME'))
invalid_sites = [s for s in site_list if s not in valid_sites] invalid_sites = [s for s in site_list if s not in valid_sites]
if invalid_sites: if invalid_sites:
response = f"Invalid sites: {', '.join(invalid_sites)}\nValid sites: {', '.join(valid_sites)}" await ctx.send(
else: f"Invalid sites: {', '.join(invalid_sites)}\nValid sites: {', '.join(valid_sites)}"
await self.config.update_setting(ctx.guild.id, "enabled_sites", site_list) )
response = f"Enabled sites: {', '.join(site_list)}" return
if ctx.interaction: await self.config.update_setting(ctx.guild.id, "enabled_sites", site_list)
await ctx.interaction.followup.send(response) await ctx.send(f"Enabled sites: {', '.join(site_list)}")
else:
await ctx.send(response)
@commands.hybrid_command(name="vas_list") @commands.hybrid_command(name="vas_list")
@app_commands.guild_only() @app_commands.guild_only()
async def vas_list(self, ctx: commands.Context): async def vas_list(self, ctx: commands.Context):
"""List all available sites and currently enabled sites""" """List all available sites and currently enabled sites"""
if ctx.interaction:
await ctx.interaction.response.defer()
enabled_sites = await self.config.get_setting(ctx.guild.id, "enabled_sites") enabled_sites = await self.config.get_setting(ctx.guild.id, "enabled_sites")
embed = discord.Embed(title="Video Sites Configuration", color=discord.Color.blue()) embed = discord.Embed(title="Video Sites Configuration", color=discord.Color.blue())
with yt_dlp.YoutubeDL() as ydl: with yt_dlp.YoutubeDL() as ydl:
@@ -387,9 +245,6 @@ class VideoArchiverCommands(commands.Cog):
inline=False, inline=False,
) )
if ctx.interaction:
await ctx.interaction.followup.send(embed=embed)
else:
await ctx.send(embed=embed) await ctx.send(embed=embed)
# Queue Management Commands # Queue Management Commands
@@ -398,9 +253,6 @@ class VideoArchiverCommands(commands.Cog):
@commands.admin_or_permissions(administrator=True) @commands.admin_or_permissions(administrator=True)
async def vaq_status(self, ctx: commands.Context): async def vaq_status(self, ctx: commands.Context):
"""Show current queue status with basic metrics""" """Show current queue status with basic metrics"""
if ctx.interaction:
await ctx.interaction.response.defer()
status = self.processor.queue_manager.get_queue_status(ctx.guild.id) status = self.processor.queue_manager.get_queue_status(ctx.guild.id)
embed = discord.Embed( embed = discord.Embed(
@@ -433,10 +285,6 @@ class VideoArchiverCommands(commands.Cog):
) )
embed.set_footer(text="Use /vaq_metrics for detailed performance metrics") embed.set_footer(text="Use /vaq_metrics for detailed performance metrics")
if ctx.interaction:
await ctx.interaction.followup.send(embed=embed)
else:
await ctx.send(embed=embed) await ctx.send(embed=embed)
@commands.hybrid_command(name="vaq_metrics") @commands.hybrid_command(name="vaq_metrics")
@@ -444,9 +292,6 @@ class VideoArchiverCommands(commands.Cog):
@commands.admin_or_permissions(administrator=True) @commands.admin_or_permissions(administrator=True)
async def vaq_metrics(self, ctx: commands.Context): async def vaq_metrics(self, ctx: commands.Context):
"""Show detailed queue performance metrics""" """Show detailed queue performance metrics"""
if ctx.interaction:
await ctx.interaction.response.defer()
status = self.processor.queue_manager.get_queue_status(ctx.guild.id) status = self.processor.queue_manager.get_queue_status(ctx.guild.id)
metrics = status["metrics"] metrics = status["metrics"]
@@ -491,10 +336,6 @@ class VideoArchiverCommands(commands.Cog):
) )
embed.set_footer(text="Metrics are updated in real-time as videos are processed") embed.set_footer(text="Metrics are updated in real-time as videos are processed")
if ctx.interaction:
await ctx.interaction.followup.send(embed=embed)
else:
await ctx.send(embed=embed) await ctx.send(embed=embed)
@commands.hybrid_command(name="vaq_clear") @commands.hybrid_command(name="vaq_clear")
@@ -502,13 +343,5 @@ class VideoArchiverCommands(commands.Cog):
@commands.admin_or_permissions(administrator=True) @commands.admin_or_permissions(administrator=True)
async def vaq_clear(self, ctx: commands.Context): async def vaq_clear(self, ctx: commands.Context):
"""Clear the video processing queue for this guild""" """Clear the video processing queue for this guild"""
if ctx.interaction:
await ctx.interaction.response.defer()
cleared = await self.processor.queue_manager.clear_guild_queue(ctx.guild.id) cleared = await self.processor.queue_manager.clear_guild_queue(ctx.guild.id)
response = f"Cleared {cleared} items from the queue" await ctx.send(f"Cleared {cleared} items from the queue")
if ctx.interaction:
await ctx.interaction.followup.send(response)
else:
await ctx.send(response)