簡體   English   中英

Discord.py on_message() 缺少參數

[英]Discord.py on_message() missing argument

我已經嘗試了很長時間來添加缺少的參數,但我的嘗試失敗了。 我目前對 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")

我希望你能盡可能簡單地解釋這個問題。 先感謝您。

呃,主要問題是你正在創建一個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類(或不使用它),您的代碼可能包含錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM