zone3dx
zone3dx

Reputation: 33

Discord.js days since account creation

Is there any way to give a user a certain role when they join the server, if they have been registered to discord for less than 10 days.

Upvotes: 1

Views: 8576

Answers (1)

luxmiyu
luxmiyu

Reputation: 615

Use the .createdAt property of User to determine their account age

When the guildMemberAdd event triggers, check the joining member's .createdAt property. You can then use .addRole() to give them a role.

// assuming you already have the `role` object or id
client.on("guildMemberAdd", member => {
  if (Date.now() - member.user.createdAt < 1000*60*60*24*10) {
    member.addRole(role);
  }
});

More detailed explanation:

  • guildMemberAdd will fire every time someone joins a server, this will pass on the member object.
  • We use the user object from that member to determine when the account was created via .createdAt.
  • Timestamps are stored in milliseconds, so 10 days is equivalent to 1000*60*60*24*10 milliseconds.
  • Compare these two timestamps, and if their account age is lower, then you give them a role.
  • We're assuming you already have the role object. Otherwise Guild.roles.get() is a good way to find a role by its ID.

Upvotes: 2

Related Questions