Confusion between commands.Bot and discord.Client | Which one should I use?

Question:

Whenever you look at YouTube tutorials or code from this website there is a real variation. Some developers use client = discord.Client(intents=intents) while the others use bot = commands.Bot(command_prefix="something", intents=intents). Now I know slightly about the difference but I get errors from different places from my code when I use either of them and its confusing. Especially since there has a few changes over the years in discord.py it is hard to find the real difference.

I tried sticking to discord.Client then I found that there are more features in commands.Bot. Then I found errors when using commands.Bot.

An example of this is:

When I try to use commands.Bot

client = commands.Bot(command_prefix=">",intents=intents)

async def load():
    for filename in os.listdir("./Cogs"):
      if filename.endswith(".py"):
        client.load_extension(f"Cogs.{filename[:-3]}")

The above doesnt giveany response from my Cogs and also says

RuntimeWarning: coroutine 'BotBase.load_extension' was never awaited  
  client.load_extension(f"Cogs.{filename[:-3]}")
RuntimeWarning: Enable tracemalloc to get the object allocation traceback`.

Then when I try to use discord.Client

client = discord.Client(command_prefix=">",intents=intents)
async def load():
    for filename in os.listdir("./Cogs"):
      if filename.endswith(".py"):
        client.load_extension(f"Cogs.{filename[:-3]}")

The above also gives me an error: Exception has occurred: AttributeError 'Client' object has no attribute 'load_extension'

Which one is better in the long run? What is the exact difference?

Asked By: DRags

||

Answers:

The difference is that commands.Bot provides a lot more functionality (like Commands), which Client doesn’t do. Bot is a subclass of Client, so it can do everything that a Client can do, but not the other way around.

In the long run you should use the one that you need. If you want to use Bot features then use a Bot. If you don’t care about Bot features then use a Client, or you can still use a Bot and just not use the extra features.

The first error in your post says coroutine load_extension was never awaited, which tells you exactly what the problem is. load_extension is a coroutine (an async function), and you’re not awaiting it. Extensions were made async in 2.0, and you should adapt your code to that. Migration guide explains what to do: https://discordpy.readthedocs.io/en/stable/migrating.html#extension-and-cog-loading-unloading-is-now-asynchronous

The second error in your post appears because you’re trying to use a Bot feature (extensions) in a Client. It’s telling you that a Client doesn’t have load_extension(), because it doesn’t.

Considering you’re using out-of-date code I assume you’re following a YouTube tutorial. This is one of the major issues with them. They teach terrible code, and are instantly outdated. They also teach you to do one specific thing, and then you won’t be able to create any features of your own. Don’t follow YouTube tutorials, just read the docs.

Answered By: stijndcl
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.