How to get the age of someones account? Discord.py

眉间皱痕 提交于 2021-01-28 21:06:55

问题


To prevent new accounts from using a certain command, how could I get the age of a users account (or creation date) when they type a message.


client.get_user(account_date)

if account_date < 14:
   print('Your account must be older than 14 days to use this command')
else:
   print('Your account can use this command')

Any help is appreciated.


回答1:


You can get a User object from either ctx with ctx.author or from a message with message.author.

Then on the User object you can call created_at.

So for example to get account of current user

@bot.command()
async def CreatedAccountAt(self,ctx):
   await ctx.send(ctx.author.created_at)

or to get a user created date by ID

@bot.command()
async def CreatedAccountAt(ctx, userId: int):
    user =  bot.get_user(userId)
    await ctx.send(user.created_at)

https://discordpy.readthedocs.io/en/latest/api.html#discord.User.created_at



来源:https://stackoverflow.com/questions/61611157/how-to-get-the-age-of-someones-account-discord-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!