-1

Whenever i try to run my code for a test bot im using on discord, it comes up with the error

main.py:15: RuntimeWarning: coroutine 'BotBase.load_extension' was never awaited bot.load_extension(f"cogs.{filename[:-3]}") RuntimeWarning: Enable tracemalloc to get the object allocation traceback

My code is

import os
from discord.ext import commands
bot = commands.Bot(command_prefix="!c", intents=discord.Intents.all(), case_insensitive=True)

@bot.event
async def on_ready():
  print(f"{bot.user} is online!")

  


for filename in os.listdir("./cogs"):
  if filename.endswith(".py"):
    bot.load_extension(f"cogs.{filename[:-3]}")
    
bot.run("im not showing you my bot token lol")```



im using replit btw. what should i do?
  • 1
    Does this answer your question? ["RuntimeWarning: coroutine 'BotBase.load\_extension' was never awaited" after updating discord.py](https://stackoverflow.com/questions/71504627/runtimewarning-coroutine-botbase-load-extension-was-never-awaited-after-upd) – TheFungusAmongUs Sep 17 '22 at 23:33

1 Answers1

1

The error itself tells you what is wrong...

main.py:15: RuntimeWarning: coroutine 'BotBase.load_extension' was never awaited bot.load_extension(f"cogs.{filename[:-3]}") RuntimeWarning: Enable tracemalloc to get the object allocation traceback

The important part is "coroutine 'BotBase.load_extension' was never awaited"

discord.py uses coroutines all around so you have to learn to use await whenever it is needed.

Long story short, you must use await next to load_extension()...

Fixed code:

import os
from discord.ext import commands
bot = commands.Bot(command_prefix="!c", intents=discord.Intents.all(), case_insensitive=True)

@bot.event
async def on_ready():
  print(f"{bot.user} is online!")


for filename in os.listdir("./cogs"):
  if filename.endswith(".py"):
    await bot.load_extension(f"cogs.{filename[:-3]}")
    
bot.run("im not showing you my bot token lol")

That should work as long as there are no other problems in your code but it looks fine lol

Edit: That actually didn't work because you cannot use await outside a function, I came to this problem myself when updating my bot to v2.0, here's my workaround (I got help from people from dpy discord, good place to be aided):

async def main():
    async with bot:
        #   This is kinda the same as you had it before
        for filename in os.listdir('./cogs'):
            if filename.endswith('.py'):
                await bot.load_extension(f'cogs.{filename[:-3]}')
        await bot.start("token"))

asyncio.run(main())

This code should actually work or at least it did work for me lol, if you are following a youtube tutorial (which you shouldn't), it might be outdated so also remember to update your cog's code to keep the async stuff, you might need to update

def setup(client): # "client" or "bot", same thing
    client.add_cog(MyCog(client))

to

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

For future reference I would advise you to check the docs in https://discordpy.readthedocs.io/en/stable/ and look for dpy's discord server to ask questions like this :)

Fer-jg
  • 31
  • 4
  • 1
    it came with another error saying "SyntaxError: 'await' outside function" Im really new to python pls help – Adam Albustami Sep 18 '22 at 00:47
  • Actually after getting into v2.0 myself I found me in the same problem, the workaround I came to was a different one, I will update my answer ASAP – Fer-jg Sep 19 '22 at 05:29