赠送命令时间转换错误



这是我的代码,时间没有被转换,我不知道该怎么做了。如果你知道该怎么做,让我知道怎么做

这是我到目前为止得到的:


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个命令在一起,这就像把兰博基尼头垫片和道奇发动机块,我知道它不应该工作,即使有一点点修改,我有点看到现在有什么问题,但我不知道如何修复它

所以我修改了你的代码并稍微改变了giveaway命令。经过一些修改后,这个命令对我来说就像它应该的那样工作了。我是这样重新定义它的:

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):
time = convert(time)
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)
await asyncio.sleep(int(time))
new_msg = await ctx.channel.fetch_message(msg.id)
user_list = [user for user in await new_msg.reactions[0].users().flatten() if
user != 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}!")

您可以使用它来转换时间。

import re
from discord.ext.commands import BadArgument
time_regex = re.compile(r"(?:(d{1,5})(h|s|m|d))+?")
time_dict = {"h": 3600, "s": 1, "m": 60, "d": 86400}

def convert(argument):
args = argument.lower()
matches = re.findall(time_regex, args)
time = 0
for key, value in matches:
try:
time += time_dict[value] * float(key)
except:
raise BadArgument
return round(time)

最新更新