【问题标题】:TypeError: on_message() missing 1 required positional argument: 'message'类型错误:on_message() 缺少 1 个必需的位置参数:“消息”
【发布时间】:2021-05-03 16:02:31
【问题描述】:

您好,我尝试使用 Discord.py 制作机器人,并尝试在我的代码中实现 this,但标题中出现错误。我还不太了解 Python,而且我很新,所以我不知道是什么导致了这个错误。任何帮助都会很棒,谢谢!

import discord
import os
import requests
import json
import random
from keep_alive import keep_alive
import asyncio


client = discord.Client()
basura = ["rezero"]
verdad = [
    "es una mierda",
    "no debería existir",
    "VIVA K-ON",
]


def get_quote():
    response = requests.get("https://zenquotes.io/api/random")
    json_data = json.loads(response.text)
    quote = json_data[0]['q'] + " - " + json_data[0]['a']
    return (quote)


@client.event
async def on_ready():
  print ('We logged in as {0.user}'.format(client))

@client.event
async def on_message(self,message):
    if message.author.id == self.user.id:
        return

    if message.content.startswith('Hola'):
        await message.channel.send('Sup nerd')

    if message.content.startswith('Inspirame'):
        quote = get_quote()
        await message.channel.send(quote)
    if any(word in message.content for word in basura):
        await message.channel.send(random.choice(verdad))

    if message.content.startswith('^Guess'):
      await message.channel.send('Adivina un numero del 1 al 10')
      def is_correct(m):
        return message.content.author == message.author and m.content.isdigit()
      answer = random.randint(1,10)

      try:
        guess = await self.wait_for('message',check=is_correct,timeout=5.0)
      except asyncio.TimeoutError:
        return await message.channel.send('Se acabó el tiempo, la respuesta era {}.'.format(answer))
      if int(guess.content) == answer:
        await message.channel.send('Acertaste')
      else:
        await message.channel.send('Fallaste, la respuesta era {}.'.format(answer))

keep_alive()
client.run(os.getenv('TOKEN'))

【问题讨论】:

  • 类定义之外的self 参数实际上没有意义。 self 应该指的是什么? This answer 解释了如何使用 @on_message 装饰器。

标签: python discord.py


【解决方案1】:

在这里提供完整的错误日志和确切的代码运行是一种很好的做法。

这个错误表明'on_message'函数没有给出它的'message'参数。在这种情况下,我假设您在从对象中提取此方法以从中创建独立函数时忘记删除“self”参数。

【讨论】:

    猜你喜欢
    • 2021-01-16
    • 2021-05-10
    • 2021-05-19
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2018-09-12
    • 2021-08-05
    相关资源
    最近更新 更多