Looking for a fix to an error in the tutorial 'Code a Discord bot with python'

I have just started learning how to make bots using python in discord, and I have been following the tutorial of freecodecamp for it. But just around 26 minutes in, I already have a problem. I have done the part of adding the command ‘$inspire’ which should give a random quote every time anyone uses it. However, whenever I use the command, nothing comes up in discord, instead, I get an error in the console saying,

'Ignoring exception in on_message
Traceback (most recent call last):
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py”, line 343, in _run_event
await coro(*args, **kwargs)
File “main.py”, line 25, in on_message
quote = get_quote()
File “main.py”, line 11, in get_quote
json_data = json.loads(response.text)
AttributeError: ‘function’ object has no attribute ‘text’

My code is given below,

import discord
import os
import requests
import json

client = discord.Client()

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 have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
  if message.author == client.user:
    return
  
  if message.content.startswith('$inspire'):
    quote = get_quote()
    await message.channel.send(quote)

client.run(os.getenv('Token'))

Please help me :confused:

Is the ("https://zenquotes.io/api/random") in same line as requests.get ?
request.get(<url>) is essentially a function call. I think that since the brackets are on the next line, python assigned the value of response to requests.get the function. rather than calling and storing the values returned by the function.
I think this should fix the error-

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)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.