Unix 时间显示不正确 |下一曲

Unix Time not displaying correctly | NEXTCORD

我有一个带有赠品命令的机器人。

信息

API:Discord API 通过 Nextcord

语言:Python

解释时间(从人类时间 [1m | 1h | 1d] 到秒)的模块:humanfriendly

预期

我想要一种……呃……一种“结束于”的东西。 Discord 有一个内置的 Unix Time 东西,它的语法是这样的:

<t:UNIX:type>

Unix 是 UNIX 时间,type 是-嗯...类型。例如,R 作为相对时间

结果

嗯,我在用1m(1分钟)的时候遇到了这个:

Ends in [some whole 2 months!]

是的,那里好像是两个月。实际时间工作正常。赠品可以在一分钟内结束。但是我的问题是时间显示。

代码

@commands.command(name="gstart")
    @commands.has_permissions(manage_guild=True)
    async def gquickStart(self, ctx, gtime, *, gprize: str):
        if gtime == None:
            return await ctx.send("Include a time.")
        elif gprize == None:
            return await ctx.send("How are we gonna giveaway nothing?")
        gawtime = humanfriendly.parse_timespan(gtime)
        gawtimetuple = nextcord.utils.utcnow() + datetime.timedelta(gawtime)
        gwembed = nextcord.Embed(
            title=f"**:tada: {gprize} :tada:**",
            description=f"Ends in <t:{int(time.mktime(gawtimetuple.timetuple()))}:R> <t:{int(time.mktime(gawtimetuple.timetuple()))}:T> \n {ctx.author.mention} is giving away **{gprize}**!",
            color=nextcord.Colour.green())
        gwemend = nextcord.Embed(
        title=f"**:tada: GIVEAWAY ENDED :tada:**",
        description = f"{ctx.author.mention} has gave away **{gprize}**!",
            color=nextcord.Colour.red()
        )
        gwembed.set_footer(text=f"Giveaway ends in {gtime}")
        gaw_msg = await ctx.send(embed=gwembed)
        await ctx.message.delete()

        await gaw_msg.add_reaction('')

        await asyncio.sleep(gawtime)

        global new_gaw_msg
        new_gaw_msg = await ctx.channel.fetch_message(gaw_msg.id)

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

        try:
            winner = random.choice(users)
    
            await ctx.send(f"{winner.mention} has won the giveaway for **{gprize}**")
            await new_gaw_msg.edit(embed=gwemend)
        except IndexError:
            await new_gaw_msg.reply("1 Winner needed for the giveaway, 0 provided")
            await new_gaw_msg.edit(embed=gwemend)

顺便说一句,如果重要的话,我使用类别中的命令。

可以的话请回答

提前致谢,

满满的

解释

构造timedelta时,第一个位置参数是days。因此,通过将 timedelta(60) 添加到 utcnow,您将当前时间增加 60 天。

一个更简单的方法是简单地将 utcnow 转换为带有 .timestamp() 的浮点数,然后将其与 gawtime.

求和

代码

@commands.command(name="gstart")
@commands.has_permissions(manage_guild=True)
async def gquickStart(self, ctx, gtime, *, gprize: str):
    if gtime is None:
        return await ctx.send("Include a time.")
    elif gprize is None:
        return await ctx.send("How are we gonna giveaway nothing?")
    gawtime = humanfriendly.parse_timespan(gtime) 
    end_time = nextcord.utils.utcnow().timestamp() + gawtime
    gwembed = nextcord.Embed(
        title=f"**:tada: {gprize} :tada:**",
        description=f"Ends in <t:{int(end_time)}:R> <t:{int(end_time)}:T> \n {ctx.author.mention} is giving away **{gprize}**!",
        color=nextcord.Colour.green()
    )

不相关:来自 PEP 8

Comparisons to singletons like None should always be done with is or is not, never the equality operators.

参考

parse_timestamp

utcnow

timedelta