Reputation: 1
Obviously some code is missing but here is all of it that should be needed to help.
import os
import discord
import requests
import json
import asyncio
channel_id = 791884298810163200
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
async def sendQuote():
channel = client.get_channel(channel_id)
await channel.send(get_quote())
async def background_task():
#if not 8am, wait until 8am (ignoring the seconds place, doesn't have to be exactly 8am)
await sendQuote() #fix this
await asyncio.sleep(5) #change this to the number of seconds in a twenty four hour period when done testing
await background_task()
if __name__ == "__main__":
client.loop.create_task(background_task())
keep_alive()
client.run(my_secret)
I have not added the wait until 8 am section yet as its not required in testing. if I move channel = client.get_channel(channel_id)
await channel.send(get_quote())
into on_ready() it prints the quote so im really not sure whats going wrong in sendQuote()
My error is:
Task exception was never retrieved future: <Task finished name='Task-1' coro=<background_task() done, defined at main.py:31> exception=AttributeError("'NoneType' object has no attribute 'send'")> Traceback (most recent call last): File "main.py", line 33, in background_task await sendQuote()
#fix this File "main.py", line 28, in sendQuote await channel.send(get_quote()) AttributeError: 'NoneType' object has no attribute 'send
Upvotes: 0
Views: 185
Reputation: 884
What you're doing isn't a good idea.
You're doing a recursive call of background_task()
this will work for some time, but you'll run into a recursion error earlier or later and your code will fail.
have a look into the Tasks
extension from discord.py this offers functions for reoccurring tasks, exactly as you want it :)
https://discordpy.readthedocs.io/en/latest/ext/tasks/index.html
Upvotes: 0
Reputation: 541
I executed your code and the only issue is not awaiting for client.wait_until_ready()
which I had mentioned in my comment above. Before the on_ready()
as the client/bot is still being setup, the channel = client.get_channel(channel_id)
returns None
which results in the below Attribute Error
.
AttributeError: 'NoneType' object has no attribute 'send'
Please find full info on the wait_until_ready
or on_ready
API calls in discordpy official documentation.
Below is the complete code with slight modifications that worked for me,
import os
import discord
import requests
import json
import asyncio
channel_id = 000000000000000000
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
async def sendQuote():
# wait till the client has executed on_ready().
await client.wait_until_ready()
channel = client.get_channel(channel_id)
await channel.send(get_quote())
async def background_task():
#if not 8am, wait until 8am (ignoring the seconds place, doesn't have to be exactly 8am)
await sendQuote() #fix this
await asyncio.sleep(5) #change this to the number of seconds in a twenty four hour period when done testing
await background_task()
if __name__ == "__main__":
client.loop.create_task(background_task())
#keep_alive() - unknown function code, so commented it in here.
client.run(my_secret)
Upvotes: 1