I’m working on a Discord bot and keep getting a TypeError about a missing ‘ctx’ parameter. Here’s my current code:
import discord
import os
from dotenv import load_dotenv
from discord.ext import commands
import logging
class BotHandler:
def __init__(self) -> None:
self.permissions = discord.Intents.all()
self.permissions.messages = True
self.permissions.message_content = True
self.permissions.guilds = True
self.permissions.members = True
self.permissions.presences = True
self.client = commands.Bot(command_prefix='!', intents=self.permissions)
async def on_ready(self):
print("Bot is online.")
@commands.command()
async def greet(self, context):
await context.channel.send('Hi there!')
print("GREETING SENT!")
if __name__ == '__main__':
load_dotenv()
BOT_TOKEN = os.getenv('BOT_TOKEN')
logging.basicConfig(level=logging.DEBUG)
my_bot = BotHandler()
my_bot.client.add_command(my_bot.greet)
my_bot.client.run(BOT_TOKEN)
The error traceback shows:
ERROR:discord.ext.commands.bot:Ignoring exception in command greet
Traceback (most recent call last):
File "/lib/python3.12/site-packages/discord/ext/commands/core.py", line 235, in wrapped
ret = await coro(*args, **kwargs)
TypeError: BotHandler.greet() missing 1 required positional argument: 'context'
What’s causing this issue and how can I resolve it?