如何在测试频道中设置角色列表的权限? 我试过了,但它只设置了最后一个角色:
Category category = guild.getCategoryById(Categ);
TextChannel channel1 = guild.createTextChannel("ticket-" + EndlessBungeeTickets.instance.sql.getTicket(username)).setParent(category).addPermissionOverride(guild.getMember(user), EnumSet.of(Permission.VIEW_CHANNEL, Permission.MESSAGE_WRITE), null).addPermissionOverride(guild.getPublicRole(), null, EnumSet.of(Permission.VIEW_CHANNEL, Permission.MESSAGE_WRITE)).complete();
//RolesWR is equal to a list of roles for ex. [738036146151358504, 814517406067589122, 789617469605281843]
//But it only sets permission to the last role of the list
RolesWR.forEach(ruolo -> {
System.out.println(ruolo);
channel1.getManager().putPermissionOverride(guild.getRoleById(ruolo), EnumSet.of(Permission.VIEW_CHANNEL, Permission.MESSAGE_WRITE), null).queue();
});
答案 0 :(得分:1)
您应该在调用 queue 之前添加所有覆盖。
Category category = guild.getCategoryById(Categ);
TextChannel channel1 = guild.createTextChannel("ticket-" + EndlessBungeeTickets.instance.sql.getTicket(username)).setParent(category).addPermissionOverride(guild.getMember(user), EnumSet.of(Permission.VIEW_CHANNEL, Permission.MESSAGE_WRITE), null).addPermissionOverride(guild.getPublicRole(), null, EnumSet.of(Permission.VIEW_CHANNEL, Permission.MESSAGE_WRITE)).complete();
ChannelManager manager = channel1.getManager();
for (long roleId : RolesWR) {
manager.putPermissionOverride(guild.getRoleById(roleId), EnumSet.of(Permission.VIEW_CHANNEL, Permission.MESSAGE_WRITE), null);
}
manager.queue(); // this will add all overrides at once
更好的是直接将它们全部添加到 ChannelAction 上:
String ticketId = EndlessBungeeTickets.instance.sql.getTicket(username);
// Use variables to make the code more readable and remove duplicate allocations
EnumSet<Permission> permissions = EnumSet.of(Permission.VIEW_CHANNEL, Permission.MESSAGE_WRITE);
// Use the channel action properly by adding all overrides to it
ChannelAction<TextChannel> manager = category.createTextChannel("ticket-" + ticketId)
.addPermissionOverride(guild.getMember(user), permissions, null) // grant access to the user
.addPermissionOverride(guild.getPublicRole(), null, permissions); // deny access to @everyone
for (long roleId : RolesWR) { // Make sure IDs are stored as longs
// grant access to the roles
manager.addPermissionOverride(guild.getRoleById(roleId), permissions, null);
}
// Creates the channel with the overrides already applied, without blocking your thread too!
manager.queue();