AmberD
AmberD

Reputation: 39

Coroutine 'get_quote' was never awaited

So, I have been unable to run this code to print a random quote for a discord bot I'm creating, I keep getting this error instead of the random quote:

Warning (from warnings module):
  File "C:\Users\amber\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\client.py", line 343
    await coro(*args, **kwargs)
RuntimeWarning: coroutine 'get_quote' was never awaited

Here's the code I'm using:

# bot.py
import discord
import os
import requests
import json

client = discord.Client()

async 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(f'{client.user} has connected to Discord!')

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

client.run("token here")

Upvotes: 0

Views: 240

Answers (1)

Harukomaze
Harukomaze

Reputation: 462

when we define a synchronous function

def somefunction(arg, arg2):
    # do something

we can simply call it with somefunction(input, input2)

when we define it like this,

async def somefunction(#some expected input):
     # do something

it's an asynchronous function, it's a coroutine that needs to be awaited

so we call it using await somfunction(#some input)

so as your error says, your "coroutine" get_quote function was never awaited

to call it, use await get_quote() instead of only get_quote()

Upvotes: 2

Related Questions