Added explicit import of CommandAlreadyRegistered exception

Wrapped the command registration in a try-except block
Silently continues if the command is already registered
Maintains all other functionality including cog loading and task initialization
This commit is contained in:
pacnpal
2024-11-15 14:32:33 +00:00
parent 172fe83288
commit 245710e4d4

View File

@@ -2,6 +2,7 @@
from redbot.core.bot import Red from redbot.core.bot import Red
import logging import logging
import discord import discord
from discord.app_commands.errors import CommandAlreadyRegistered
from .birthday import Birthday, birthday_context_menu from .birthday import Birthday, birthday_context_menu
logger = logging.getLogger("Birthday") logger = logging.getLogger("Birthday")
@@ -11,9 +12,11 @@ async def setup(bot: Red) -> None:
try: try:
cog = Birthday(bot) cog = Birthday(bot)
await bot.add_cog(cog) await bot.add_cog(cog)
# Add context menu command if it doesn't exist # Try to add context menu command, ignore if already registered
if not discord.utils.get(bot.tree.get_commands(), name="Give Birthday Role"): try:
bot.tree.add_command(birthday_context_menu) bot.tree.add_command(birthday_context_menu)
except CommandAlreadyRegistered:
pass
# Initialize scheduled tasks # Initialize scheduled tasks
try: try:
await cog.reload_scheduled_tasks() await cog.reload_scheduled_tasks()