Home > Software engineering >  DiscordPy Extension 'cogs.economy' raised an error: TypeError: object NoneType can't
DiscordPy Extension 'cogs.economy' raised an error: TypeError: object NoneType can't

Time:08-29

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:\Documents\PythonProjects\DiscordBot\venv\lib\site-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 "D:\Documents\PythonProjects\DiscordBot\venv\lib\site-packages\discord\client.py", line 409, in _run_event
await coro(*args, **kwargs)
File "D:\Documents\PythonProjects\DiscordBot\exp_bot.py", line 51, in on_ready
await client.load_extension('cogs.economy')
File "D:\Documents\PythonProjects\DiscordBot\venv\lib\site-packages\discord\ext\commands\bot.py", line 1012, in load_extension
await self._load_from_module_spec(spec, name)
File "D:\Documents\PythonProjects\DiscordBot\venv\lib\site-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

CodePudding user response:

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

  • Related