简体   繁体   English

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

[英]How to edit the time remaining every 10 seconds in an embed in discord.py v1.4.1

Here is my code -这是我的代码 -

@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

I want to edit the embed every 5 seconds until the giveaway ends, I cannot quite figure out how to do it, any help is much appreciated.我想每 5 秒编辑一次嵌入,直到赠品结束,我不知道该怎么做,非常感谢任何帮助。 Thanks!谢谢!

To wait 10 seconds, you should use asyncio.sleep() , then, there's a Message.edit() method:要等待 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: I've imported TextChannel and Embed so I replaced discord.TextChannel and discord.Embed . PS:我已经导入了TextChannelEmbed所以我替换discord.TextChanneldiscord.Embed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM