diff options
author | Marko Zajc <marko@zajc.eu.org> | 2023-04-10 18:29:43 +0200 |
---|---|---|
committer | Marko Zajc <marko@zajc.eu.org> | 2023-04-10 18:29:43 +0200 |
commit | 16cefc208ded3fc054ff8f33d51c766eb6143262 (patch) | |
tree | e13d71b99a9b3c371c3d8d9a9afc53faa30824fe | |
parent | 05c5927488caaedc31b200b7376e83bb0d2535c5 (diff) |
Reupload avatar data in *avatar as an attachment
This prevents the data from being lost when the user removes or changes
their avatar
-rw-r--r-- | pom.xml | 6 | ||||
-rw-r--r-- | src/main/java/libot/commands/AvatarCommand.java | 37 |
2 files changed, 35 insertions, 8 deletions
@@ -36,6 +36,12 @@ | |||
36 | <groupId>zajc.libot</groupId> | 36 | <groupId>zajc.libot</groupId> |
37 | <artifactId>core</artifactId> | 37 | <artifactId>core</artifactId> |
38 | </dependency> | 38 | </dependency> |
39 | |||
40 | <!-- HTTP --> | ||
41 | <dependency> | ||
42 | <groupId>com.konghq</groupId> | ||
43 | <artifactId>unirest-java</artifactId> | ||
44 | </dependency> | ||
39 | </dependencies> | 45 | </dependencies> |
40 | 46 | ||
41 | <build> | 47 | <build> |
diff --git a/src/main/java/libot/commands/AvatarCommand.java b/src/main/java/libot/commands/AvatarCommand.java index 1d21ade..da3f069 100644 --- a/src/main/java/libot/commands/AvatarCommand.java +++ b/src/main/java/libot/commands/AvatarCommand.java | |||
@@ -2,6 +2,7 @@ package libot.commands; | |||
2 | 2 | ||
3 | import static libot.core.Constants.LITHIUM; | 3 | import static libot.core.Constants.LITHIUM; |
4 | import static libot.core.commands.CommandCategory.INFORMATIVE; | 4 | import static libot.core.commands.CommandCategory.INFORMATIVE; |
5 | import static libot.core.commands.exceptions.ExceptionHandler.reportException; | ||
5 | import static libot.utils.CommandUtils.findUserOrAuthor; | 6 | import static libot.utils.CommandUtils.findUserOrAuthor; |
6 | import static libot.utils.ParseUtils.parseLong; | 7 | import static libot.utils.ParseUtils.parseLong; |
7 | import static libot.utils.Utilities.array; | 8 | import static libot.utils.Utilities.array; |
@@ -10,6 +11,7 @@ import static net.dv8tion.jda.internal.requests.RestActionImpl.getDefaultFailure | |||
10 | 11 | ||
11 | import javax.annotation.Nonnull; | 12 | import javax.annotation.Nonnull; |
12 | 13 | ||
14 | import kong.unirest.*; | ||
13 | import libot.core.commands.*; | 15 | import libot.core.commands.*; |
14 | import libot.core.entities.CommandContext; | 16 | import libot.core.entities.CommandContext; |
15 | import libot.core.extensions.EmbedPrebuilder; | 17 | import libot.core.extensions.EmbedPrebuilder; |
@@ -26,9 +28,9 @@ public class AvatarCommand extends Command { | |||
26 | long id = parseLong(c.params().get(0)); | 28 | long id = parseLong(c.params().get(0)); |
27 | var user = c.shredder().getUserById(id); | 29 | var user = c.shredder().getUserById(id); |
28 | if (user != null) { | 30 | if (user != null) { |
29 | sendAvatar(c, user); | 31 | downloadAndSendAvatar(c, user); |
30 | } else { | 32 | } else { |
31 | c.jda().retrieveUserById(id).queue(u -> sendAvatar(c, u), e -> { | 33 | c.jda().retrieveUserById(id).queue(u -> downloadAndSendAvatar(c, u), e -> { |
32 | if (e instanceof ErrorResponseException ere && ere.getErrorResponse() == UNKNOWN_USER) { | 34 | if (e instanceof ErrorResponseException ere && ere.getErrorResponse() == UNKNOWN_USER) { |
33 | c.reply("Couldn't find a user with that ID."); | 35 | c.reply("Couldn't find a user with that ID."); |
34 | } else { | 36 | } else { |
@@ -38,16 +40,35 @@ public class AvatarCommand extends Command { | |||
38 | } | 40 | } |
39 | 41 | ||
40 | } else { | 42 | } else { |
41 | sendAvatar(c, findUserOrAuthor(c)); | 43 | downloadAndSendAvatar(c, findUserOrAuthor(c)); |
42 | } | 44 | } |
43 | } | 45 | } |
44 | 46 | ||
45 | private static void sendAvatar(@Nonnull CommandContext c, @Nonnull User user) { | 47 | @SuppressWarnings("null") |
46 | var url = user.getEffectiveAvatarUrl() + "?size=4096"; | 48 | private static void downloadAndSendAvatar(@Nonnull CommandContext c, @Nonnull User user) { |
49 | var url = user.getEffectiveAvatarUrl(); | ||
50 | Unirest.get(url + "?size=4096").asBytesAsync().thenAccept(b -> sendAvatar(c, url, b)).exceptionally(t -> { | ||
51 | reportException(c, t); | ||
52 | c.error("Couldn't download the avatar due to an unknown error"); | ||
53 | return null; | ||
54 | }); | ||
55 | } | ||
56 | |||
57 | @SuppressWarnings("null") | ||
58 | private static void sendAvatar(@Nonnull CommandContext c, @Nonnull String url, @Nonnull HttpResponse<byte[]> b) { | ||
59 | if (!b.isSuccess()) | ||
60 | throw new RuntimeException("Non-zero status code when downloading '%s?size=4096':%d %s" | ||
61 | .formatted(url, b.getStatus(), b.getStatusText())); | ||
62 | |||
63 | sendAvatar(c, url, b.getBody()); | ||
64 | } | ||
65 | |||
66 | private static void sendAvatar(@Nonnull CommandContext c, @Nonnull String url, @Nonnull byte[] data) { | ||
67 | var extension = url.substring(url.lastIndexOf('.') + 1); | ||
47 | var e = new EmbedPrebuilder(LITHIUM); | 68 | var e = new EmbedPrebuilder(LITHIUM); |
48 | e.setImage(url); | 69 | e.setDescriptionf("[View original (%d KiB)](%s?size=4096)", data.length / 1024, url) |
49 | e.setDescriptionf("[Download](%s)", url); | 70 | .setImage("attachment://avatar." + extension); |
50 | c.reply(e); | 71 | c.getChannel().sendFile(data, "avatar." + extension).setEmbeds(e.build()).queue(); |
51 | } | 72 | } |
52 | 73 | ||
53 | @Override | 74 | @Override |