Trying to make a discord bot but on.message or message.content not working

Question:

I want the bot to use the gpt-3 API to answer questions but for some reason on.message is not working

import openai
import discord


openai.api_key = "apikey"

client = discord.Client()

@client.event

async def on_ready():
 print('online')

async def on_message(message):
   
    if message.content.startswith("!ask"):
        print('I read the message')
        

        question = message.content[5:]

        response = openai.Completion.create(
            engine="text-davinci-002",
            prompt=f"{question}n",
            temperature=0.7,
            max_tokens=1024,
            top_p=1,
            frequency_penalty=0,
            presence_penalty=0
        )

        await message.channel.send(response.choices[0].text)


client.run('token')

Everything works fine and the ‘online’ appears but after that i don’t know what’s happening since i am not getting any errors (Sorry if its something obvious i am just now learning)

the

client.run('token')
open.api_key="apikey"

are obviously replaced with the real ones in my code

Asked By: panoskava

||

Answers:

You need to use @client.event for every event

@client.event
async def on_ready():
 print('online')

@client.event # <- this is new
async def on_message(message):
   # ...
Answered By: Nawaf

If you want to register an event, you have to put the client.event decorator on top of the event functions. Your on_message function certainly doesn’t have one, so just put one on it.

@client.event
async def on_message(message):
    ...
Answered By: Catgal