因此,标题简要解释了我想要什么。
当成员加入我的Discord服务器时,我希望机器人自动为他们分配用户角色。在此之前,我想验证机器人是否有管理角色的权限,到目前为止,我的代码中有以下内容。
/** Event will trigger when a user joins the server **/
bot.on("guildMemberAdd", function(member) {
if (!bot.user.hasPermission("MANAGE_ROLES")) {
var embed2 = new discord.RichEmbed()
.setTitle("Error")
.setDescription("The bot doesn't have the appropriate permissions to assign new members roles automatically.nTo resolve this problem, go to **Server Settings** and then navigate to the **Roles** option. Next, click on **Bots**, and then click on the slider next to **Manage Roles** to activate it.")
.setColor("#3937a5")
bot.channels.get("466878539980079105").send(embed2);
} else {
member.addRole(member.guild.roles.find("name", "User"));
}
});
如果机器人没有权限,它会通过在#alert频道上发布警报来引起工作人员的注意。
然而,我试图加入一个新的Discord帐户,它在控制台中报告hasPermission不是一个函数,然后停止了。
关于如何解决此代码,有什么建议吗?
在Discord.js上,我们有用户和成员。用户是Discord的用户。成员是Discord服务器的成员。用户对象没有有关角色、昵称和权限的信息。成员会这样做,因为他们与该服务器相关
因此,如果你想以公会成员的身份获取机器人,你需要使用这样的东西:
<guild>.me
这将返回所选公会中机器人的成员对象
当一个成员被添加到公会时,你会得到GuildMember对象,同时你也会得到公会。所以你可以使用这个:
member.guild.me
最后检查机器人是否有权限:
if(member.guild.me.hasPermission("MANAGE_ROLES"))
注意:
hasPermission()
和hasPermissions()
将在DiscordJS v13中弃用,并替换为permissions.has()
例如:member.permissions.has(permissions.FLAGS.SEND_MESSAGES(;
错误消息是正确的,用户没有名为hasPermission
的函数。但角色属性确实如此。
https://discord.js.org/#/docs/main/stable/class/Role?scrollTo=hasPermission