【问题标题】:How to send when an event has ended in discord.py当事件在 discord.py 中结束时如何发送
【发布时间】:2021-12-30 10:22:13
【问题描述】:

我正在尝试获得这样的输出,它会告诉您何时发送消息

消息发送时我成功获取

    emb = discord.Embed(title=f"**{title}**", description=f"<:854745395314163784:910908229897297940> Ends in {lenght}")
    emb.set_thumbnail(url='https://images-ext-2.discordapp.net/external/D14-NqrYBiv91a8nM8tVD4b0SuN-CAUlmooEq0dtWsM/https/kaj.gg/r/kq902alos9a.png?width=80&height=80')
    emb.set_footer(text = f"{winners} Winner")
    msg_2 = await ctx.send(embed=emb)
    await msg_2.add_reaction(emoji)
    id = msg_2.id
    created = msg_2.created_at
    print(created)

但我不知道如何返回“几秒钟前结束”或“几小时前结束”之类的消息

【问题讨论】:

    标签: python datetime timestamp discord.py python-datetime


    【解决方案1】:

    您发送的示例使用 Discord 的内置时间戳格式,概述为 here。您可以通过在msg_2.created_at 上使用 datetime 的 timestamp() 函数来获取 Unix 时间戳,例如:
    f"&lt;t:{int(msg_2.created_at.replace(tzinfo=datetime.timezone.utc).timestamp())}:R&gt;"
    然后,您可以正常将该字符串包含在消息中。

    【讨论】:

    • 显示 6 小时前结束,这与时区有关吗?
    • 是的,感谢您指出这一点,并为未将其包含在原始答案中而道歉。我现在已经更新了。
    • 没问题,很高兴我能帮上忙!
    猜你喜欢
    • 2011-10-25
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 2021-11-08
    • 2011-03-04
    • 1970-01-01
    相关资源
    最近更新 更多