【问题标题】:How to set roles permission of a channel? - Java JDA如何设置频道的角色权限? -Java JDA
【发布时间】:2021-05-28 07:11:49
【问题描述】:

如何在测试频道中设置角色列表的权限? 我试过了,但它只设置了最后一个角色:

            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();
            });

【问题讨论】:

    标签: java discord discord-jda


    【解决方案1】:

    您应该在调用队列之前添加所有覆盖。

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-02
      • 1970-01-01
      • 2021-08-16
      • 2016-01-30
      • 2020-12-05
      • 2021-12-31
      相关资源
      最近更新 更多