From 5c0031205126d71cf993e1401693a9cba04483c9 Mon Sep 17 00:00:00 2001 From: Reason <28310208+Reasonlesss@users.noreply.github.com> Date: Tue, 16 Apr 2024 14:02:45 +0100 Subject: [PATCH 1/4] Add VIP role logic --- .gitignore | 1 - .../java/com/diamondfire/helpbot/HelpBot.java | 4 - .../helpbot/sys/tasks/TaskRegistry.java | 3 +- .../helpbot/sys/tasks/impl/VIPStarTask.java | 128 ++++++++++++++++++ .../diamondfire/helpbot/util/StarUtil.java | 40 ++++++ src/main/resources/star.png | Bin 0 -> 652 bytes 6 files changed, 170 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/diamondfire/helpbot/sys/tasks/impl/VIPStarTask.java create mode 100644 src/main/java/com/diamondfire/helpbot/util/StarUtil.java create mode 100644 src/main/resources/star.png diff --git a/.gitignore b/.gitignore index 1d24e1fe..d20abb20 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,4 @@ build /src/main/resources/swear_filter.json *.json -*.png *.txt diff --git a/src/main/java/com/diamondfire/helpbot/HelpBot.java b/src/main/java/com/diamondfire/helpbot/HelpBot.java index bd820c9e..02b6daa8 100644 --- a/src/main/java/com/diamondfire/helpbot/HelpBot.java +++ b/src/main/java/com/diamondfire/helpbot/HelpBot.java @@ -1,13 +1,9 @@ package com.diamondfire.helpbot; - import com.diamondfire.helpbot.bot.HelpBotInstance; import com.diamondfire.helpbot.df.codeinfo.codedatabase.changelog.CodeDifferenceHandler; import com.diamondfire.helpbot.df.codeinfo.codedatabase.db.CodeDatabase; -import com.diamondfire.helpbot.sys.tag.TagHandler; - import javax.security.auth.login.LoginException; -import java.io.IOException; public class HelpBot { diff --git a/src/main/java/com/diamondfire/helpbot/sys/tasks/TaskRegistry.java b/src/main/java/com/diamondfire/helpbot/sys/tasks/TaskRegistry.java index ed290d18..c0488c1e 100644 --- a/src/main/java/com/diamondfire/helpbot/sys/tasks/TaskRegistry.java +++ b/src/main/java/com/diamondfire/helpbot/sys/tasks/TaskRegistry.java @@ -17,7 +17,8 @@ public void initialize() { new GraphChannelTask(), //new RefreshCreditsTask(), new SupporterClassTask(), - new NameUpdateTask() + new NameUpdateTask(), + new VIPStarTask() ); SupportUnexcuseTask.prepare(); diff --git a/src/main/java/com/diamondfire/helpbot/sys/tasks/impl/VIPStarTask.java b/src/main/java/com/diamondfire/helpbot/sys/tasks/impl/VIPStarTask.java new file mode 100644 index 00000000..33053856 --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/sys/tasks/impl/VIPStarTask.java @@ -0,0 +1,128 @@ +package com.diamondfire.helpbot.sys.tasks.impl; + +import com.diamondfire.helpbot.bot.HelpBotInstance; +import com.diamondfire.helpbot.sys.database.impl.DatabaseQuery; +import com.diamondfire.helpbot.sys.database.impl.queries.BasicQuery; +import com.diamondfire.helpbot.sys.tasks.LoopingTask; +import com.diamondfire.helpbot.util.StarUtil; +import net.dv8tion.jda.api.entities.*; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.io.*; +import java.sql.*; +import java.util.*; +import java.util.List; +import java.util.concurrent.TimeUnit; + +public class VIPStarTask implements LoopingTask { + + // todo: add this + private static final long VIP_PASS_HOLDER_ROLE = 0L; + + @Override + public long getInitialStart() { + return 0; + } + + @Override + public long getNextLoop() { + return TimeUnit.MINUTES.toMillis(30L); + } + + @Override + public void run() { + Guild guild = HelpBotInstance.getJda().getGuildById(HelpBotInstance.DF_GUILD); + + if (guild == null) { + return; + } + + Role generalRole = guild.getRoleById(VIP_PASS_HOLDER_ROLE); + + if (generalRole == null) { + return; + } + + // Load members and create the color -> role map. + final Map roles = new HashMap<>(); + List members = guild.loadMembers().get(); + + + // Load roles from the database + new DatabaseQuery() + .query(new BasicQuery("SELECT * FROM owen.vip_roles")) + .compile() + .run(result -> { + ResultSet set = result.getResult(); + while (set.next()) { + roles.put(set.getInt("color"), guild.getRoleById(set.getLong("role_id"))); + } + }); + + + Set vips = new HashSet<>(); + new DatabaseQuery() + .query(new BasicQuery("SELECT linked_accounts.discord_id FROM linked_accounts, hypercube.ranks " + + "WHERE linked_accounts.player_uuid = ranks.uuid AND ranks.vip = 1")) + .compile() + .run(result -> { + ResultSet set = result.getResult(); + while (set.next()) { + vips.add(set.getLong("discord_id")); + } + }); + + for (Member member : members) { + Role role = roles.get(member.getColorRaw()); + if (role == null) { + try { + // Create the role and add it to the roles map. + role = createRole(guild, member.getColor(), member.getColorRaw()); + roles.put(member.getColorRaw(), role); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + // If the vips returned by the db contains the member add the role. + if (vips.contains(member.getIdLong())) { + if (!member.getRoles().contains(role)) { + guild.addRoleToMember(member, role).queue(); + } + if (!member.getRoles().contains(generalRole)) { + guild.addRoleToMember(member, generalRole).queue(); + } + } else { + // If the user has the roles, remove them. + if (member.getRoles().contains(role)) { + guild.removeRoleFromMember(member, role).queue(); + } + if (member.getRoles().contains(generalRole)) { + guild.removeRoleFromMember(member, generalRole).queue(); + } + } + } + } + + /** + * Creates a colored star role and adds it to the database. + */ + private Role createRole(Guild guild, Color color, int colorRaw) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ImageIO.write(StarUtil.create(color), "png", baos); + baos.flush(); + Role role = guild.createRole() + .setName(" ") + .setIcon(Icon.from(baos.toByteArray())) + .complete(); + new DatabaseQuery() + .query(new BasicQuery("INSERT INTO owen.vip_roles (color, role_id) VALUES (?, ?)", statement -> { + statement.setInt(1, colorRaw); + statement.setLong(2, role.getIdLong()); + })) + .compile() + .run(ignored -> {}); + return role; + } + +} diff --git a/src/main/java/com/diamondfire/helpbot/util/StarUtil.java b/src/main/java/com/diamondfire/helpbot/util/StarUtil.java new file mode 100644 index 00000000..bfea79f5 --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/util/StarUtil.java @@ -0,0 +1,40 @@ +package com.diamondfire.helpbot.util; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.*; + +/** + * Creates a coloured star to be used as a role icon. + */ +public class StarUtil { + + private static final BufferedImage STAR; + private static final int SIZE = 32; + + static { + // Load the star from the resources. + InputStream inputStream = StarUtil.class.getResourceAsStream("/star.png"); + + try { + assert inputStream != null; + STAR = ImageIO.read(inputStream); + }catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static BufferedImage create(Color color) { + BufferedImage bufferedImage = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_ARGB); + Graphics2D g2d = bufferedImage.createGraphics(); + + g2d.drawImage(STAR, 0, 0, null); + g2d.setComposite(AlphaComposite.SrcAtop); + g2d.setColor(color); + g2d.fillRect(0, 0, 32, 32); + g2d.dispose(); + return bufferedImage; + } + +} diff --git a/src/main/resources/star.png b/src/main/resources/star.png new file mode 100644 index 0000000000000000000000000000000000000000..668f3af6416b30851c294b59550bdb9783e10340 GIT binary patch literal 652 zcmV;70(1R|P)EX>4Tx04R}tkv&MmKpe$i(@LdO6zm`(lA$_?1yK=4twIqhgj%6h2a`)bgeDD1 zi;JV+T5#}VvFhOBtgC~oAP9bdI6F8gx=4xtOA0MwJUH&hyL*qjcYshYG0h5$0h(Sh zQ;E2k$*zdOR|L?55C$~S<%#pCyAq~rc=I< zaarZO#aSy=S@WL!h2fmOyu@{y14v*2i;y5fK@}S)p@JB#Iw=-Xv>*5I_d9-xTnf21 zz{oL=GBn7JAN&t~cWdP*#@(cF9O!tl?T;ZKxC=CDw*7r<+l>>z{|sE|Eq}QVOn;JI zYiZFVpnDs*xNd369&ot>^gS7}DLay%rjXAA?`QN)S)lh82(7xkHTQA)0Hmp_y{D4^000SaNLh0L z04^f{04^f|c%?sf00007bV*G`2j~P43N-@K^5k3q000?uMObu0Z*6U5Zgc=ca%Ew3 zWn>_CX>@2HM@dakSAh-}0001ENklOAY`K3`AAj|H`$oArb$mcwN&@yy=8< zzn9EjR8`FyiA?qKw_gAO2ta_;_WJnXlp7$h Date: Tue, 16 Apr 2024 14:23:23 +0100 Subject: [PATCH 2/4] Formatting --- src/main/java/com/diamondfire/helpbot/HelpBot.java | 13 +++++++++++++ .../helpbot/sys/tasks/impl/VIPStarTask.java | 4 +++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/diamondfire/helpbot/HelpBot.java b/src/main/java/com/diamondfire/helpbot/HelpBot.java index 02b6daa8..e16235cc 100644 --- a/src/main/java/com/diamondfire/helpbot/HelpBot.java +++ b/src/main/java/com/diamondfire/helpbot/HelpBot.java @@ -3,11 +3,24 @@ import com.diamondfire.helpbot.bot.HelpBotInstance; import com.diamondfire.helpbot.df.codeinfo.codedatabase.changelog.CodeDifferenceHandler; import com.diamondfire.helpbot.df.codeinfo.codedatabase.db.CodeDatabase; +import com.diamondfire.helpbot.util.StarUtil; + +import javax.imageio.ImageIO; import javax.security.auth.login.LoginException; +import java.awt.*; +import java.io.*; public class HelpBot { public static void main(String[] args) throws LoginException { + try { + File file = new File("test/test.png"); + file.mkdirs(); + ImageIO.write(StarUtil.create(Color.CYAN), "png", new File("test/test.png")); + } catch (IOException e) { + throw new RuntimeException(e); + } + if (true) return; CodeDatabase.initialize(); HelpBotInstance.initialize(); CodeDifferenceHandler.refresh(); diff --git a/src/main/java/com/diamondfire/helpbot/sys/tasks/impl/VIPStarTask.java b/src/main/java/com/diamondfire/helpbot/sys/tasks/impl/VIPStarTask.java index 33053856..07dd2a6e 100644 --- a/src/main/java/com/diamondfire/helpbot/sys/tasks/impl/VIPStarTask.java +++ b/src/main/java/com/diamondfire/helpbot/sys/tasks/impl/VIPStarTask.java @@ -114,6 +114,7 @@ private Role createRole(Guild guild, Color color, int colorRaw) throws IOExcepti Role role = guild.createRole() .setName(" ") .setIcon(Icon.from(baos.toByteArray())) + .setPermissions(0L) .complete(); new DatabaseQuery() .query(new BasicQuery("INSERT INTO owen.vip_roles (color, role_id) VALUES (?, ?)", statement -> { @@ -121,7 +122,8 @@ private Role createRole(Guild guild, Color color, int colorRaw) throws IOExcepti statement.setLong(2, role.getIdLong()); })) .compile() - .run(ignored -> {}); + .run(ignored -> { + }); return role; } From ed568796828d848714647f62408190b6b53a36b9 Mon Sep 17 00:00:00 2001 From: Reason <28310208+Reasonlesss@users.noreply.github.com> Date: Tue, 16 Apr 2024 14:25:21 +0100 Subject: [PATCH 3/4] Remove testing code --- src/main/java/com/diamondfire/helpbot/HelpBot.java | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/main/java/com/diamondfire/helpbot/HelpBot.java b/src/main/java/com/diamondfire/helpbot/HelpBot.java index e16235cc..95b064b0 100644 --- a/src/main/java/com/diamondfire/helpbot/HelpBot.java +++ b/src/main/java/com/diamondfire/helpbot/HelpBot.java @@ -3,24 +3,12 @@ import com.diamondfire.helpbot.bot.HelpBotInstance; import com.diamondfire.helpbot.df.codeinfo.codedatabase.changelog.CodeDifferenceHandler; import com.diamondfire.helpbot.df.codeinfo.codedatabase.db.CodeDatabase; -import com.diamondfire.helpbot.util.StarUtil; -import javax.imageio.ImageIO; import javax.security.auth.login.LoginException; -import java.awt.*; -import java.io.*; public class HelpBot { public static void main(String[] args) throws LoginException { - try { - File file = new File("test/test.png"); - file.mkdirs(); - ImageIO.write(StarUtil.create(Color.CYAN), "png", new File("test/test.png")); - } catch (IOException e) { - throw new RuntimeException(e); - } - if (true) return; CodeDatabase.initialize(); HelpBotInstance.initialize(); CodeDifferenceHandler.refresh(); From be89c3c9eed967508aaa114a0cb2b89e0f64a369 Mon Sep 17 00:00:00 2001 From: Reason <28310208+Reasonlesss@users.noreply.github.com> Date: Tue, 16 Apr 2024 21:43:42 +0100 Subject: [PATCH 4/4] Owen's requested changes --- .../sys/externalfile/ExternalFiles.java | 6 + .../helpbot/sys/tasks/impl/VIPStarTask.java | 109 ++++------------- .../helpbot/sys/vip/VIPRoleHandler.java | 110 ++++++++++++++++++ 3 files changed, 137 insertions(+), 88 deletions(-) create mode 100644 src/main/java/com/diamondfire/helpbot/sys/vip/VIPRoleHandler.java diff --git a/src/main/java/com/diamondfire/helpbot/sys/externalfile/ExternalFiles.java b/src/main/java/com/diamondfire/helpbot/sys/externalfile/ExternalFiles.java index 6d85802b..2becc72c 100644 --- a/src/main/java/com/diamondfire/helpbot/sys/externalfile/ExternalFiles.java +++ b/src/main/java/com/diamondfire/helpbot/sys/externalfile/ExternalFiles.java @@ -61,6 +61,12 @@ public interface ExternalFiles { .setFileType("json") .buildFile(); + File VIP_ROLES = new ExternalFileBuilder() + .isDirectory(false) + .setName("vip_roles") + .setFileType("json") + .buildFile(); + File SAM_QUOTES = new ExternalFileBuilder() .isDirectory(false) .setName("samquotes") diff --git a/src/main/java/com/diamondfire/helpbot/sys/tasks/impl/VIPStarTask.java b/src/main/java/com/diamondfire/helpbot/sys/tasks/impl/VIPStarTask.java index 07dd2a6e..f254c16b 100644 --- a/src/main/java/com/diamondfire/helpbot/sys/tasks/impl/VIPStarTask.java +++ b/src/main/java/com/diamondfire/helpbot/sys/tasks/impl/VIPStarTask.java @@ -1,91 +1,48 @@ package com.diamondfire.helpbot.sys.tasks.impl; import com.diamondfire.helpbot.bot.HelpBotInstance; -import com.diamondfire.helpbot.sys.database.impl.DatabaseQuery; -import com.diamondfire.helpbot.sys.database.impl.queries.BasicQuery; import com.diamondfire.helpbot.sys.tasks.LoopingTask; -import com.diamondfire.helpbot.util.StarUtil; -import net.dv8tion.jda.api.entities.*; +import com.diamondfire.helpbot.sys.vip.VIPRoleHandler; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.Role; -import javax.imageio.ImageIO; -import java.awt.*; -import java.io.*; -import java.sql.*; -import java.util.*; import java.util.List; +import java.util.Set; import java.util.concurrent.TimeUnit; public class VIPStarTask implements LoopingTask { - - // todo: add this + private static final long VIP_PASS_HOLDER_ROLE = 0L; - + @Override public long getInitialStart() { return 0; } - + @Override public long getNextLoop() { return TimeUnit.MINUTES.toMillis(30L); } - + @Override public void run() { Guild guild = HelpBotInstance.getJda().getGuildById(HelpBotInstance.DF_GUILD); - - if (guild == null) { - return; - } - - Role generalRole = guild.getRoleById(VIP_PASS_HOLDER_ROLE); - - if (generalRole == null) { - return; - } - - // Load members and create the color -> role map. - final Map roles = new HashMap<>(); + + assert guild != null; + List members = guild.loadMembers().get(); - - - // Load roles from the database - new DatabaseQuery() - .query(new BasicQuery("SELECT * FROM owen.vip_roles")) - .compile() - .run(result -> { - ResultSet set = result.getResult(); - while (set.next()) { - roles.put(set.getInt("color"), guild.getRoleById(set.getLong("role_id"))); - } - }); - - - Set vips = new HashSet<>(); - new DatabaseQuery() - .query(new BasicQuery("SELECT linked_accounts.discord_id FROM linked_accounts, hypercube.ranks " + - "WHERE linked_accounts.player_uuid = ranks.uuid AND ranks.vip = 1")) - .compile() - .run(result -> { - ResultSet set = result.getResult(); - while (set.next()) { - vips.add(set.getLong("discord_id")); - } - }); - + Set vipIds = VIPRoleHandler.retrieveVIPs(); + Role generalRole = guild.getRoleById(VIP_PASS_HOLDER_ROLE); + + assert generalRole != null; + for (Member member : members) { - Role role = roles.get(member.getColorRaw()); + Role role = VIPRoleHandler.getOrCreateRole(member.getColorRaw()); if (role == null) { - try { - // Create the role and add it to the roles map. - role = createRole(guild, member.getColor(), member.getColorRaw()); - roles.put(member.getColorRaw(), role); - } catch (IOException e) { - throw new RuntimeException(e); - } + continue; } - // If the vips returned by the db contains the member add the role. - if (vips.contains(member.getIdLong())) { + if (vipIds.contains(member.getIdLong())) { if (!member.getRoles().contains(role)) { guild.addRoleToMember(member, role).queue(); } @@ -93,7 +50,6 @@ public void run() { guild.addRoleToMember(member, generalRole).queue(); } } else { - // If the user has the roles, remove them. if (member.getRoles().contains(role)) { guild.removeRoleFromMember(member, role).queue(); } @@ -103,28 +59,5 @@ public void run() { } } } - - /** - * Creates a colored star role and adds it to the database. - */ - private Role createRole(Guild guild, Color color, int colorRaw) throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ImageIO.write(StarUtil.create(color), "png", baos); - baos.flush(); - Role role = guild.createRole() - .setName(" ") - .setIcon(Icon.from(baos.toByteArray())) - .setPermissions(0L) - .complete(); - new DatabaseQuery() - .query(new BasicQuery("INSERT INTO owen.vip_roles (color, role_id) VALUES (?, ?)", statement -> { - statement.setInt(1, colorRaw); - statement.setLong(2, role.getIdLong()); - })) - .compile() - .run(ignored -> { - }); - return role; - } - + } diff --git a/src/main/java/com/diamondfire/helpbot/sys/vip/VIPRoleHandler.java b/src/main/java/com/diamondfire/helpbot/sys/vip/VIPRoleHandler.java new file mode 100644 index 00000000..ad0a15b1 --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/sys/vip/VIPRoleHandler.java @@ -0,0 +1,110 @@ +package com.diamondfire.helpbot.sys.vip; + +import com.diamondfire.helpbot.bot.HelpBotInstance; +import com.diamondfire.helpbot.sys.database.impl.DatabaseQuery; +import com.diamondfire.helpbot.sys.database.impl.queries.BasicQuery; +import com.diamondfire.helpbot.sys.externalfile.ExternalFiles; +import com.diamondfire.helpbot.util.StarUtil; +import com.google.gson.*; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Icon; +import net.dv8tion.jda.api.entities.Role; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.io.*; +import java.nio.file.Files; +import java.nio.file.StandardOpenOption; +import java.sql.ResultSet; +import java.util.*; + +public class VIPRoleHandler { + + private static final String ROLE_NAME = " "; + + private static final File FILE = ExternalFiles.VIP_ROLES; + private static final Map COLOR_ROLE_MAP = new HashMap<>(); + + static { + try { + cacheJson(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void cacheJson() throws IOException { + String content = new String(Files.readAllBytes(FILE.toPath())); + + if (content.isEmpty()) { + content = "{}"; + } + + JsonObject obj = JsonParser.parseString(content).getAsJsonObject(); + + for (String key : obj.keySet()) { + long roleId = obj.get(key).getAsLong(); + COLOR_ROLE_MAP.put(Integer.parseInt(key), roleId); + } + } + + public static void save() throws IOException { + JsonObject json = new JsonObject(); + + for (int color : COLOR_ROLE_MAP.keySet()) { + json.addProperty(String.valueOf(color), COLOR_ROLE_MAP.get(color)); + } + + FILE.delete(); + FILE.createNewFile(); + Files.write(FILE.toPath(), json.toString().getBytes(), StandardOpenOption.WRITE); + } + + public static Set retrieveVIPs() { + Set vips = new HashSet<>(); + new DatabaseQuery() + .query(new BasicQuery("SELECT linked_accounts.discord_id FROM linked_accounts, hypercube.ranks " + + "WHERE linked_accounts.player_uuid = ranks.uuid AND ranks.vip = 1")) + .compile() + .run(result -> { + ResultSet set = result.getResult(); + while (set.next()) { + vips.add(set.getLong("discord_id")); + } + }); + return vips; + } + + public static Role getOrCreateRole(int color) { + if (color == Role.DEFAULT_COLOR_RAW) { + return null; + } + Guild guild = HelpBotInstance.getJda().getGuildById(HelpBotInstance.DF_GUILD); + Role role; + + assert guild != null; + + if (COLOR_ROLE_MAP.containsKey(color)) { + role = guild.getRoleById(COLOR_ROLE_MAP.get(color)); + } else { + try { + // Create the coloured star and register it on discord. + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ImageIO.write(StarUtil.create(new Color(color)), "png", baos); + baos.flush(); + role = guild.createRole() + .setName(ROLE_NAME) + .setIcon(Icon.from(baos.toByteArray())) + .setPermissions(0L) + .complete(); + COLOR_ROLE_MAP.put(color, role.getIdLong()); + VIPRoleHandler.save(); + } catch (IOException e) { + return null; + } + } + return role; + } + + +}