Search code examples
pythonpython-3.xpycord

Pycord Error: discord.errors.ExtensionFailed: Extension 'cogs.cmds' raised an error: AttributeError: 'Bot' object has no attribute 'add_command'


I am trying to load a cog (located in the cogs directory) but everytime I try to load it I get the following error message:

discord.errors.ExtensionFailed: Extension 'cogs.cmds' raised an error: AttributeError: 'Bot' object has no attribute 'add_command'

I'm doing exacly how the tutorial shows, but it still doesn't work. Here is my code:

# main.py

import discord, os, dotenv

# setup
bot = discord.Bot(
    intents = discord.Intents.default(),
    prefix = "zrun "
)

@bot.event
async def on_ready():
    print(f'Connected as {bot.user}')

# load cmds
bot.load_extension('cogs.cmds')

# get token and run bot
dotenv.load_dotenv()
bot.run(os.getenv('TOKEN'))

The cog file:

# cogs/cmds.py

import discord
from discord.ext import commands

class Cmds(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def echo(self, ctx, msg:str):
        """Sends the specified text in the chat"""

        await ctx.send(msg)

def setup(bot):
    bot.add_cog(Cmds(bot))

Yes, I've searched StackOveflow, but no cog-related answers solved my problem.


Solution

  • The answer is really simple.

    discord.Bot doesn't support prefix commands, only application ones (like slash commands and context menus).

    If you want to use prefix commands you first need to import the commands extension by typing from discord.ext import commands then switching discord.Bot to commands.Bot where you define your bot.

    Also make sure to have the message_content intent as you need it to have prefix commands and I'm not sure if discord.Intents.default() was updated to include it.