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..95b064b0 100644 --- a/src/main/java/com/diamondfire/helpbot/HelpBot.java +++ b/src/main/java/com/diamondfire/helpbot/HelpBot.java @@ -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 { diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/disable/DisableCommandHandler.java b/src/main/java/com/diamondfire/helpbot/bot/command/disable/DisableCommandHandler.java index 14c925e1..a72af1b5 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/disable/DisableCommandHandler.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/disable/DisableCommandHandler.java @@ -48,6 +48,9 @@ public void enable(Command command) { } public void disable(Command command) { + if (command == null) { + return; + } // Prevents tricky people using eval. if (command instanceof CommandDisableFlag) { return; diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/codeblock/AbstractSingleQueryCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/codeblock/AbstractSingleQueryCommand.java index f512dee2..f2dccdf8 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/codeblock/AbstractSingleQueryCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/codeblock/AbstractSingleQueryCommand.java @@ -14,6 +14,7 @@ import com.diamondfire.helpbot.util.*; import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel; import net.dv8tion.jda.api.entities.emoji.Emoji; import net.dv8tion.jda.api.interactions.components.buttons.Button; @@ -24,7 +25,7 @@ public abstract class AbstractSingleQueryCommand extends Command { - public static void sendMultipleMessage(List actions, TextChannel channel, long userToWait, BiConsumer onChosen) { + public static void sendMultipleMessage(List actions, GuildMessageChannel channel, long userToWait, BiConsumer onChosen) { // This here is to determine if all the duplicate types are the same. If not, we need to make sure that we filter those out first.. CodeObject referenceData = actions.get(0); Class classReference = referenceData.getClass(); @@ -67,7 +68,7 @@ public static void sendMultipleMessage(List actions, TextChannel cha // when msg is deleted causes nullpointer when tries to remove reactions! FIX CodeObject object = buttonMap.get(event.getComponentId()); - onChosen.accept(object, message.getChannel().asTextChannel()); + onChosen.accept(object, message.getChannel().asGuildMessageChannel()); }); }); @@ -85,9 +86,9 @@ public void run(CommandEvent event) { getData(event, onDataReceived()); } - public abstract BiConsumer onDataReceived(); + public abstract BiConsumer onDataReceived(); - protected void getData(CommandEvent event, BiConsumer onChosen) { + protected void getData(CommandEvent event, BiConsumer onChosen) { String name = event.getArgument("name"); PresetBuilder preset = new PresetBuilder(); @@ -120,9 +121,9 @@ protected void getData(CommandEvent event, BiConsumer o // If none, proceed. Else we need to special case that. if (sameActions.size() == 1) { - onChosen.accept(sameActions.get(0), event.getChannel().asTextChannel()); + onChosen.accept(sameActions.get(0), event.getChannel().asGuildMessageChannel()); } else if (sameActions.size() > 1) { - sendMultipleMessage(sameActions, event.getChannel().asTextChannel(), event.getMember().getIdLong(), onChosen); + sendMultipleMessage(sameActions, event.getChannel().asGuildMessageChannel(), event.getMember().getIdLong(), onChosen); } return; diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/codeblock/CodeCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/codeblock/CodeCommand.java index 759baba5..cde99c68 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/codeblock/CodeCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/codeblock/CodeCommand.java @@ -7,6 +7,7 @@ import com.diamondfire.helpbot.util.Util; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel; import net.dv8tion.jda.api.utils.FileUpload; import java.io.File; @@ -15,7 +16,7 @@ public class CodeCommand extends AbstractSingleQueryCommand { - public static void sendHelpMessage(T data, TextChannel channel) { + public static void sendHelpMessage(T data, GuildMessageChannel channel) { EmbedBuilder builder = data.getEnum().getEmbedBuilder().generateEmbed(data); String customHead = data.getItem().getHead(); @@ -57,7 +58,7 @@ public void run(CommandEvent event) { } @Override - public BiConsumer onDataReceived() { + public BiConsumer onDataReceived() { return CodeCommand::sendHelpMessage; } } diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/codeblock/RawCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/codeblock/RawCommand.java index 75d5c4be..2be76c85 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/codeblock/RawCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/codeblock/RawCommand.java @@ -7,13 +7,14 @@ import com.diamondfire.helpbot.util.StringUtil; import com.google.gson.*; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel; import java.util.function.BiConsumer; public class RawCommand extends AbstractSingleQueryCommand { - private static void sendRawMessage(CodeObject data, TextChannel channel) { + private static void sendRawMessage(CodeObject data, GuildMessageChannel channel) { Gson gson = new GsonBuilder().setPrettyPrinting().create(); String json = gson.toJson(data.getJson()); @@ -55,7 +56,7 @@ public void run(CommandEvent event) { } @Override - public BiConsumer onDataReceived() { + public BiConsumer onDataReceived() { return RawCommand::sendRawMessage; } diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/codeblock/TagsCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/codeblock/TagsCommand.java index 92f84dee..d7a84a72 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/codeblock/TagsCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/codeblock/TagsCommand.java @@ -7,6 +7,7 @@ import com.diamondfire.helpbot.util.Util; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel; import net.dv8tion.jda.api.utils.FileUpload; import java.io.File; @@ -15,7 +16,7 @@ public class TagsCommand extends AbstractSingleQueryCommand { - private static void sendTagMessage(CodeObject data, TextChannel channel) { + private static void sendTagMessage(CodeObject data, GuildMessageChannel channel) { EmbedBuilder builder = new EmbedBuilder(); ActionData actionData; @@ -92,7 +93,7 @@ public void run(CommandEvent event) { } @Override - public BiConsumer onDataReceived() { + public BiConsumer onDataReceived() { return TagsCommand::sendTagMessage; } } diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/dev/FetchDataCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/dev/FetchDataCommand.java index d868e11e..779c3bb6 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/dev/FetchDataCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/dev/FetchDataCommand.java @@ -16,6 +16,7 @@ import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel; import net.dv8tion.jda.api.utils.FileUpload; import java.io.*; @@ -61,7 +62,7 @@ public Permission getPermission() { public void run(CommandEvent event) { List flags = event.getArgument("flag"); if (flags == null) { - setup(event.getChannel().asTextChannel()); + setup(event.getChannel().asGuildMessageChannel()); } else { boolean includeColors = false; boolean updateDb = true; @@ -72,15 +73,15 @@ public void run(CommandEvent event) { updateDb = false; } } - setup(event.getChannel().asTextChannel(), includeColors, updateDb); + setup(event.getChannel().asGuildMessageChannel(), includeColors, updateDb); } } - public void setup(TextChannel channel) { + public void setup(GuildMessageChannel channel) { setup(channel, false, true); } - public void setup(TextChannel channel, boolean includeColors, boolean updateDb) { + public void setup(GuildMessageChannel channel, boolean includeColors, boolean updateDb) { EmbedBuilder builder = new EmbedBuilder(); builder.setTitle("Fetching Code Database..."); diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/fun/NbsCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/fun/NbsCommand.java index 9688bc54..9ba93905 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/fun/NbsCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/fun/NbsCommand.java @@ -11,6 +11,7 @@ import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel; import net.dv8tion.jda.api.utils.FileUpload; import java.awt.*; @@ -44,7 +45,7 @@ public Permission getPermission() { @Override public void run(CommandEvent event) { - TextChannel channel = event.getChannel().asTextChannel(); + GuildMessageChannel channel = event.getChannel().asGuildMessageChannel(); PresetBuilder attachNbsMsg = new PresetBuilder().withPreset(new InformativeReply(InformativeReplyType.ERROR,"You need to attach an nbs file!")); PresetBuilder errorMsg = new PresetBuilder().withPreset(new InformativeReply(InformativeReplyType.ERROR,"Something went wrong while generating!")); diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java index f439efd8..cbdc99db 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java @@ -12,6 +12,7 @@ import com.diamondfire.helpbot.sys.externalfile.ExternalFileUtil; import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel; import net.dv8tion.jda.api.utils.FileUpload; import java.io.File; @@ -57,7 +58,7 @@ public void run(CommandEvent event) { ); event.reply(builder); } else { - TextChannel channel = event.getChannel().asTextChannel(); + GuildMessageChannel channel = event.getChannel().asGuildMessageChannel(); channel.getHistory().retrievePast(messagesToRemove).queue((messages) -> { // Adds the messages to the messageBuilder object StringBuilder stringBuilder = new StringBuilder(); diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/reply/ReplyHandler.java b/src/main/java/com/diamondfire/helpbot/bot/command/reply/ReplyHandler.java index 5f7e2184..426fc6d1 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/reply/ReplyHandler.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/reply/ReplyHandler.java @@ -3,6 +3,7 @@ import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import net.dv8tion.jda.api.requests.RestAction; import net.dv8tion.jda.api.requests.restaction.MessageCreateAction; @@ -10,13 +11,13 @@ public class ReplyHandler { - private final TextChannel channel; + private final GuildMessageChannel channel; - public ReplyHandler(TextChannel channel) { + public ReplyHandler(GuildMessageChannel channel) { this.channel = channel; } - public TextChannel getChannel() { + public GuildMessageChannel getChannel() { return channel; } diff --git a/src/main/java/com/diamondfire/helpbot/bot/events/CommandEvent.java b/src/main/java/com/diamondfire/helpbot/bot/events/CommandEvent.java index 6a980cef..c5220bd9 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/events/CommandEvent.java +++ b/src/main/java/com/diamondfire/helpbot/bot/events/CommandEvent.java @@ -15,7 +15,7 @@ public class CommandEvent extends MessageReceivedEvent { private Command command; - private final ReplyHandler replyHandler = new ReplyHandler(getChannel().asTextChannel()); + private final ReplyHandler replyHandler = new ReplyHandler(getChannel().asGuildMessageChannel()); //TODO Cleanup and refactor this. // I'd like to see stuff like replying be put into it's whole own section and refactored as well. private ParsedArgumentSet parsedArgumentSet = null; diff --git a/src/main/java/com/diamondfire/helpbot/df/ranks/Rank.java b/src/main/java/com/diamondfire/helpbot/df/ranks/Rank.java index fa6b48a3..771f5121 100644 --- a/src/main/java/com/diamondfire/helpbot/df/ranks/Rank.java +++ b/src/main/java/com/diamondfire/helpbot/df/ranks/Rank.java @@ -37,7 +37,7 @@ public enum Rank { this.rankName = rankName; this.number = number; this.category = category; - this.emote = HelpBotInstance.getJda().getGuildById(615846886414483465L).getEmojiById(emote); + this.emote = HelpBotInstance.getJda().getGuildById(615846886414483465L).getEmojisByName(emote, false).get(0); } public String getRankName() { 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/message/acceptors/TagAcceptor.java b/src/main/java/com/diamondfire/helpbot/sys/message/acceptors/TagAcceptor.java index 05cdc074..0f8a4700 100644 --- a/src/main/java/com/diamondfire/helpbot/sys/message/acceptors/TagAcceptor.java +++ b/src/main/java/com/diamondfire/helpbot/sys/message/acceptors/TagAcceptor.java @@ -21,7 +21,7 @@ public boolean accept(Message message) { try { // Get Tag and send response TagHandler.getTag(parsedText) - .sendResponse(message.getChannel().asTextChannel(), message.getAuthor()); + .sendResponse(message.getChannel().asGuildMessageChannel(), message.getAuthor()); } catch (TagDoesNotExistException | IOException ignored) { return false; diff --git a/src/main/java/com/diamondfire/helpbot/sys/rolereact/RoleReactListener.java b/src/main/java/com/diamondfire/helpbot/sys/rolereact/RoleReactListener.java index a80494ad..af5bd748 100644 --- a/src/main/java/com/diamondfire/helpbot/sys/rolereact/RoleReactListener.java +++ b/src/main/java/com/diamondfire/helpbot/sys/rolereact/RoleReactListener.java @@ -35,7 +35,9 @@ public RoleReactListener() { roleMap.put(button.getId(), role.getRoleID()); } - msg.editMessage("__**Reaction Roles**__ \nClick to add/remove roles from yourself").setActionRow(buttons).queue(); + msg.editMessage("__**Reaction Roles**__ \nClick to add/remove roles from yourself") + .setComponents(Util.of(buttons)) + .queue(); }); } diff --git a/src/main/java/com/diamondfire/helpbot/sys/tag/Tag.java b/src/main/java/com/diamondfire/helpbot/sys/tag/Tag.java index 4f47f71d..b8c89ed1 100644 --- a/src/main/java/com/diamondfire/helpbot/sys/tag/Tag.java +++ b/src/main/java/com/diamondfire/helpbot/sys/tag/Tag.java @@ -5,6 +5,7 @@ import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel; import org.jetbrains.annotations.NotNull; import java.io.Serializable; @@ -66,7 +67,7 @@ public void setImage(String image) { this.image = image; } - public void sendResponse(TextChannel channel, @NotNull User requester) { + public void sendResponse(GuildMessageChannel channel, @NotNull User requester) { EmbedBuilder embed = new EmbedBuilder() .setTitle(getTitle()) 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 324c0f1c..2959bede 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..93207b64 --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/sys/tasks/impl/VIPStarTask.java @@ -0,0 +1,51 @@ +package com.diamondfire.helpbot.sys.tasks.impl; + +import com.diamondfire.helpbot.bot.HelpBotInstance; +import com.diamondfire.helpbot.sys.tasks.LoopingTask; +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 java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +public class VIPStarTask implements LoopingTask { + + @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); + Set vipIds = VIPRoleHandler.retrieveVIPs(); + + guild.loadMembers((member) -> { + Role role = VIPRoleHandler.getRole(member.getColorRaw()); + if (role == null) { + return; + } + if (vipIds.contains(member.getIdLong()) && !member.getRoles().contains(role)) { + guild.addRoleToMember(member, role) + .reason("User has VIP Pass") + .queue(); + return; + } + if (!vipIds.contains(member.getIdLong()) && member.getRoles().contains(role)) { + guild.removeRoleFromMember(member, role) + .reason("User's VIP Pass has expired") + .queue(); + } + }); + + } + +} 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..2743407f --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/sys/vip/VIPRoleHandler.java @@ -0,0 +1,114 @@ +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 = "VIP"; + + private static final File FILE = ExternalFiles.VIP_ROLES; + private static final Map COLOR_ROLE_MAP = new HashMap<>(); + + static { + try { + cacheJson(); + + Guild guild = HelpBotInstance.getJda().getGuildById(HelpBotInstance.DF_GUILD); + for (Role role : guild.getRoles()) { + int color = role.getColorRaw(); + if (color == Role.DEFAULT_COLOR_RAW) { + continue; + } + if (role.getIcon() != null) { + continue; + } + if (!COLOR_ROLE_MAP.containsKey(color)) { + // 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()); + } + } + save(); + } 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 getRole(int color) { + if (color == Role.DEFAULT_COLOR_RAW) { + return null; + } + Guild guild = HelpBotInstance.getJda().getGuildById(HelpBotInstance.DF_GUILD); + if (!COLOR_ROLE_MAP.containsKey(color)) { + return null; + } + return guild.getRoleById(COLOR_ROLE_MAP.get(color)); + } + + +} 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..f268069f --- /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 = 64; + + 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, SIZE, SIZE); + g2d.dispose(); + return bufferedImage; + } + +} diff --git a/src/main/resources/star.png b/src/main/resources/star.png new file mode 100644 index 00000000..10d5e0f0 Binary files /dev/null and b/src/main/resources/star.png differ