diff options
author | Marko Zajc <marko@zajc.eu.org> | 2022-11-03 17:33:28 +0100 |
---|---|---|
committer | Marko Zajc <marko@zajc.eu.org> | 2022-11-03 17:33:43 +0100 |
commit | 4a6a8a52d7de17cdf50b12f92a44aed4d65cd7b3 (patch) | |
tree | d4432f61b1c846de13c66985dbb7769cb0b9611b | |
parent | bcb3c4f104fc9c71ef3c29558767f19440595cca (diff) |
Avatar command can now fetch avatars based on user ID
-rw-r--r-- | src/main/java/libot/commands/AvatarCommand.java | 49 |
1 files changed, 38 insertions, 11 deletions
diff --git a/src/main/java/libot/commands/AvatarCommand.java b/src/main/java/libot/commands/AvatarCommand.java index 6606e9d..e60305b 100644 --- a/src/main/java/libot/commands/AvatarCommand.java +++ b/src/main/java/libot/commands/AvatarCommand.java | |||
@@ -1,22 +1,49 @@ | |||
1 | package libot.commands; | 1 | package libot.command; |
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.command.CommandCategory.INFORMATIVE; |
5 | import static libot.utils.Utilities.array; | 5 | import static libot.util.CommandUtils.findUserOrAuthor; |
6 | import static libot.util.ParseUtils.parseLong; | ||
7 | import static libot.util.Utilities.array; | ||
8 | import static net.dv8tion.jda.api.requests.ErrorResponse.UNKNOWN_USER; | ||
9 | import static net.dv8tion.jda.internal.requests.RestActionImpl.getDefaultFailure; | ||
6 | 10 | ||
7 | import libot.core.commands.*; | 11 | import javax.annotation.Nonnull; |
12 | |||
13 | import libot.core.command.*; | ||
8 | import libot.core.entities.CommandContext; | 14 | import libot.core.entities.CommandContext; |
9 | import libot.core.extensions.EmbedPrebuilder; | 15 | import libot.core.extension.EmbedPrebuilder; |
10 | import libot.utils.CommandUtils; | 16 | import net.dv8tion.jda.api.entities.User; |
17 | import net.dv8tion.jda.api.exceptions.ErrorResponseException; | ||
11 | 18 | ||
12 | public class AvatarCommand extends Command { | 19 | public class AvatarCommand extends Command { |
13 | 20 | ||
14 | @Override | 21 | @Override |
22 | @SuppressWarnings("null") | ||
15 | public void execute(CommandContext c) { | 23 | public void execute(CommandContext c) { |
16 | var target = CommandUtils.findUserOrAuthor(c); | 24 | if (c.getMessage().getMentionedUsers().isEmpty() && c.params().check(1)) { |
25 | long id = parseLong(c.params().get(0)); | ||
26 | var user = c.shredder().getUserById(id); | ||
27 | if (user != null) { | ||
28 | sendAvatar(c, user); | ||
29 | } else { | ||
30 | c.jda().retrieveUserById(id).queue(u -> sendAvatar(c, u), e -> { | ||
31 | if (e instanceof ErrorResponseException ere && ere.getErrorResponse() == UNKNOWN_USER) | ||
32 | c.reply("Couldn't find a user with that ID."); | ||
33 | else | ||
34 | getDefaultFailure().accept(e); | ||
35 | }); | ||
36 | } | ||
37 | |||
38 | } else { | ||
39 | sendAvatar(c, findUserOrAuthor(c)); | ||
40 | } | ||
41 | } | ||
42 | |||
43 | private static void sendAvatar(@Nonnull CommandContext c, @Nonnull User user) { | ||
17 | var e = new EmbedPrebuilder(LITHIUM); | 44 | var e = new EmbedPrebuilder(LITHIUM); |
18 | e.setImage(target.getEffectiveAvatarUrl()); | 45 | e.setImage(user.getEffectiveAvatarUrl()); |
19 | e.setDescriptionf("[Download](%s)", target.getEffectiveAvatarUrl()); | 46 | e.setDescriptionf("[Download](%s)", user.getEffectiveAvatarUrl()); |
20 | c.reply(e); | 47 | c.reply(e); |
21 | } | 48 | } |
22 | 49 | ||
@@ -29,12 +56,12 @@ public class AvatarCommand extends Command { | |||
29 | public String getInfo() { | 56 | public String getInfo() { |
30 | return """ | 57 | return """ |
31 | Displays a link to the mentioned user's avatar (profile picture). \ | 58 | Displays a link to the mentioned user's avatar (profile picture). \ |
32 | If no user is mentioned, your avatar will be displayed."""; | 59 | If no user is mentioned and no user ID is provided, your avatar will be displayed."""; |
33 | } | 60 | } |
34 | 61 | ||
35 | @Override | 62 | @Override |
36 | public String[] getParameters() { | 63 | public String[] getParameters() { |
37 | return array("[user]"); | 64 | return array("[user or ID]"); |
38 | } | 65 | } |
39 | 66 | ||
40 | @Override | 67 | @Override |