|
3 | 3 | import com.diamondfire.helpbot.bot.HelpBotInstance; |
4 | 4 | import com.diamondfire.helpbot.sys.tasks.LoopingTask; |
5 | 5 | import com.diamondfire.helpbot.sys.vip.VIPRoleHandler; |
6 | | -import net.dv8tion.jda.api.entities.Guild; |
7 | | -import net.dv8tion.jda.api.entities.Member; |
8 | | -import net.dv8tion.jda.api.entities.Role; |
| 6 | +import net.dv8tion.jda.api.entities.*; |
9 | 7 |
|
10 | | -import java.util.List; |
11 | | -import java.util.Set; |
| 8 | +import java.util.*; |
12 | 9 | import java.util.concurrent.TimeUnit; |
13 | 10 |
|
| 11 | +/** |
| 12 | + * Gives every VIP the star role for their name color, takes stars from everyone else, |
| 13 | + * and deletes star roles that no VIP needs. |
| 14 | + */ |
14 | 15 | public class VIPStarTask implements LoopingTask { |
15 | | - |
| 16 | + |
16 | 17 | @Override |
17 | 18 | public long getInitialStart() { |
18 | 19 | return 0; |
19 | 20 | } |
20 | | - |
| 21 | + |
21 | 22 | @Override |
22 | 23 | public long getNextLoop() { |
23 | 24 | return TimeUnit.MINUTES.toMillis(30L); |
24 | 25 | } |
25 | | - |
| 26 | + |
| 27 | + private record Seen(long id, boolean vip, int color, Set<Long> stars) { |
| 28 | + |
| 29 | + } |
| 30 | + |
26 | 31 | @Override |
27 | 32 | public void run() { |
28 | 33 | Guild guild = HelpBotInstance.getJda().getGuildById(HelpBotInstance.DF_GUILD); |
| 34 | + if (guild == null) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
29 | 38 | Set<Long> vipIds = VIPRoleHandler.retrieveVIPs(); |
| 39 | + if (vipIds.isEmpty()) { |
| 40 | + // Most likely a failed lookup; acting on it would strip every star. |
| 41 | + System.out.println("No VIPs found, skipping VIP star update."); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + VIPRoleHandler.syncWithGuild(guild); |
| 46 | + |
| 47 | + // Only VIPs and members holding a star matter, so only those are kept while members load. |
| 48 | + List<Seen> relevant = Collections.synchronizedList(new ArrayList<>()); |
| 49 | + try { |
| 50 | + guild.loadMembers(member -> { |
| 51 | + Set<Long> stars = new HashSet<>(); |
| 52 | + for (Role role : member.getRoles()) { |
| 53 | + if (VIPRoleHandler.isStarRole(role)) { |
| 54 | + stars.add(role.getIdLong()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + boolean vip = vipIds.contains(member.getIdLong()); |
| 59 | + if (vip || !stars.isEmpty()) { |
| 60 | + relevant.add(new Seen(member.getIdLong(), vip, VIPRoleHandler.getNameColor(member), stars)); |
| 61 | + } |
| 62 | + }).get(); |
| 63 | + } catch (Exception e) { |
| 64 | + // Without every member, a star in use can't be told apart from an unused one. |
| 65 | + System.out.println("Could not load members, skipping VIP star update: " + e); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + // The colors VIPs need, and the existing roles that serve them. |
| 70 | + Set<Integer> neededColors = new HashSet<>(); |
| 71 | + for (Seen member : relevant) { |
| 72 | + if (member.vip() && member.color() != Role.DEFAULT_COLOR_RAW) { |
| 73 | + neededColors.add(member.color()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + Set<Long> keep = new HashSet<>(); |
| 78 | + for (int color : neededColors) { |
| 79 | + Role role = VIPRoleHandler.getRole(guild, color); |
| 80 | + if (role != null) { |
| 81 | + keep.add(role.getIdLong()); |
| 82 | + } |
| 83 | + } |
30 | 84 |
|
31 | | - guild.loadMembers((member) -> { |
32 | | - Role role = VIPRoleHandler.getRole(member.getColorRaw()); |
| 85 | + // Delete unused star roles first: that takes the star from anyone still holding one, and frees room for new roles. |
| 86 | + int deleted = 0; |
| 87 | + for (Role role : guild.getRoles()) { |
| 88 | + if (VIPRoleHandler.isStarRole(role) && !keep.contains(role.getIdLong())) { |
| 89 | + VIPRoleHandler.deleteRole(role); |
| 90 | + deleted++; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + int created = 0; |
| 95 | + Map<Integer, Role> roles = new HashMap<>(); |
| 96 | + for (int color : neededColors) { |
| 97 | + Role role = VIPRoleHandler.getRole(guild, color); |
33 | 98 | if (role == null) { |
34 | | - return; |
| 99 | + role = VIPRoleHandler.createRole(guild, color); |
| 100 | + if (role != null) { |
| 101 | + created++; |
| 102 | + } |
| 103 | + } |
| 104 | + if (role != null) { |
| 105 | + roles.put(color, role); |
35 | 106 | } |
36 | | - if (vipIds.contains(member.getIdLong()) && !member.getRoles().contains(role)) { |
37 | | - guild.addRoleToMember(member, role) |
| 107 | + } |
| 108 | + |
| 109 | + for (Seen member : relevant) { |
| 110 | + UserSnowflake user = UserSnowflake.fromId(member.id()); |
| 111 | + Role wanted = member.vip() ? roles.get(member.color()) : null; |
| 112 | + |
| 113 | + if (wanted != null && !member.stars().contains(wanted.getIdLong())) { |
| 114 | + guild.addRoleToMember(user, wanted) |
38 | 115 | .reason("User has VIP Pass") |
39 | 116 | .queue(); |
40 | | - return; |
41 | 117 | } |
42 | | - if (!vipIds.contains(member.getIdLong()) && member.getRoles().contains(role)) { |
43 | | - guild.removeRoleFromMember(member, role) |
44 | | - .reason("User's VIP Pass has expired") |
45 | | - .queue(); |
| 118 | + |
| 119 | + for (long roleId : member.stars()) { |
| 120 | + if (!keep.contains(roleId) || (wanted != null && roleId == wanted.getIdLong())) { |
| 121 | + // Deleted above, or the one they should have. |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + Role role = guild.getRoleById(roleId); |
| 126 | + if (role != null) { |
| 127 | + guild.removeRoleFromMember(user, role) |
| 128 | + .reason(member.vip() ? "User's name color changed" : "User's VIP Pass has expired") |
| 129 | + .queue(); |
| 130 | + } |
46 | 131 | } |
47 | | - }); |
| 132 | + } |
48 | 133 |
|
| 134 | + if (deleted > 0 || created > 0) { |
| 135 | + System.out.printf("VIP stars: %s VIPs, %s colors, %s roles deleted, %s created%n", vipIds.size(), neededColors.size(), deleted, created); |
| 136 | + } |
49 | 137 | } |
50 | | - |
51 | 138 | } |
0 commit comments