eG8eb
eG8eb

Reputation: 1

Using Cogs In discord.py rewrite

I am experimenting with using cogs in discord.py and have followed a youtube guide on how to do it, I followed the guide however I am getting an error

This my main bot.py file

import discord
import os
from discord.ext import commands

client = commands.Bot(command_prefix ='!')

@client.command()
async def load(ctx, extension):
    client.load_extension(f'cogs.{extension}')

@client.command()
async def unload(ctx, extension):
    client.unload_extension(f'cogs.{extension}')

for filename in os.listdir('./cogs'):
    if filename.endswith('.py'):
        client.load_extension(f'cogs.{filename[:-3]}')


client.run('Token')

And this is my Cog example.py

import discord
from discord.ext import Commands

class Example(commands.Cog):

    def __init__(self, client):
        self.client = client

    #Events
    @commands.Cog.listener()
    async def on_ready(self):
        print('Bot is online.')

    #commands
    @commands.command()
    async def ping(self, ctx):
        await ctx.send('Pong!')

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

My Cog is in a folder called cogs in the same directory however when i run my bot.py file i get this error

    raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.example' raised an error: ImportError: cannot import name 'Commands' from 'discord.ext' (unknown location)

Upvotes: 0

Views: 368

Answers (1)

MrSpaar
MrSpaar

Reputation: 3994

Commands has to be in lowercase, since it's refering to discord.ext.commands, discord.py commands' module.

from discord.ext import commands

Upvotes: 1

Related Questions