【问题标题】:Discord.py on_message() missing argumentDiscord.py on_message() 缺少参数
【发布时间】:2020-03-21 23:40:10
【问题描述】:

我已经尝试了很长时间来添加缺少的参数,但我的尝试都失败了。 我目前Python不太好,代码还没写完,如果我不能很好地解释,请理解。

这是我的错误

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'

这是我的部分代码

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")

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

【问题讨论】:

  • 只是on_message(message)
  • 但是这个命令不起作用?标题 = await self.wait_for('message', check=artikelanswer, timeout=10.0)
  • 在你的客户端上使用它client.wait_for(....discordpy.readthedocs.io/en/latest/…

标签: python message discord.py


【解决方案1】:

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

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 课程的帮助(或未使用它),您的代码可能包含错误。

【讨论】:

    猜你喜欢
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 2021-01-16
    相关资源
    最近更新 更多