Cogs aren't loading pycord

171 views Asked by At

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)
2

There are 2 answers

0
Akitoo On

Your code:

@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.")

First Issue: You correctly identified that the event should be named on_ready, which is triggered when the bot is connected and ready. The load() event doesn't exist in py-cord. See more about events in py-cord.

Second Issue: The await keyword is used to call asynchronous functions or methods. Since load_extension is not an asynchronous function, using await with it will result in a TypeError.

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 like bot.load_extension is unnecessary and leads to a TypeError.

By removing await in this context, you ensure that the bot.load_extension operation is executed synchronously and doesn't require awaiting a result, which is why the correction works.

Correct Working Code:

@bot.event #A bot event in pycord.
async def on_ready(): #when bot is connected and ready.
    for filename in os.listdir('./cogs'): #checking files inside the ./cogs directory.
        if filename.endswith('.py'): #checking for .py files inside ./cogs directory.
            bot.load_extension(f'cogs.{filename[:-3]}') #loading every .py files.
            print(f"Loaded Cog: {filename[:-3]}")
        else:
            print("No python(.py) files found!") #if can't find any .py files.

Why did I changed that else block print statement? While your print statement wasn't wrong, and won't throw any errors but the purpose of that else statement in your code is intended to provide a message when there are no .py files found in the ./cogs directory. The else part comes into play when the if statement fails to find any .py files to work with. Its purpose is to handle this situation by trying executing the specified else code. The condition for entering the else 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 your if 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

0
Yasir On

You are using the incorrect decorator.

Use:

@bot.command()

Also, please ensure you have enabled the 'Message Content' intent on your bot application. Read more on gateway intents here.

You can also learn more on creating Pycord commands here.