I made a bot with basic commands and a cog for every moderation command. When I start the bot, I only have access to the commands on main.py and not the ones in the cogs.
I used this load function:
@bot.event
async def load():
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
await bot.load_extension(f'cogs.{filename[:-3]}')
print(f"Loaded Cog: {filename[:-3]}")
else:
print("Unable to load pycache folder.")
none of the prints shows up in the console and i have no error
Here is the setup function in the cog:
async def setup(bot):
await bot.add_cog(bot)
Your code:
First Issue: You correctly identified that the event should be named
on_ready, which is triggered when the bot is connected and ready. Theload()event doesn't exist inpy-cord. See more about events in py-cord.Second Issue: The
awaitkeyword is used to call asynchronous functions or methods. Sinceload_extensionis not an asynchronous function, usingawaitwith it will result in aTypeError.Solution to it? By removing
awaitkeyword, you resolve this issue and ensure that the extensions are loaded correctly.Explaination: The
awaitkeyword is used to pause the execution of a coroutine until the awaited function or operation is completed. It's typically used with asynchronous operations that might take some time to finish, like making API requests or database queries. In your case, using await with a non-asynchronous function likebot.load_extensionis unnecessary and leads to aTypeError.By removing
awaitin this context, you ensure that thebot.load_extensionoperation is executed synchronously and doesn't require awaiting a result, which is why the correction works.Correct Working Code:
Why did I changed that
elseblockprintstatement? While yourprintstatement wasn't wrong, and won't throw any errors but the purpose of thatelsestatement in your code is intended to provide a message when there are no.pyfiles found in the./cogsdirectory. Theelsepart comes into play when theifstatement fails to find any.pyfiles to work with. Its purpose is to handle this situation by trying executing the specifiedelsecode. The condition for entering theelsewhen it didn't find any.py files, which is why it's important for understanding what's happening in the code. The reference to "pycache" not loading is not actually an issue here as it doesn't even end with.pyas per yourifcondition, The primary reason for the else is to handle the absence of.pyfiles in the specified directory.Hope it helps and I was able to explain you something!:D #KeepLearning ;) #Python