Having trouble loading Cog commands in Discord bot main file

I’m building a Discord bot and trying to use cogs to organize my commands better. I want to put a kick command in a separate file and load it into my main bot file, but I keep getting errors when I try to run it.

Here’s my main bot file:

import discord
import os
from discord.ext import commands

client = commands.Bot(command_prefix="?", intents=discord.Intents.all())

async def setup_cogs():
    for file in os.listdir('./commands'):
        if file.endswith(".py"):
            extension_name = file[:-3]
            module_path = f'commands.{extension_name}'
            try:
                await client.load_extension(module_path)
                print(f"Successfully loaded: {extension_name}")
            except commands.ExtensionAlreadyLoaded:
                print(f"Module {extension_name} already loaded.")
            except commands.ExtensionNotFound:
                print(f"Could not find module: {extension_name}")

@client.event
async def on_ready():
    await client.change_presence(status=discord.Status.online, activity=discord.Game('Moderating Server'))
    print("Bot is online and ready!")
    await setup_cogs()

@client.command()
async def ping(ctx):
    await ctx.send(f"Pong! {ctx.author.mention}")

with open("bot_token.txt", "r") as f:
    bot_token = f.read().strip()
    
client.run(bot_token)

And here’s my kick command in the commands folder:

import discord
from discord.ext import commands

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

    @commands.command()
    @commands.has_permissions(kick_members=True)
    async def kick(self, ctx, user: discord.Member, *, reason="No reason provided"):
        await user.kick(reason=reason)
        await ctx.send(f"{user.display_name} has been kicked from the server!")

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

I keep getting parameter errors and the cog won’t load properly. What am I doing wrong with the setup?

looks like your setup function might be the issue - try changing it to just def setup(bot): instead of async. also make sure you have an __init__.py file in your commands folder or python wont recognize it as a module

The main problem I see is with your setup function signature. In newer versions of discord.py, the setup function should take bot as parameter, not client. Change your setup function to async def setup(bot): and then use await bot.add_cog(Moderation(bot)). Also noticed you’re calling setup_cogs() in on_ready which can cause timing issues - better to load extensions before running the bot. Move that setup_cogs() call right before client.run() and it should work much better. Had the same exact issue when I started using cogs and this fixed it completely.

Your cog structure looks correct but there’s a mismatch in parameter naming that’s causing the load failures. In your main file you’re passing client to the setup function, but discord.py expects consistent naming throughout. Change your setup function to async def setup(bot): and update the Moderation class to use def __init__(self, bot): with self.bot = bot instead of client. I had similar parameter errors when I mixed client/bot naming in my moderation cogs. Also worth checking that your commands folder has proper Python module structure - sometimes the module path resolution fails if the folder structure isn’t recognized correctly by the import system.