在 discord.py 中添加任何 unix 时间戳

问题描述 投票:0回答:1

我想像这样在赠品时间添加unix时间戳image

这里是代码:

@client.command()
async def giveaway(ctx, days:int, hours:int, minutes:int, seconds:int, prize:str):
  time = days + hours + minutes + seconds
  embed = discord.Embed(title=prize, description=f"time: <t:{time}:R>")
  await ctx.send(embed=embed, delete_after=time)
  endembed = discord.Embed(title=f"you have won {prize}", description="The time has ended")
  await ctx.send(embed=endembed)

当我运行代码时,它会显示这个image

python discord.py unix-timestamp
1个回答
1
投票

您可以通过使用时间模块中的

mktime
功能来做到这一点。

import datetime
import time

def convert_to_unix_time(date: datetime.datetime, days: int, hours: int, minutes: int, seconds: int) -> str:
    # Get the end date
    end_date = date + datetime.timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds)

    # Get a tuple of the date attributes
    date_tuple = (end_date.year, end_date.month, end_date.day, end_date.hour, end_date.minute, end_date.second)

    # Convert to unix time
    return f'<t:{int(time.mktime(datetime.datetime(*date_tuple).timetuple()))}:R>'

返回值应该像这样:

<t:1655392367:R>

粘贴返回的字符串将提供动态显示,如您提供的图像所示。

mktime
函数返回一个浮点值。要动态显示日期时间,其间的值必须仅为数字。

返回字符串末尾的

R
代表
relative
。动态显示将相对于当前时间。 其他格式是
t
,
T
,
d
,
D
,
f
and
F
.

© www.soinside.com 2019 - 2024. All rights reserved.