Skip to content

Commit

Permalink
New Update
Browse files Browse the repository at this point in the history
1. Fix
  • Loading branch information
megoRU committed Jan 31, 2024
1 parent c467d40 commit 75c4920
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 103 deletions.
3 changes: 2 additions & 1 deletion src/main/java/main/config/BotStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -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..."));
Expand Down Expand Up @@ -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<Long, Settings> getMapLanguages() {
Expand Down
32 changes: 19 additions & 13 deletions src/main/java/main/core/events/ReactionEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Long> 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<Long> 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);
}
}
}
Expand Down
183 changes: 94 additions & 89 deletions src/main/java/main/service/ParticipantsUpdaterService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -30,92 +29,98 @@ public ParticipantsUpdaterService(ActiveGiveawayRepository activeGiveawayReposit
this.activeGiveawayRepository = activeGiveawayRepository;
}

// public void update(JDA jda) {
// GiveawayRegistry instance = GiveawayRegistry.getInstance();
// List<Giveaway> 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<MessageReaction> 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<String, User> 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<Member> 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<Member> 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<Giveaway> 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<MessageReaction> 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<String, User> 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<Member> members = guild.loadMembers().get();

for (Member member : members) {
List<Long> 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<Member> members = guild.loadMembers().get();

for (Member member : members) {
List<Long> 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);
}
}
}
}
}
}
}

0 comments on commit 75c4920

Please sign in to comment.