检查我的机器人是否有权管理角色
Checking if my bot has permissions to manage roles
所以,标题简单说明了我想要什么。
当成员加入我的 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 不是一个函数,然后停止。
对于如何解决此代码有什么建议吗?
错误消息正确,用户没有调用 hasPermission
的函数。但角色 属性 确实如此。
https://discord.js.org/#/docs/main/stable/class/Role?scrollTo=hasPermission
在 Discord.js 上,我们有 用户 和 成员。 Users 是 Discord 的用户。成员是 Discord 服务器的成员。用户对象没有关于角色、昵称、权限的信息。成员会,因为他们与该服务器相关。
所以如果你想把机器人作为公会的成员,你需要使用这样的东西:
<guild>.me
这将 return 来自所选公会的机器人的成员对象。
当一个成员被添加到公会时,你会得到 GuildMember 对象,同时你也会得到公会。所以你可以使用这个:
member.guild.me
最后检查机器人是否有权限:
if(member.guild.me.hasPermission("MANAGE_ROLES"))
注:
hasPermission()
和 hasPermissions()
将在 DiscordJS v13 中弃用并替换为 permissions.has()
例如:member.permissions.has(Permissions.FLAGS.SEND_MESSAGES);
所以,标题简单说明了我想要什么。
当成员加入我的 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 不是一个函数,然后停止。
对于如何解决此代码有什么建议吗?
错误消息正确,用户没有调用 hasPermission
的函数。但角色 属性 确实如此。
https://discord.js.org/#/docs/main/stable/class/Role?scrollTo=hasPermission
在 Discord.js 上,我们有 用户 和 成员。 Users 是 Discord 的用户。成员是 Discord 服务器的成员。用户对象没有关于角色、昵称、权限的信息。成员会,因为他们与该服务器相关。
所以如果你想把机器人作为公会的成员,你需要使用这样的东西:
<guild>.me
这将 return 来自所选公会的机器人的成员对象。
当一个成员被添加到公会时,你会得到 GuildMember 对象,同时你也会得到公会。所以你可以使用这个:
member.guild.me
最后检查机器人是否有权限:
if(member.guild.me.hasPermission("MANAGE_ROLES"))
注:
hasPermission()
和 hasPermissions()
将在 DiscordJS v13 中弃用并替换为 permissions.has()
例如:member.permissions.has(Permissions.FLAGS.SEND_MESSAGES);