Skey
Skey

Reputation: 45

Why do I get a TypeError that says: missing 1 required positional argument: 'message'

If I run this code:

    @commands.Cog.listener()
async def on_message(self, ctx, message):
    if message.content.startswith('/test'):
        ctx.send("Test")

I get this error:

TypeError: on_message() missing 1 required positional argument: 'message'

How can I fix it?

Upvotes: 0

Views: 3490

Answers (1)

moinierer3000
moinierer3000

Reputation: 1979

As it is written in the docs, on_message only takes 1 argument: message. Since your listener seems to be in a class, it will only take self and message.

If you remove the ctx from the arguments it should work. Obviously the ctx.send will not work then, you can use message.channel.send as a substitute. Also make sure to use await for sending a message.

@commands.Cog.listener()
async def on_message(self, message):
    if message.content.startswith('/test'):
        await message.channel.send("Test")

https://discordpy.readthedocs.io/en/master/api.html?highlight=on_message#discord.on_message

Upvotes: 1

Related Questions