Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unknown error in latest discord.py rewrite

I am trying to create a discord bot in the new version of discord.py (latest version). I have been following an old tutorial for how to use cogs, but then used the discord.py API in order to update it with intents, etc. I cannot seem to shake the error in my code once it starts up. The error is

  File "C:\Users\MyUsername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\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

Here is the code for my main.py file:

import discord
import os
from discord.ext import commands

# Create bot
client = commands.Bot(command_prefix=">", intents=discord.Intents.all())


# Load commands
@client.command()
async def load(ctx, extension):
    await client.load_extension(f"Cogs.{extension}")


# Unload commands
@client.command()
async def unload(ctx, extension):
    await client.unload_extension(f"Cogs.{extension}")


# On start
@client.event
async def on_ready():
    # Import all commands on start
    for filename in os.listdir("./Cogs"):
        if filename.endswith(".py"):
            await client.load_extension(f"Cogs.{filename[:-3]}")

Client.run() excluded for obvious reasons, it is at the end though.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The only cog I have is this (Ping.py):

import discord
from discord.ext import commands
import time


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

    @commands.Cog.listener()
    async def on_ready(self):
        print("Bot is online")
        await self.client.change_presence(
            status=discord.Status.online,
            activity=discord.Game('SuperBot, >help for help')
        )

    @commands.command()
    async def ping(self, ctx):
        embed = discord.Embed(
            title="Pong!",
            description=f"I'm up! The bot latency is {self.client.latency}",
            color=discord.Colour.random()
        )
        embed.set_footer(
            text=time.strftime("%a : %H : %M")
        )
        await ctx.channel.send(embed=embed)


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

I have tried moving loading the cogs in the main file out of the on_ready func but that errors too. How can I resolve this error?

>Solution :

setup is async now.

Change it to this:

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

Read the changelog: https://discordpy.readthedocs.io/en/latest/migrating.html#migrating-2-0-commands-extension-cog-async

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading