diff options
author | Marko Zajc <marko@zajc.eu.org> | 2023-04-10 19:31:24 +0200 |
---|---|---|
committer | Marko Zajc <marko@zajc.eu.org> | 2023-04-10 19:31:24 +0200 |
commit | 41597f2fbb60be7704f29c9ff51bd0eeb9dc8aef (patch) | |
tree | 120794a6c65cf2c973ecde34fb0827ffc593ff65 | |
parent | 16cefc208ded3fc054ff8f33d51c766eb6143262 (diff) |
Include avatar resolution in *avatar
-rw-r--r-- | src/main/java/libot/commands/AvatarCommand.java | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/src/main/java/libot/commands/AvatarCommand.java b/src/main/java/libot/commands/AvatarCommand.java index da3f069..2d5395e 100644 --- a/src/main/java/libot/commands/AvatarCommand.java +++ b/src/main/java/libot/commands/AvatarCommand.java | |||
@@ -1,5 +1,6 @@ | |||
1 | package libot.commands; | 1 | package libot.commands; |
2 | 2 | ||
3 | import static javax.imageio.ImageIO.*; | ||
3 | import static libot.core.Constants.LITHIUM; | 4 | import static libot.core.Constants.LITHIUM; |
4 | import static libot.core.commands.CommandCategory.INFORMATIVE; | 5 | import static libot.core.commands.CommandCategory.INFORMATIVE; |
5 | import static libot.core.commands.exceptions.ExceptionHandler.reportException; | 6 | import static libot.core.commands.exceptions.ExceptionHandler.reportException; |
@@ -9,8 +10,12 @@ import static libot.utils.Utilities.array; | |||
9 | import static net.dv8tion.jda.api.requests.ErrorResponse.UNKNOWN_USER; | 10 | import static net.dv8tion.jda.api.requests.ErrorResponse.UNKNOWN_USER; |
10 | import static net.dv8tion.jda.internal.requests.RestActionImpl.getDefaultFailure; | 11 | import static net.dv8tion.jda.internal.requests.RestActionImpl.getDefaultFailure; |
11 | 12 | ||
13 | import java.io.*; | ||
14 | |||
12 | import javax.annotation.Nonnull; | 15 | import javax.annotation.Nonnull; |
13 | 16 | ||
17 | import org.slf4j.*; | ||
18 | |||
14 | import kong.unirest.*; | 19 | import kong.unirest.*; |
15 | import libot.core.commands.*; | 20 | import libot.core.commands.*; |
16 | import libot.core.entities.CommandContext; | 21 | import libot.core.entities.CommandContext; |
@@ -21,6 +26,8 @@ import net.dv8tion.jda.api.exceptions.ErrorResponseException; | |||
21 | 26 | ||
22 | public class AvatarCommand extends Command { | 27 | public class AvatarCommand extends Command { |
23 | 28 | ||
29 | private static final Logger LOG = LoggerFactory.getLogger(AvatarCommand.class); | ||
30 | |||
24 | @Override | 31 | @Override |
25 | @SuppressWarnings("null") | 32 | @SuppressWarnings("null") |
26 | public void execute(CommandContext c) { | 33 | public void execute(CommandContext c) { |
@@ -63,14 +70,41 @@ public class AvatarCommand extends Command { | |||
63 | sendAvatar(c, url, b.getBody()); | 70 | sendAvatar(c, url, b.getBody()); |
64 | } | 71 | } |
65 | 72 | ||
73 | @SuppressWarnings("null") | ||
66 | private static void sendAvatar(@Nonnull CommandContext c, @Nonnull String url, @Nonnull byte[] data) { | 74 | private static void sendAvatar(@Nonnull CommandContext c, @Nonnull String url, @Nonnull byte[] data) { |
67 | var extension = url.substring(url.lastIndexOf('.') + 1); | 75 | var extension = url.substring(url.lastIndexOf('.') + 1); |
68 | var e = new EmbedPrebuilder(LITHIUM); | 76 | var e = new EmbedPrebuilder(LITHIUM); |
69 | e.setDescriptionf("[View original (%d KiB)](%s?size=4096)", data.length / 1024, url) | 77 | e.setImage("attachment://avatar." + extension); |
70 | .setImage("attachment://avatar." + extension); | 78 | e.setDescriptionf("[View original](%s?size=4096) (%d KiB%s)", url, data.length / 1024, |
79 | getImageResolutionString(data, extension)); | ||
71 | c.getChannel().sendFile(data, "avatar." + extension).setEmbeds(e.build()).queue(); | 80 | c.getChannel().sendFile(data, "avatar." + extension).setEmbeds(e.build()).queue(); |
72 | } | 81 | } |
73 | 82 | ||
83 | @Nonnull | ||
84 | private static String getImageResolutionString(@Nonnull byte[] data, @Nonnull String extension) { | ||
85 | var resolution = getImageDimension(data, extension); | ||
86 | return resolution == -1 ? "" : ", " + resolution + " × " + resolution + " pixels"; | ||
87 | } | ||
88 | |||
89 | private static int getImageDimension(@Nonnull byte[] data, @Nonnull String extension) { | ||
90 | var readers = getImageReadersBySuffix(extension); | ||
91 | while (readers.hasNext()) { | ||
92 | var reader = readers.next(); | ||
93 | try (var is = createImageInputStream(new ByteArrayInputStream(data))) { | ||
94 | reader.setInput(is); | ||
95 | return reader.getWidth(reader.getMinIndex()); | ||
96 | |||
97 | } catch (IOException e) { | ||
98 | LOG.warn("Failed to decode the image resolution for {}", extension); | ||
99 | LOG.warn("", e); | ||
100 | |||
101 | } finally { | ||
102 | reader.dispose(); | ||
103 | } | ||
104 | } | ||
105 | return -1; | ||
106 | } | ||
107 | |||
74 | @Override | 108 | @Override |
75 | public String getName() { | 109 | public String getName() { |
76 | return "avatar"; | 110 | return "avatar"; |