DiscordPy Extension 'cogs.economy' raised an error: TypeError: object NoneType can't be used in 'await' expression

Question:

I get the following error when i run my bot.

discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.economy' raised an error: TypeError: object NoneType can't be used in 'await' expression

I have the extension economy.py in the folder cogs. economy.py:

from discord.ext import commands


class Economy(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_message(self, message):
        if message.author != self.client.user:
            await message.channel.send("hello")

    @commands.command()
    async def ping(self, ctx):
        await ctx.send("pong")


def setup(client):
    client.add_cog(Economy(client))

and my client.py has:

import discord
from discord.ext import commands


intents = discord.Intents.all()
intents.message_content = True


prefix = "?"

client = commands.Bot(command_prefix=prefix, intents=intents)
@client.event
async def on_ready():
    await client.load_extension('hello')
    await client.load_extension('cogs.economy')
    print("Bot is ready")

client.run(token=token)

I have tried async-await for the setup but i still cant around this. This is my first time coding a bot. The ‘hello’ has just a command in it, and it works just fine. I have no idea how there is a NoneType object here.

Thanks.

Edit: Full Error

Traceback (most recent call last):
File "D:DocumentsPythonProjectsDiscordBotvenvlibsite-packagesdiscordextcommandsbot.py", line 946, in _load_from_module_spec
await setup(self)
TypeError: object NoneType can't be used in 'await' expression

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "D:DocumentsPythonProjectsDiscordBotvenvlibsite-packagesdiscordclient.py", line 409, in _run_event
await coro(*args, **kwargs)
File "D:DocumentsPythonProjectsDiscordBotexp_bot.py", line 51, in on_ready
await client.load_extension('cogs.economy')
File "D:DocumentsPythonProjectsDiscordBotvenvlibsite-packagesdiscordextcommandsbot.py", line 1012, in load_extension
await self._load_from_module_spec(spec, name)
File "D:DocumentsPythonProjectsDiscordBotvenvlibsite-packagesdiscordextcommandsbot.py", line 951, in _load_from_module_spec
raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.economy' raised an error: TypeError: object NoneType can't be used in 'await' expression
Asked By: zassar

||

Answers:

You didn’t show FULL error message but it shows problem with await setup(self)

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/discord/ext/commands/bot.py", line 946, in _load_from_module_spec
    await setup(self)
TypeError: object NoneType can't be used in 'await' expression

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/discord/client.py", line 409, in _run_event
    await coro(*args, **kwargs)
  File "/home/furas/main.py", line 18, in on_ready
    await client.load_extension('cogs.economy')
  File "/usr/local/lib/python3.10/dist-packages/discord/ext/commands/bot.py", line 1012, in load_extension
    await self._load_from_module_spec(spec, name)
  File "/usr/local/lib/python3.10/dist-packages/discord/ext/commands/bot.py", line 951, in _load_from_module_spec
    raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.economy' raised an error: TypeError: object NoneType can't be used in 'await' expression

and it may suggest that setup() has to use async.

When I use async def setup(client) then I gets different error

/home/furas/cogs/economy.py:19: RuntimeWarning: coroutine 'BotBase.add_cog' was never awaited
  client.add_cog(Economy(client))
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

which may suggest that add_cog() has to use await


Code works for me when I add async and await.

async def setup(client):
    await client.add_cog(Economy(client))

BTW:

Documentation for load_extension also shows async def setup()

Answered By: furas
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.