如何在discord.py中检查用户的创建日期?

我想检查一个用户的帐户在加入时是否已超过30天。如果不是,则该漫游器应向频道发送一条消息。我在检查帐户的“年龄”时遇到问题。

有人知道吗?

评论
  • 叫我女王
    叫我女王 回复

    From the discord.py documentation, you can use the created_at attribute of a discord.User or discord.Member class. It will return a datetime.datetime object.

    >>> myaccount = client.get_user(my_id)
    >>> myaccount.created_at
    datetime.datetime(2013, 8, 6, 14, 22, 14)
    >>> myaccount.timestamp()
    1375813334.0
    >>> myaccount.timestamp() - time.time() > 2592000 # 2592000 seconds is 30 days
    True
    

    You can incorporate this into an on_member_join client event.

    @client.event
    async def on_member_join(member):
        if member.created_at.timestamp() - time.time() < 2592000:
            # do stuff if the account is young #
        else:
            # do stuff if the account is not young #