[Python] Pycord cog loading gives TypeError: object NoneType can't be used in 'await'...

Discussão em 'Python' iniciado por Stack, Julho 14, 2025.

  1. Stack

    Stack Membro Participativo

    I'm writing a Discord bot using Pycord, and I'm trying to dynamically load cogs with an async def setup(bot) method.

    Here's the relevant part of my main.py:

    async def main():
    for filename in os.listdir("cogs"):
    if filename.endswith(".py"):
    cog_path = f"cogs.{filename[:-3]}"
    print(f" Importing {cog_path}")
    try:
    module = importlib.import_module(cog_path)
    setup = getattr(module, "setup", None)
    print(f" Checking setup in {cog_path}: {setup}")
    print(f" Type of setup: {type(setup)}")

    if setup:
    if inspect.iscoroutinefunction(setup):
    await setup(bot)
    else:
    print(f"⚠️ Skipped setup for {cog_path} because it's not async.")
    except Exception as e:
    print(f"❌ Failed to load {cog_path}: {e.__class__.__name__} - {e}")

    await bot.start(TOKEN)


    And here’s the trivia.py cog:

    async def setup(bot):
    await bot.add_cog(Trivia(bot))


    I get the following error at runtime:

    TypeError: object NoneType can't be used in 'await' expression


    Even though the setup function is async and the cog is implemented correctly.

    I'm using Pycord Version: 2.6.1 in a WSL environment, not discord.py, and I confirmed that setup is a coroutine function via inspect.iscoroutinefunction(setup).

    Questions


    1. Why would I be getting NoneType in await setup(bot) even when it's async?


    2. Is there something different about Pycord's cog loading I should be aware of?

    Continue reading...

Compartilhe esta Página