ValidationUtil { public static void validateRequestGuildMembers(RequestGuildMembers request, Possible<IntentSet> possibleIntents) { if (request.query().isAbsent() == request.userIds().isAbsent()) { throw new IllegalArgumentException("One of query or user ids is required."); } if (possibleIntents.isAbsent()) { return; } IntentSet intents = possibleIntents.get(); boolean requestingPresences = request.presences().toOptional().orElse(false); if (requestingPresences && !intents.contains(Intent.GUILD_PRESENCES)) { throw new IllegalArgumentException("GUILD_PRESENCES intent is required to set presences = true."); } boolean requestingEntireList = request.query().toOptional().map(String::isEmpty).orElse(false) && request.limit() == 0; if (requestingEntireList && !intents.contains(Intent.GUILD_MEMBERS)) { throw new IllegalArgumentException("GUILD_MEMBERS intent is required to request the entire member list"); } }  static void validateRequestGuildMembers(RequestGuildMembers request, Possible<IntentSet> possibleIntents);  }
@Test public void shouldLetAQueryForAllMembersHappenIfIntentsAreAbsent() { ValidationUtil.validateRequestGuildMembers( RequestGuildMembers.builder().guildId("1234").query("").limit(0).build(), Possible.absent() ); }
@Test public void shouldMakeSureExactlyOneOfQueryOrUserIds() { ValidationUtil.validateRequestGuildMembers( RequestGuildMembers.builder().guildId("1234").query("prefix").limit(0).build(), Possible.absent() ); ValidationUtil.validateRequestGuildMembers( RequestGuildMembers.builder().guildId("1234").addUserId("9876").limit(1).build(), Possible.absent() ); assertThrows(IllegalArgumentException.class, () -> ValidationUtil.validateRequestGuildMembers( RequestGuildMembers.builder().guildId("1234").query("prefix").addUserId("5678").limit(0).build(), Possible.absent() )); assertThrows(IllegalArgumentException.class, () -> ValidationUtil.validateRequestGuildMembers( RequestGuildMembers.builder().guildId("1234").limit(0).build(), Possible.absent() )); }
@Test public void shouldRequireGuildPresencesIntentsIfRequestingEntireMemberListAndUsingIntents() { ValidationUtil.validateRequestGuildMembers( RequestGuildMembers.builder().guildId("1234").query("").limit(0).build(), Possible.absent() ); ValidationUtil.validateRequestGuildMembers( RequestGuildMembers.builder().guildId("1234").query("prefix").limit(0).build(), Possible.of(IntentSet.none()) ); ValidationUtil.validateRequestGuildMembers( RequestGuildMembers.builder().guildId("1234").query("").limit(100).build(), Possible.of(IntentSet.none()) ); assertThrows(IllegalArgumentException.class, () -> ValidationUtil.validateRequestGuildMembers( RequestGuildMembers.builder().guildId("1234").query("").limit(0).build(), Possible.of(IntentSet.none()) )); }