discord.py: How to get age of user's account?

Question:

I want to check if the a user’s account is more than 30 days old when he joins (on_member_join). If not, the bot should send a message to a channel. I have problems how to get the account’s "age".

Example

bot = commands.Bot()

@bot.event
async def on_member_join(member):
    # this obviously doesn't work
    if not member.age.days > 30:
        channel = bot.get_channel(ID)
        await channel.send("Your account age is too young to join this server!")

Has anyone an idea?

Asked By: puncher

||

Answers:

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
>>> time.time() - myaccount.timestamp() > 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 time.time() - member.created_at.timestamp() < 2592000:
        # do stuff if the account is young #
    else:
        # do stuff if the account is not young #

Answered By: Eric Jin
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.