That title made no sense but I'll clarify here. I have this command where it'll send an embed and get data from a JSON and then send it back inside of the embed and I want to make it so that after 5 seconds, it deletes and sends another JSON.
Here's the code:
@client.command()
async def test(ctx):
with open('test.json', 'r') as homeworkfile:
homework = json.loads(homeworkfile.read())
embed=discord.Embed(title='Test', description=f"Here's the saved contents for test: {homework}", color=0x9932CC)
deletewarn=discord.Embed(title='Answer ticket expired.', description="Oops, looks like this ticket expired. Try saying !ss again?", color=0x9932CC)
await ctx.send(embed=embed, delete_after=5)
time.sleep(5)
await ctx.send(embed=deletewarn)
When I use the command, it does send the other embed after 5 seconds have passed, but the other embed doesn't delete until a few seconds later.
How do I fix this?
time.sleep
is blocking, meaning that all code execution is stopped until the 5 seconds pass. Instead, use asyncio.sleep
:
from asyncio import sleep
def my_func():
# code blah blah...
await sleep(5)
# code...