Noah
Noah

Reputation: 38

Discord.py on_message() missing argument

I have tried for a long time to add the missing argument, but my attempts have failed to work. I am currently not very good at Python, and the Code is not finished yet, so understand if I cannot explain something very well.

This is my error:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\...\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: 'self'

Here is part of my code:

import asyncio
import discord
from discord import Member

client = discord.Client()
regel_channel_id = someid

class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged in as')
        print(self.user.name)
        print(self.user.id)
        print('------')

@client.event
async def on_message(message, self):
    if '$artikel' in message.content:
        await message.channel.send('Question here')

        def artikelanswer(m):
            return m.author == message.author and m.content.isstring()
        try:
            Titel = await self.wait_for('message', check=artikelanswer, timeout=10.0)
        except asyncio.TimeoutError:
            return await message.channel.send('Sorry, you took too long!.')
        print(Titel)

client.run("SECRET TOKEN")

I would prefer if you could explain this problem as simply as possible. Thank you in advance.

Upvotes: 1

Views: 936

Answers (1)

Gugu72
Gugu72

Reputation: 2218

Uhh, the main problem is you are creating a MyClient class but not using it. Well, 2 solutions:

1. You want to use the MyClient class

So your code indentation is wrong, you need to indent the on_message() function properly.

import asyncio
import discord
from discord import Member

regel_channel_id = someid

class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged in as')
        print(self.user.name)
        print(self.user.id)
        print('------')

    async def on_message(self, message):
        if '$artikel' in message.content:
            await message.channel.send('Question here')

            def artikelanswer(m):
                return m.author == message.author and m.content.isstring()
            try:
                Titel = await self.wait_for('message', check=artikelanswer, timeout=10.0)
            except asyncio.TimeoutError:
                return await message.channel.send('Sorry, you took too long!.')
            print(Titel)

client = MyClient()
client.run("SECRET TOKEN")

2. You don't want to use the MyClient class

So you must not use self.

import asyncio
import discord
from discord import Member

client = discord.Client()
regel_channel_id = someid

@client.event
async def on_ready(self):
    print('Logged in as')
    print(self.user.name)
    print(self.user.id)
    print('------')

@client.event
async def on_message(message):
    if '$artikel' in message.content:
        await message.channel.send('Question here')

        def artikelanswer(m):
            return m.author == message.author and m.content.isstring()
        try:
            Titel = await self.wait_for('message', check=artikelanswer, timeout=10.0)
        except asyncio.TimeoutError:
            return await message.channel.send('Sorry, you took too long!.')
        print(Titel)

client.run("SECRET TOKEN")

Note that you should use @client.command decorator instead of looking for a command in the message, that would be really helpful. Please note that I just helped you for the MyClient class (or not using it) and your code might contain errors.

Upvotes: 2

Related Questions