任务权限:在 Discord4J 中向成员添加角色时

huangapple go评论77阅读模式
英文:

Mission Permission When Adding a Role to a Member Discord4J

问题

在我的程序中,我尝试将一个角色添加给公会的(新)成员。但是当我尝试将该角色添加给成员时,我收到一个带有“缺少权限”消息的错误。令人困惑的是,机器人已经具有管理角色的权限。即使我赋予机器人管理员权限,但错误仍然出现。

Role studentRole = getRole(STUDENT_ROLE_NAME, tempMem.getGuild().block());
if(studentRole == null){
    tempMem.getPrivateChannel().block().createMessage("抱歉,服务器没有“学生”角色,请联系管理员寻求帮助!");
    return;
}
tempMem.addRole(studentRole.getId()).block();
tempMem.getPrivateChannel().block().createMessage("成功将您添加为学生!祝您玩得开心 :smile:");

对代码的进一步解释:
**Member tempMem:**应该接收学生角色的成员。
**Role getRole(String name, Guild guild):**是一个方法,它只是在公会的角色列表中查找,并返回具有给定名称的角色。此机器人所在的每个公会都应该有一个名为STUDENT_ROLE_NAME的角色。
此代码段应该只是获取学生角色,并在一切正常时将其分配给tempMem。

英文:

In my program I try to add a role to a (new) member of a guild. But when I try to add the role to the member I get an error with the message: "Missing Permissions". What's confusing is that the bot already has permissions to manage roles. After I gave the bot admin rights but the error still came up.


Role studentRole = getRole(STUDENT_ROLE_NAME, tempMem.getGuild().block());
if(studentRole == null){
		tempMem.getPrivateChannel().block().createMessage("Sorry the Server doesn't have a \"Student\" Role contact admins for help!");
		return;
}
tempMem.addRole(studentRole.getId()).block();
tempMem.getPrivateChannel().block().createMessage("Successfully added you as a Student! Have fun :smile:");

Some further explaination to the code:
Member tempMem: is the member which should recieve the student role.
Role getRole(String name, Guild guild): is a method which just looks through the list of roles in a guild and returns the Role which has the right name given as a parameter.
Every guild this bot is on should have a role with a name of STUDENT_ROLE_NAME.
This snippet should just get the Student-role and assign it to the tempMem if everything works correctly.

答案1

得分: 1

如上所述,我已经尝试赋予机器人比学生角色更高的权限,但解决方案实际上是将机器人的角色移动到学生角色之上。因此,缺少的权限错误实际上是正确的。

英文:

As already said above i tried giving the bot "higher" permissions than the student role, but the solution was litteraly to move the role of the bot over the role of a student.
So the missing permission error was actually correct

huangapple
  • 本文由 发表于 2020年8月17日 18:44:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63449260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定