Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VIP Role Star #100

Merged
merged 13 commits into from
Jun 14, 2024
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ build
/src/main/resources/swear_filter.json

*.json
*.png
*.txt
3 changes: 0 additions & 3 deletions src/main/java/com/diamondfire/helpbot/HelpBot.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public void initialize() {
new GraphChannelTask(),
//new RefreshCreditsTask(),
new SupporterClassTask(),
new NameUpdateTask()
new NameUpdateTask(),
new VIPStarTask()
);

SupportUnexcuseTask.prepare();
Expand Down
130 changes: 130 additions & 0 deletions src/main/java/com/diamondfire/helpbot/sys/tasks/impl/VIPStarTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
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);
Owen1212055 marked this conversation as resolved.
Show resolved Hide resolved

if (generalRole == null) {
return;
}

// Load members and create the color -> role map.
final Map<Integer, Role> roles = new HashMap<>();
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")));
}
});
Reasonlesss marked this conversation as resolved.
Show resolved Hide resolved


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()) {
Reasonlesss marked this conversation as resolved.
Show resolved Hide resolved
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()))
.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;
}

}
40 changes: 40 additions & 0 deletions src/main/java/com/diamondfire/helpbot/util/StarUtil.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
Binary file added src/main/resources/star.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading