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
await
keyword is used to call asynchronous functions or methods. Sinceload_extension
is not an asynchronous function, usingawait
with it will result in aTypeError
.Solution to it? By removing
await
keyword, you resolve this issue and ensure that the extensions are loaded correctly.Explaination: The
await
keyword 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_extension
is unnecessary and leads to aTypeError
.By removing
await
in this context, you ensure that thebot.load_extension
operation is executed synchronously and doesn't require awaiting a result, which is why the correction works.Correct Working Code:
Why did I changed that
else
blockprint
statement? While yourprint
statement wasn't wrong, and won't throw any errors but the purpose of thatelse
statement in your code is intended to provide a message when there are no.py
files found in the./cogs
directory. Theelse
part comes into play when theif
statement fails to find any.py
files to work with. Its purpose is to handle this situation by trying executing the specifiedelse
code. The condition for entering theelse
when 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.py
as per yourif
condition, The primary reason for the else is to handle the absence of.py
files in the specified directory.Hope it helps and I was able to explain you something!:D #KeepLearning ;) #Python