因此,标题简要说明了我想要的内容。
当成员加入我的 Discord 服务器时,我希望机器人自动为他们分配用户 Angular 色。在此之前,我想验证机器人是否有权管理 Angular 色,到目前为止,我的代码中有以下内容。
/** 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 channel 发布警报来引起工作人员的注意。
但是,我尝试加入一个新的 Discord 帐户,它在控制台中报告 hasPermission 不是一个函数,然后停止。
对于如何解决此代码有任何建议吗?
最佳答案
在 Discord.js 上,我们有用户和成员。用户是 Discord 的用户。成员是 Discord 服务器的成员。用户对象没有有关 Angular 色、昵称、权限的信息。成员这样做,因为他们与该服务器相关。
因此,如果您想将机器人作为公会成员,您需要使用如下内容:
<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);
关于javascript - 检查我的机器人是否有权管理角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51301089/