Discord.py on_message()缺少参数

时间:2020-03-21 23:40:10

标签: python message discord.py

我尝试添加缺少的参数已经很长时间了,但是我的尝试失败了。 我目前不太擅长Python,并且代码尚未完成,因此请理解我是否能很好地解释一些内容。

这是我的错误

<svg>

这是我的代码的一部分

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'

我希望您能尽可能简单地解释这个问题。预先谢谢你。

1 个答案:

答案 0 :(得分:2)

嗯,主要的问题是您正在创建MyClient类,但没有使用它。 好吧,有两种解决方案:

1。您想使用MyClient

因此您的代码缩进是错误的,您需要正确缩进on_message()函数。

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。您不想使用MyClient

因此,请勿使用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")

请注意,您应该使用@client.command装饰器,而不要在消息中查找命令,这将非常有帮助。请注意,我只是为您提供了MyClient类的帮助(或不使用它),您的代码可能包含错误。