这是我有的代码,时间没有被转换,我不知道该怎么办了。如果您知道该怎么做,请告诉我该怎么做
这是我目前得到的:
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 个命令,这就像放入兰博基尼缸垫和道奇发动机缸体一样,我知道它不应该起作用,即使稍作修改,我也有点明白了现在错了,但我不知道如何解决
答案 0 :(得分:3)
所以我修改了您的代码并稍微更改了 wrap :: Integral n => n -> a -> [a] -> [a]
wrap _ y [] = [y]
wrap n y xs = y : (insertAfter n xs)
where
insertAfter 0 zs = wrap n y zs
insertAfter m [] = []
insertAfter m (z:zs) = z : insertAfter (m-1) zs
命令。经过一些修改后,该命令对我来说应该正常工作。这是我重新定义它的方式:
giveaway
答案 1 :(得分:0)
您可以使用它来转换时间。
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)