0

I'm new to python but trying to learn some things with it and discord.py ... but I ran in to this error:

Ignoring exception in on_message
Traceback (most recent call last):
  File "blabla/.local/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
TypeError: on_message() takes 1 positional argument but 2 were given

And here is my code:

from discord.ext import commands

# Bot responses
class BotResponses(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_message(message):
        if message.author == bot.user:
            return
        if message.content.startswith('hello'):
            await message.channel.send('Hello')
        await bot.process_commands(message)

def setup(bot):
    bot.add_cog(BotResponses(bot))

Am I doing something that adds an argument or something? And if so, is there a way to prevent it?

Marko
  • 11
  • 1

1 Answers1

1

You've created a commands.Cog which is a class. A method (a function inside a class) needs the parameter self.

So, your method should look like that:

@commands.Cog.listener()
async def on_message(self, message):
    if message.author == bot.user:
        return
    if message.content.startswith('hello'):
        await message.channel.send('Hello')
    await bot.process_commands(message)

Learn about Python classes here: https://www.w3schools.com/python/python_classes.asp
Learn about cogs here: https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html

puncher
  • 1,570
  • 4
  • 15
  • 38