如何在discord.py v1.4.1中嵌入的每10秒编辑剩余时间

时间:2020-08-19 17:33:36

标签: python python-3.x discord discord.py discord.py-rewrite

这是我的代码-

@client.command()
@commands.guild_only()
@commands.has_permissions(administrator=True)
async def giveaway(ctx, duration: int, channel: discord.TextChannel, *, prize: str):
    giveaway_embed = discord.Embed(title=f"{str(prize)}",
                          description=f"Hosted by - {ctx.author.mention}\n"
                                      f"**React with :tada: to enter!**"
                                      f"Time Remaining: {duration} seconds",
                          color=ctx.guild.me.top_role.color,)
    embed_send = await channel.send(content=":tada: **GIVEAWAY** :tada:", embed=giveaway_embed)
    await embed_send.add_reaction("?")
    #Every 10 seconds I want to change the Time Remaining: field

我想每5秒钟编辑一次嵌入,直到赠品结束为止,我不太清楚该怎么做,非常感谢您的帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

要等待10秒钟,应使用asyncio.sleep(),然后使用Message.edit()方法:

from discord import Embed, TextChannel
from asyncio import sleep

@client.command()
@commands.guild_only()
@commands.has_permissions(administrator=True)
async def giveaway(ctx, duration: int, channel: TextChannel, *, prize: str):
    embed = Embed(title=prize,
                  description=f"Hosted by - {ctx.author.mention}\n"
                              f"**React with :tada: to enter!**"
                              f"Time Remaining: {duration} seconds",
                  color=ctx.guild.me.top_role.color,)

    msg = await ctx.channel.send(content=":tada: **GIVEAWAY** :tada:", embed=embed)
    await msg.add_reaction("?")

    while duration:
        await sleep(10)
        duration -= 10
        embed.description = f"Hosted by - {ctx.author.mention}\n**React with :tada: to enter!**\nTime Remaining: {duration} seconds"
        await msg.edit(embed=embed)

    await ctx.send("Giveaway is over!")

PS:我已经导入了TextChannelEmbed,所以我替换了discord.TextChanneldiscord.Embed