LeQuit
LeQuit

Reputation: 57

TypeError. on_message() missing 1 required positional argument: 'ctx'

I try to make a auto response bot for something like automated support everytime i type: hey i

The bot outputs this Error:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\lequi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
TypeError: on_message() missing 1 required positional argument: 'ctx'

But i have did ctx in de function so i dont know whats wrong with the code I hope you guys can help me :D

Please help me if you can :D

@bot.event
async def on_message(message, ctx):
    if 'hey' in message.content.lower():
        if 'i' in message.content.lower():
            embed = discord.Embed(title=f'test',description=f'**test** : ``test``', color=0x001fff )
            await ctx.send(embed=embed)


    if 'test' in message.content.lower():
        embed = discord.Embed(title=f'test',description=f'**test** : ``test``', color=0x001fff )
        await ctx.send(embed=embed)```

Upvotes: 0

Views: 5109

Answers (2)

Deru
Deru

Reputation: 1172

The on_message only has 1 input argument (see documentation).

Thus your code:

@bot.event
async def on_message(message, ctx):
    if 'hey' in message.content.lower():
        if 'i' in message.content.lower():
            embed = discord.Embed(title=f'test',description=f'**test** : ``test``', color=0x001fff )
            await ctx.send(embed=embed)


    if 'test' in message.content.lower():
        embed = discord.Embed(title=f'test',description=f'**test** : ``test``', color=0x001fff )
        await ctx.send(embed=embed)

Is incorrect as you specify 2 input arguments, instead of 1 (i.e. you are adding a new required positional argument).


So now the question why does it say: TypeError. on_message() missing 1 required positional argument: 'ctx'?

Well it is very simple, the process that executes the on_message event doesnt have a second input argument for what you call "ctx", as it expected only 1 input argument and not two. Thus throwing this error.


Removing ctx will solve the problem, if not then you have other code that is interfering (something you havent showed). Example of how to do this below.

@bot.event
async def on_message(message):
    if 'hey' in message.content.lower():
        if 'i' in message.content.lower():
            embed = discord.Embed(title=f'test',description=f'**test** : ``test``', color=0x001fff )
            await message.channel.send(embed=embed)


    if 'test' in message.content.lower():
        embed = discord.Embed(title=f'test',description=f'**test** : ``test``', color=0x001fff )
        await message.channel.send(embed=embed)

Upvotes: 3

stijndcl
stijndcl

Reputation: 5647

According to the documentation on_message only takes 1 argument, being the message. To fix this you should just remove ctx as a parameter.

async def on_message(message):

But i have did ctx in de function so i dont know whats wrong with the code I hope you guys can help me :D

You have an extra parameter ctx, which shouldn't be there. As a result, Discord will call this event & never pass ctx in there, so the function called will be missing 1 argument, which throws that error. The error does not mean you didn't add it to the function signature, but that it wasn't passed into the function by whatever called it.

Upvotes: 1

Related Questions