这个标题毫无意义,但我会在这里澄清。我有这个命令,它将发送一个嵌入并从 JSON 中获取数据,然后将其发送回嵌入内部,我想让它在 5 秒后删除并发送另一个 JSON。
代码如下:
@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)
当我使用该命令时,它会在 5 秒后发送另一个嵌入内容,但另一个嵌入内容直到几秒钟后才会删除。
如何解决这个问题?
最佳答案
await ctx.send(embed=embed, delete_after=5)
使用 asyncio.sleep
函数在删除消息之前等待。
您使用阻塞调用:time.sleep(5)
。它会阻止您的所有代码,还会阻止 await ctx.send(embed=embed, delete_after=5)
计时器执行。
您应该使用asyncio.sleep
:
import asyncio
@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)
await asyncio.sleep(5)
await ctx.send(embed=deletewarn)
关于python - 删除嵌入后发送另一个嵌入不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70687114/