Corey Brasseaux
Corey Brasseaux

Reputation: 1

How do I give a newly created role permissions to join a newly created voice chat with the JDA library?

I am attempting to make roles that users can assign themselves and then create hidden voice chats that only that role can see. So far I have created a role and created a hidden voice chat with the code below. But now I don't know how to add permissions to the newly created role to join the newly created voice chat.

event.getGuild().getController().createCopyOfRole(event.getGuild().getRoleById("582333645948452874")).setMentionable(true).setName(messageSent[1]).queue();

event.getGuild().getController().createCopyOfChannel(event.getGuild().getVoiceChannelById("583088218145292298")).setName(messageSent[1]).queue();

Upvotes: 0

Views: 306

Answers (1)

Minn
Minn

Reputation: 6131

You can use addPermissionOverride on the ChannelAction that is returned by createCopyOfChannel.

EnumSet<Permission> permissions = EnumSet.of(Permission.VIEW_CHANNEL);
Role role = event.getGuild().getRoleById(582333645948452874L);
VoiceChannel channel = event.getGuild().getVoiceChannelById(583088218145292298L);
GuildController controller = event.getGuild().getController();
controller.createCopyOfRole(role)
          .setMentionable(true)
          .setName(messageSent[1])
          .queue((r) -> {
              controller.createCopyOfChannel(channel)
                        .setName(messageSent[1])
                        // allow the new role to view the channel
                        .addPermissionOverride(r, permissions, null)
                        // Don't allow the everyone role to view the channel
                        .addPermissionOverride(guild.getPublicRole(), null, permissions)
                        .queue();
          });

The new role that was created is available in the callback of queue. In my case I named it r. You can then use that role r to add a permission override to the channel before creating it. To deny others access you simply use the public role (@everyone) and deny view access from it.

Upvotes: 1

Related Questions