【发布时间】:2021-06-02 13:20:54
【问题描述】:
这是我的代码,时间没有被转换,我不知道该怎么办了。如果您知道该怎么做,请告诉我该怎么做
这是我目前得到的:
def convert(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]
#---------------------------------------------------------------------------
@client.command()
@commands.has_permissions(manage_messages = True)
async def giveaway(ctx, time : str, *, prize: str):
embed = discord.Embed(title=prize,
description=f"Hosted by - {ctx.author.mention}\nReact with :tada: to enter!\nTime Remaining: **{time}** seconds",
color=ctx.guild.me.top_role.color, )
msg = await ctx.channel.send(content=":tada: **GIVEAWAY** :tada:", embed=embed)
await msg.add_reaction("????")
await asyncio.sleep(3)
new_msg = await ctx.channel.fetch_message(msg.id)
user_list = [u for u in await new_msg.reactions[0].users().flatten() if u != client.user] # Check the reactions/don't count the bot reaction
if len(user_list) == 0:
await ctx.send("No one reacted.")
else:
winner = random.choice(user_list)
await ctx.send(f"{winner.mention} You have won the {prize}!")
当我输入 2m 表示 2 分钟时,它显示剩余 2m 秒,现在我知道为什么它显示秒了,因为我还没有更新响应,但时间只有 2 秒加上 3 秒延迟时间。基本上总共大约 6 秒。
我只是从堆栈溢出中抛出了 2 个命令,这就像放入兰博基尼头垫和道奇发动机缸体,我知道它不应该工作,即使稍作修改,我也确实看到了什么现在错了,但我不知道如何解决它
【问题讨论】:
-
我不知道为什么,但你只做了
await asyncio.sleep(3)。你不应该在await asyncio.sleep(3)之后添加await asyncio.sleep(int(convert(time)))吗? -
我刚刚编辑它,因为我刚刚意识到时间转换器与代码本身不同
标签: python discord.py