Discord.js days since account creation(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.
这篇关于Discord.js 帐户创建后的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Discord.js 帐户创建后的天数
基础教程推荐
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
