aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarko Zajc <marko@zajc.eu.org>2023-04-10 18:29:43 +0200
committerMarko Zajc <marko@zajc.eu.org>2023-04-10 18:29:43 +0200
commit16cefc208ded3fc054ff8f33d51c766eb6143262 (patch)
treee13d71b99a9b3c371c3d8d9a9afc53faa30824fe
parent05c5927488caaedc31b200b7376e83bb0d2535c5 (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.xml6
-rw-r--r--src/main/java/libot/commands/AvatarCommand.java37
2 files changed, 35 insertions, 8 deletions
diff --git a/pom.xml b/pom.xml
index 31a8ad4..2eb7e68 100644
--- a/pom.xml
+++ b/pom.xml
@@ -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
3import static libot.core.Constants.LITHIUM; 3import static libot.core.Constants.LITHIUM;
4import static libot.core.commands.CommandCategory.INFORMATIVE; 4import static libot.core.commands.CommandCategory.INFORMATIVE;
5import static libot.core.commands.exceptions.ExceptionHandler.reportException;
5import static libot.utils.CommandUtils.findUserOrAuthor; 6import static libot.utils.CommandUtils.findUserOrAuthor;
6import static libot.utils.ParseUtils.parseLong; 7import static libot.utils.ParseUtils.parseLong;
7import static libot.utils.Utilities.array; 8import static libot.utils.Utilities.array;
@@ -10,6 +11,7 @@ import static net.dv8tion.jda.internal.requests.RestActionImpl.getDefaultFailure
10 11
11import javax.annotation.Nonnull; 12import javax.annotation.Nonnull;
12 13
14import kong.unirest.*;
13import libot.core.commands.*; 15import libot.core.commands.*;
14import libot.core.entities.CommandContext; 16import libot.core.entities.CommandContext;
15import libot.core.extensions.EmbedPrebuilder; 17import 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