fix: Improve command handling and defaults - Add fallback for hybrid commands - Make addmonitor and addrole use defaults - Fix async cog loading

This commit is contained in:
pacnpal
2024-11-15 00:40:12 +00:00
parent fa4c2ec208
commit 1588c5cb96
2 changed files with 25 additions and 17 deletions

View File

@@ -5,22 +5,23 @@ from typing import Optional
import yt_dlp import yt_dlp
from datetime import datetime from datetime import datetime
def setup(bot):
"""Load the VideoArchiverCommands cog."""
cog = VideoArchiverCommands(bot)
bot.add_cog(cog)
return cog
class VideoArchiverCommands(commands.Cog): class VideoArchiverCommands(commands.Cog):
"""Command handler for VideoArchiver""" """Command handler for VideoArchiver"""
def __init__(self, bot, config_manager, update_checker, processor): def __init__(self, bot, config_manager=None, update_checker=None, processor=None):
self.bot = bot self.bot = bot
self.config = config_manager self.config = config_manager
self.update_checker = update_checker self.update_checker = update_checker
self.processor = processor self.processor = processor
super().__init__() super().__init__()
async def cog_load(self) -> None: @commands.hybrid_group(name="videoarchiver", aliases=["va"], fallback="settings")
"""Initialize commands when cog loads"""
# Red-DiscordBot handles command syncing automatically
pass
@commands.hybrid_group(name="videoarchiver", aliases=["va"])
@commands.guild_only() @commands.guild_only()
@commands.admin_or_permissions(administrator=True) @commands.admin_or_permissions(administrator=True)
async def videoarchiver(self, ctx: commands.Context): async def videoarchiver(self, ctx: commands.Context):
@@ -48,10 +49,12 @@ class VideoArchiverCommands(commands.Cog):
@videoarchiver.command(name="addrole") @videoarchiver.command(name="addrole")
@commands.guild_only() @commands.guild_only()
async def add_allowed_role(self, ctx: commands.Context, role: discord.Role): async def add_allowed_role(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 (default: @everyone)"""
await self.config.add_to_list(ctx.guild.id, "allowed_roles", role.id) role_id = role.id if role else ctx.guild.default_role.id
await ctx.send(f"Added {role.name} to allowed roles") role_name = role.name if role else "@everyone"
await self.config.add_to_list(ctx.guild.id, "allowed_roles", role_id)
await ctx.send(f"Added {role_name} to allowed roles")
@videoarchiver.command(name="removerole") @videoarchiver.command(name="removerole")
@commands.guild_only() @commands.guild_only()
@@ -71,7 +74,8 @@ class VideoArchiverCommands(commands.Cog):
) )
return return
role_names = [ role_names = [
r.name for r in [ctx.guild.get_role(role_id) for role_id in roles] if r r.name if r else "@everyone"
for r in [ctx.guild.get_role(role_id) for role_id in roles]
] ]
await ctx.send(f"Allowed roles: {', '.join(role_names)}") await ctx.send(f"Allowed roles: {', '.join(role_names)}")
@@ -117,11 +121,15 @@ class VideoArchiverCommands(commands.Cog):
@videoarchiver.command(name="addmonitor") @videoarchiver.command(name="addmonitor")
@commands.guild_only() @commands.guild_only()
async def add_monitored_channel( async def add_monitored_channel(
self, ctx: commands.Context, channel: discord.TextChannel self, ctx: commands.Context, channel: Optional[discord.TextChannel] = None
): ):
"""Add a channel to monitor for videos""" """Add a channel to monitor for videos (default: all channels)"""
await self.config.add_to_list(ctx.guild.id, "monitored_channels", channel.id) if channel:
await ctx.send(f"Now monitoring {channel.mention} for videos") await self.config.add_to_list(ctx.guild.id, "monitored_channels", channel.id)
await ctx.send(f"Now monitoring {channel.mention} for videos")
else:
await self.config.update_setting(ctx.guild.id, "monitored_channels", [])
await ctx.send("Now monitoring all channels for videos")
@videoarchiver.command(name="removemonitor") @videoarchiver.command(name="removemonitor")
@commands.guild_only() @commands.guild_only()

View File

@@ -32,7 +32,7 @@ logger = logging.getLogger('VideoArchiver')
def setup(bot): def setup(bot):
"""Load the VideoArchiver cog.""" """Load the VideoArchiver cog."""
cog = VideoArchiver(bot) cog = VideoArchiver(bot)
bot.add_cog(cog) asyncio.create_task(bot.add_cog(cog))
return cog return cog
class VideoArchiver(VideoArchiverCommands): class VideoArchiver(VideoArchiverCommands):