Skip to content

Commit

Permalink
Owen's requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Reasonlesss committed Apr 16, 2024
1 parent 552433b commit d6df4b2
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
109 changes: 21 additions & 88 deletions src/main/java/com/diamondfire/helpbot/sys/tasks/impl/VIPStarTask.java
Original file line number Diff line number Diff line change
@@ -1,99 +1,55 @@
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<Integer, Role> roles = new HashMap<>();

assert guild != null;

List<Member> 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<Long> 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<Long> 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();
}
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();
}
Expand All @@ -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;
}


}
110 changes: 110 additions & 0 deletions src/main/java/com/diamondfire/helpbot/sys/vip/VIPRoleHandler.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Long> 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<Long> retrieveVIPs() {
Set<Long> 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;
}


}

0 comments on commit d6df4b2

Please sign in to comment.