【发布时间】:2021-05-02 17:48:26
【问题描述】:
我试图创建一个带有赠品命令的不和谐机器人,该命令在赠品嵌入中每 1-2 分钟更新一次剩余时间。自 3 天以来我一直在尝试它,但无法找到解决方案。我设法让它更新秒数,但如果我指定时间超过 1m 即 60 秒,它会自动将其转换为秒并开始赠品,剩余时间为 just seconds 。我希望它保持给定单位的时间并以--days、--hours、--minutes、--seconds 为单位更新时间。
这里有几张图片我的意思:
目前在做什么:
我想要它做什么:
它只是结束或剩余时间我想要改变的!
我当前的代码:
@commands.command()
@commands.guild_only()
async def gstart(self, ctx, duration, *, prize):
time = self.convert(duration)
if time == -1:
await ctx.send(f'Answer Time With A Proper Unit (s, m, h, d)')
return
elif time == -2:
await ctx.send(f'Time Must Be A Integer!')
return
giveawayembed = discord.Embed(
title="???? New Giveaway! ????",
description=f"**Prize:** {prize}\n"
f"**Hosted By:** {ctx.author.mention}\n"
f"**Ends In:** {time} Seconds",
colour=discord.Color.green()
)
msg = await ctx.send(embed=giveawayembed)
reactions = await msg.add_reaction("????")
while time:
await sleep(10)
time -= 10
giveawayembed.description= f"**Prize:** {prize}\n**Hosted By:** {ctx.author.mention}\n**Ends In:** {time} Seconds"
await msg.edit(embed=giveawayembed)
new_msg = await ctx.fetch_message(msg.id)
users = await new_msg.reactions[0].users().flatten()
users.pop(users.index(self.client.user))
winner = random.choice(users)
endembed = discord.Embed(
title="Giveaway ended!",
description=f"Prize: {prize}\nWinner: {winner.mention}")
await msg.edit(embed=endembed)
await ctx.send(f"???? Giveaway Winner: {winner.mention} | Prize: {prize}")
为了转换我拥有的时间:
class Giveaway(commands.Cog):
def __init__(self, client):
self.client = client
def convert(self, time):
pos = ["s", "m", "h", "d"]
time_dict = {"s" : 1, "m" : 60, "h" : 3600, "d" : 3600*24}
unit = time[-1]
if unit not in pos:
return -1
try:
val = int(time[:-1])
except:
return -2
return val * time_dict[unit]
非常感谢任何帮助! ??? 如果我不能让你理解我的问题,我很抱歉。 ????
【问题讨论】:
标签: python python-3.x discord discord.py discord.py-rewrite