【问题标题】:Discord py message.createdatDiscord py message.createdat
【发布时间】:2021-03-24 14:08:22
【问题描述】:

我想知道如何格式化以下内容:

async def sendmessage():
channel = client.get_channel(786258218925293629)
fetchMessages = await channel.history().find(lambda m: m.author.id == 627862713242222632)
print(fetched.created_at)

我的最终目标是检查邮件是否在过去 30 分钟内发送,如果是,则删除该邮件。

我需要将其格式化为消息发送前的秒数。目前的输出是: 2020-12-13 20:19:24.414000

任何帮助将不胜感激。

【问题讨论】:

    标签: python discord.py python-3.8


    【解决方案1】:

    您可以通过从表示当前时间的 datetime 对象中减去 created_at datetime 对象来获得消息发送的秒数。执行此操作的代码应如下所示:

    import datetime # put this at the top of your code
    
    ...
    
    async def sendmessage():
        channel = client.get_channel(786258218925293629)
        fetchMessages = await channel.history().find(lambda m: m.author.id == 627862713242222632)
        # i'm not sure where the fetched variable is from so i'm assuming you just forgot to include that bit of code
        msg_secs = (datetime.datetime.now() - fetched.created_at).total_seconds()
        print(int(msg_secs))
    

    API 参考: datetime / timedelta object for datetime subtraction

    created_at property of message

    【讨论】:

    • 没问题。如果这有帮助,请不要忘记将此问题标记为已解决!
    • 完成 ^ 非常感谢,它运行良好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2021-06-04
    • 2021-05-28
    • 2021-11-22
    • 2018-08-19
    • 1970-01-01
    相关资源
    最近更新 更多