diff --git a/src/main/java/main/config/BotStart.java b/src/main/java/main/config/BotStart.java index e6eb8b6..2eb2994 100644 --- a/src/main/java/main/config/BotStart.java +++ b/src/main/java/main/config/BotStart.java @@ -96,6 +96,7 @@ private void startBot() { jdaBuilder.disableCache(cacheFlags); jdaBuilder.enableIntents(intents); + jdaBuilder.setChunkingFilter(ChunkingFilter.ALL); jdaBuilder.setAutoReconnect(true); jdaBuilder.setStatus(OnlineStatus.ONLINE); jdaBuilder.setActivity(Activity.playing("Starting...")); @@ -143,7 +144,7 @@ private void savingParticipants() { @Scheduled(fixedDelay = (60 * 2), initialDelay = 30, timeUnit = TimeUnit.SECONDS) public void updateUserList() { -// participantsUpdaterService.update(jda); + participantsUpdaterService.update(jda); } public static Map getMapLanguages() { diff --git a/src/main/java/main/core/events/ReactionEvent.java b/src/main/java/main/core/events/ReactionEvent.java index 6318af1..0ac13ec 100644 --- a/src/main/java/main/core/events/ReactionEvent.java +++ b/src/main/java/main/core/events/ReactionEvent.java @@ -15,6 +15,7 @@ import org.springframework.stereotype.Service; import java.awt.*; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -55,25 +56,30 @@ public void reaction(@NotNull MessageReactionAddEvent event) { Long roleId = giveaway.getRoleId(); // null -> 0 Long forbiddenRole = giveaway.getForbiddenRole(); - if (roleId != null) { - Role roleById = event.getGuild().getRoleById(roleId); - boolean isForSpecificRole = giveaway.isForSpecificRole(); + if (forbiddenRole != null) { + Role guildRole = event.getGuild().getRoleById(forbiddenRole); + if (guildRole == null) return; + List memberRolesLost = member.getRoles().stream().map(Role::getIdLong).toList(); - if (isForSpecificRole && !member.getRoles().contains(roleById)) { + if (memberRolesLost.contains(guildRole.getIdLong())) { userDontHaveRestrictions(event, guildId, user); - } else { - giveaway.addUser(user); + return; } - } else if (forbiddenRole != null) { - Role roleById = event.getGuild().getRoleById(forbiddenRole); - if (member.getRoles().contains(roleById)) { + } + + if (roleId != null) { + Role role = event.getGuild().getRoleById(roleId); + if (role == null) return; + boolean isForSpecificRole = giveaway.isForSpecificRole(); + List userRolesList = member.getRoles().stream().map(Role::getIdLong).toList(); + + if (isForSpecificRole && !userRolesList.contains(role.getIdLong())) { userDontHaveRestrictions(event, guildId, user); - } else { - giveaway.addUser(user); + return; } - } else { - giveaway.addUser(user); } + + giveaway.addUser(user); } } } diff --git a/src/main/java/main/service/ParticipantsUpdaterService.java b/src/main/java/main/service/ParticipantsUpdaterService.java index b7c29db..e3d0fd1 100644 --- a/src/main/java/main/service/ParticipantsUpdaterService.java +++ b/src/main/java/main/service/ParticipantsUpdaterService.java @@ -16,7 +16,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; @Service public class ParticipantsUpdaterService { @@ -30,92 +29,98 @@ public ParticipantsUpdaterService(ActiveGiveawayRepository activeGiveawayReposit this.activeGiveawayRepository = activeGiveawayRepository; } -// public void update(JDA jda) { -// GiveawayRegistry instance = GiveawayRegistry.getInstance(); -// List giveawayList = new ArrayList<>(instance.getGiveaways()); -// for (Giveaway giveaway : giveawayList) { -// long guildIdLong = giveaway.getGuildId(); -// boolean isForSpecificRole = giveaway.isForSpecificRole(); -// long messageId = giveaway.getMessageId(); -// Long forbiddenRole = giveaway.getForbiddenRole(); -// -// if (jda != null) { -// if (instance.hasGiveaway(guildIdLong)) { -// long channelId = giveaway.getTextChannelId(); -// Guild guild = jda.getGuildById(guildIdLong); -// -// try { -// if (guild != null) { -// TextChannel textChannelById = guild.getTextChannelById(channelId); -// if (textChannelById != null) { -// List reactions = textChannelById -// .retrieveMessageById(messageId) -// .complete() -// .getReactions() -// .stream() -// .filter(messageReaction -> messageReaction.getEmoji().getName().equals(ReactionEvent.TADA)) -// .toList(); -// -// if (!reactions.isEmpty()) { -// //-1 because one Bot -// if (reactions.get(0).getCount() - 1 != giveaway.getListUsersSize()) { -// Map userList = new HashMap<>(); -// for (MessageReaction reaction : reactions) { -// userList = reaction -// .retrieveUsers() -// .complete() -// .stream() -// .filter(user -> !user.isBot()) -// .filter(user -> !giveaway.isUserContains(user.getId())) -// .collect(Collectors.toMap(User::getId, user -> user)); -// } -// -// if (isForSpecificRole) { -// Role roleGiveaway = jda.getRoleById(giveaway.getRoleId()); -// List members = guild.loadMembers().get(); -// for (Member member : members) { -// boolean contains = member.getRoles().contains(roleGiveaway); -// String memberId = member.getId(); -// if (!contains) { -// userList.remove(memberId); -// } -// } -// } -// -// if (forbiddenRole != null) { -// Role roleGiveaway = jda.getRoleById(giveaway.getForbiddenRole()); -// List members = guild.loadMembers().get(); -// for (Member member : members) { -// boolean contains = member.getRoles().contains(roleGiveaway); -// String memberId = member.getId(); -// if (contains) { -// userList.remove(memberId); -// } -// } -// } -// -// userList.values().forEach(user -> { -// if (instance.hasGiveaway(guildIdLong)) { -// giveaway.addUser(user); -// } -// }); -// } -// } -// } -// } -// } catch (Exception e) { -// boolean unknownMessage = e.getMessage().contains("10008: Unknown Message"); -// boolean missingPermission = e.getMessage().contains("Missing permission: VIEW_CHANNEL"); -// if (unknownMessage || missingPermission) { -// LOGGER.info(e.getMessage()); -// activeGiveawayRepository.deleteById(guildIdLong); -// GiveawayRegistry.getInstance().removeGiveaway(guildIdLong); -// } else { -// LOGGER.error(e.getMessage(), e); -// } -// } -// } -// } -// } -// } + public void update(JDA jda) { + GiveawayRegistry instance = GiveawayRegistry.getInstance(); + List giveawayList = new ArrayList<>(instance.getGiveaways()); + for (Giveaway giveaway : giveawayList) { + long guildIdLong = giveaway.getGuildId(); + boolean isForSpecificRole = giveaway.isForSpecificRole(); + long messageId = giveaway.getMessageId(); + Long forbiddenRole = giveaway.getForbiddenRole(); + + if (jda != null) { + if (instance.hasGiveaway(guildIdLong)) { + long channelId = giveaway.getTextChannelId(); + Guild guild = jda.getGuildById(guildIdLong); + + try { + if (guild != null) { + TextChannel textChannelById = guild.getTextChannelById(channelId); + if (textChannelById != null) { + List reactions = textChannelById + .retrieveMessageById(messageId) + .complete() + .getReactions() + .stream() + .filter(messageReaction -> messageReaction.getEmoji().getName().equals(ReactionEvent.TADA)) + .toList(); + + if (!reactions.isEmpty()) { + //-1 because one Bot + if (reactions.get(0).getCount() - 1 != giveaway.getListUsersSize()) { + final Map userList = new HashMap<>(); + + MessageReaction messageReaction = reactions.get(0); + messageReaction.retrieveUsers().complete().forEach(user -> { + if (!user.isBot()) userList.put(user.getId(), user); + }); + + if (isForSpecificRole) { + Role roleGiveaway = guild.getRoleById(giveaway.getRoleId()); + if (roleGiveaway == null) continue; + + List members = guild.loadMembers().get(); + + for (Member member : members) { + List memberRolesList = member.getRoles().stream().map(Role::getIdLong).toList(); + + boolean contains = memberRolesList.contains(roleGiveaway.getIdLong()); + if (!contains) { + String memberId = member.getId(); + userList.remove(memberId); + } + } + } + + if (forbiddenRole != null) { + Role forbidden = guild.getRoleById(giveaway.getForbiddenRole()); + if (forbidden == null) continue; + + List members = guild.loadMembers().get(); + + for (Member member : members) { + List memberRolesList = member.getRoles().stream().map(Role::getIdLong).toList(); + + boolean contains = memberRolesList.contains(forbidden.getIdLong()); + if (contains) { + String memberId = member.getId(); + userList.remove(memberId); + } + } + } + + userList.values().forEach(user -> { + if (instance.hasGiveaway(guildIdLong)) { + giveaway.addUser(user); + } + }); + } + } + } + } + } catch (Exception e) { + boolean unknownMessage = e.getMessage().contains("10008: Unknown Message"); + boolean missingPermission = e.getMessage().contains("Missing permission: VIEW_CHANNEL"); + if (unknownMessage || missingPermission) { + LOGGER.info(e.getMessage()); + activeGiveawayRepository.deleteById(guildIdLong); + GiveawayRegistry.getInstance().removeGiveaway(guildIdLong); + } else { + LOGGER.error(e.getMessage(), e); + } + } + } + } + } + } } \ No newline at end of file