首页 > 解决方案 > 赠品结束时的时间戳

问题描述

所以,我想在赠品结束时为赠品命令做一个时间戳。现在时间戳是 01.01.1970,我导入了 datetime 并做了一个转换系统。没有任何错误,所以它一定是我的代码。有什么想法我该怎么做?我不知道如何改变它

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(administrator=True)
async def gewinnspiel(ctx):
    await ctx.send(
        "**Beginnen wir mit diesem Gewinnspiel! Beantworten diese Fragen innerhalb von 15 Sekunden!**"
    )

questions = [
    "In welchem ​​Kanal soll es gehostet werden?",
    "Wie lange soll das Giveaway dauern? (s|m|h|d)",
    "Was ist der Preis des Giveaways?"
]

answers = []

def check(m):
    return m.author == ctx.author and m.channel == ctx.channel

for i in questions:
    await ctx.send(i)

    try:
        msg = await client.wait_for('message', timeout=15.0, check=check)
    except asyncio.TimeoutError:
        await ctx.send(
            '**Du hast nicht in zeit geantwortet! Bitte sei schneller.**')
        return
    else:
        answers.append(msg.content)
try:
    c_id = int(answers[0][2:-1])
except:
    await ctx.send(
        f"**Du hast den Kanal nicht richtig Makiert. Versuche es so: {ctx.channel.mention} nächstes mal.**"
    )
    return

channel = client.get_channel(c_id)

time = convert(answers[1])
if time == -1:
    await ctx.send(
        f"**Du hast die Zeit nicht mit einer richtigen Einheit beantwortet. Benutze beim nächsten Mal (s|m|h|d)!**"
    )
    return
elif time == -2:
    await ctx.send(
        f"**Die Zeit muss eine ganze Zahl sein. Bitte geb beim nächsten Mal eine ganze Zahl ein.**"
    )
    return

prize = answers[2]

await ctx.send(
    f"Das Gewinnspiel wird in {channel.mention} sein und dauert {answers[1]}!"
)

timestamp = time
embed = nextcord.Embed(
    title="Gewinnspiel!",
    description=
    f"Reagiere mit :tada: um teilzunehmen!\nPreis: {prize}\n Dauer: {answers[1]}\nHosted von: {ctx.author.mention}",timestamp=(datetime.datetime.utcfromtimestamp(timestamp)),
    color=ctx.author.color)

my_msg = await channel.send(embed=embed)

await my_msg.add_reaction('')

await asyncio.sleep(time)

new_msg = await channel.fetch_message(my_msg.id)

users = await new_msg.reactions[0].users().flatten()
users.pop(users.index(client.user))

winner = random.choice(users)

await channel.send(
    f"**Herzlichen Glückwunsch!**\n {winner.mention} hat {prize} Gewonnen!"
)

这是我的赠品代码

在此处输入图像描述

标签: pythondiscord.pybotsnextcord

解决方案


感谢@Tim Roberts Comment,我修复了它,首先我导入了时间,然后我将我的时间变量重命名为 Timer 并更改了 Timestamp = Value

import Time
...
timestamp = time.time() + Timer

推荐阅读