Skip to content
Advertisement

TypeError: on_message() missing 1 required positional argument: ‘message’

Hello I tried to make a bot with Discord.py and I tried implementing this to my code but I get the error in the title. I don’t know a lot of Python yet and I’m very new so I don’t know what may cause this error. Any help would be great, thanks!

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

Advertisement

Answer

Giving the full error log and the exact code run is good practice here.

This error indicates that the ‘on_message’ function was not given its ‘message’ argument. In this case, I would presume that you forgot to delete the ‘self’ argument when extracting this method from an object to make a stand-alone function out of it.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement