From f64252f5c0c4ea7555895da0976c5c1da2a83b60 Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Mon, 15 Mar 2021 09:07:20 +0100 Subject: [PATCH] emote limit check --- .../commands/util/EmoteAddCommand.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/github/nilstrieb/commands/util/EmoteAddCommand.java b/src/main/java/com/github/nilstrieb/commands/util/EmoteAddCommand.java index f9bf578..e161893 100644 --- a/src/main/java/com/github/nilstrieb/commands/util/EmoteAddCommand.java +++ b/src/main/java/com/github/nilstrieb/commands/util/EmoteAddCommand.java @@ -29,9 +29,11 @@ public class EmoteAddCommand extends Command { List attachments = event.getMessage().getAttachments(); Member author = event.getGuild().retrieveMember(event.getAuthor()).complete(); - if(!author.getPermissions().contains(Permission.MANAGE_EMOTES)) { + if (!author.getPermissions().contains(Permission.MANAGE_EMOTES)) { reply("You don't have the permissions to do that."); - } else if (attachments.size() == 0 || !attachments.get(0).isImage()) { + } else if (emoteLimitReached()) { + reply("Emote limit reached"); + } else if (attachments.isEmpty() || !attachments.get(0).isImage()) { reply("No image attached"); } else if (args.length() < 3) { reply("Name must be at least 3 characters: " + args); @@ -54,6 +56,13 @@ public class EmoteAddCommand extends Command { } } + private boolean emoteLimitReached() { + int max = event.getGuild().getMaxEmotes(); + int count = event.getGuild().retrieveEmotes().complete().size(); + + return max == count; + } + private byte[] readImage(Message.Attachment image) throws IOException { String urlString = image.getUrl(); URL url = new URL(urlString); @@ -61,10 +70,11 @@ public class EmoteAddCommand extends Command { byte[] chunk = new byte[4096]; int bytesRead; - InputStream stream = url.openStream(); - while ((bytesRead = stream.read(chunk)) > 0) { - out.write(chunk, 0, bytesRead); + try (InputStream stream = url.openStream()) { + while ((bytesRead = stream.read(chunk)) > 0) { + out.write(chunk, 0, bytesRead); + } } return out.toByteArray();