Discord.js 帐户创建后的天数
如果用户注册discord不到10天,有什么方法可以在用户加入服务器时赋予他们特定的角色.
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.
推荐答案
使用 User 的 .createdAt 属性来确定他们的帐户年龄
Use the .createdAt property of User to determine their account age
当 guildMemberAdd 事件触发时,检查加入成员的 .createdAt 属性.然后你可以使用 .addRole() 给他们一个角色.
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);
}
});
更详细的解释:
guildMemberAdd将在每次有人加入服务器时触发,这将传递member对象.- 我们使用该成员的
user对象来确定帐户是何时通过.createdAt创建的. - 时间戳以毫秒为单位存储,因此 10 天相当于
1000*60*60*24*10毫秒. - 比较这两个时间戳,如果他们的帐户年龄较低,那么你就给他们一个角色.
- 我们假设您已经拥有
role对象.否则,Guild.roles.get()是通过 ID 查找角色的好方法.
guildMemberAddwill fire every time someone joins a server, this will pass on thememberobject.- We use the
userobject 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*10milliseconds. - Compare these two timestamps, and if their account age is lower, then you give them a role.
- We're assuming you already have the
roleobject. OtherwiseGuild.roles.get()is a good way to find a role by its ID.
相关文章