Search code examples
javadiscorddiscord-jda

How to check if a user HAD a permission before they left the server?


Since

onGuildMemberRemove() 

is called when a members leaves, when trying to check if he had permission to view/speak in a specific channel, it returns that

textChannel.canTalk(event.getMember()) 

is empty / null

Please help me get around this.

Here is my

onGuildMemberRemove() 
for (int i = 0; i < channelID.length; i++) {
    TextChannel textChannel = event.getGuild().getTextChannelById(channelID[i]);

    if (textChannel.canTalk(event.getMember()) && textChannel.getParentCategoryId().equals("1030472114907652137")) {
        textChannel.delete().queue();
        Arrays.stream(channelID).toList().remove(i);
    }

}

Solution

  • It took me a while, but i found the asnwer!

    First declare an array of type PermissionOverride[] in the main method.

    Then in the onGuildMemberJoin method:

        overrides = new PermissionOverride[overrides.length +1];
            overrides[overrides.length - 1] = event.getGuild()
                    .getTextChannelById(textChannel.getId())
                    .getPermissionContainer()
                    .getPermissionOverrides()
                    .get(0);
    

    Now in the onGuildMemberRemove method i can simply check if what i am looking for exists in overrides[]

        public void onGuildMemberRemove(GuildMemberRemoveEvent event) {
    
            for(int k=0; k<event.getGuild().getCategoryById("1030472114907652137").getChannels().size(); k++) {
                for(int l = 0 ; l < overrides.length; l++){
                    if(overrides[l].getChannel().getId().equals(event.getGuild().getCategoryById("1030472114907652137").getChannels().get(k).getId())) {
                        if (!overrides[l].getChannel().getId().equals(null)) {
                            overrides[l].getChannel().asTextChannel().delete().queue();
    
                            overrides = removeOverride(overrides, overrides[l]);
    
                            System.out.println(overrides);
                        }
                    }
                }
            }
        }
    
    

    For explanation i am creating a verification channel every time someone joins the server and grants them the VIEW_CHANNEL permission, then in case they leave before completing the verification, i remove the channel that they had access too.

    In other words if you want to do it, you have to save the data in a PermissionOverride array, to access it when you need.

    @Minn Nothing is impossible.