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?