|
| 1 | +package vc.commands; |
| 2 | + |
| 3 | +import discord4j.core.event.domain.interaction.ChatInputInteractionEvent; |
| 4 | +import discord4j.core.object.entity.Message; |
| 5 | +import discord4j.core.spec.EmbedCreateSpec; |
| 6 | +import discord4j.rest.util.Color; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.springframework.stereotype.Component; |
| 9 | +import reactor.core.publisher.Mono; |
| 10 | +import vc.api.LabyRestClient; |
| 11 | +import vc.api.model.LabyProfileSearchResponse; |
| 12 | +import vc.openapi.handler.ApiException; |
| 13 | +import vc.util.DiscordMarkdownEscape; |
| 14 | + |
| 15 | +import java.net.http.HttpTimeoutException; |
| 16 | + |
| 17 | +import static org.slf4j.LoggerFactory.getLogger; |
| 18 | + |
| 19 | +@Component |
| 20 | +public class NamesCommand implements SlashCommand { |
| 21 | + private static final Logger LOGGER = getLogger(NamesCommand.class); |
| 22 | + private final LabyRestClient labyRestClient; |
| 23 | + |
| 24 | + public NamesCommand(final LabyRestClient labyRestClient) { |
| 25 | + this.labyRestClient = labyRestClient; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public String getName() { |
| 30 | + return "names"; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public Mono<Message> handle(final ChatInputInteractionEvent event) { |
| 35 | + var player = event.getOptionAsString("player").orElse(null); |
| 36 | + if (player == null) { |
| 37 | + return error(event, "`player` option is required"); |
| 38 | + } |
| 39 | + LabyProfileSearchResponse searchResponse; |
| 40 | + try { |
| 41 | + searchResponse = labyRestClient.searchProfiles(player); |
| 42 | + } catch (Exception e) { |
| 43 | + if (e instanceof ApiException apiException) { |
| 44 | + if (apiException.getCause() instanceof HttpTimeoutException httpTimeoutException) { |
| 45 | + LOGGER.error("Timed out searching for names: {}", player, httpTimeoutException); |
| 46 | + return error(event, "Timed out searching. Try again in a minute"); |
| 47 | + } |
| 48 | + } |
| 49 | + LOGGER.error("Error searching for names: {}", player, e); |
| 50 | + return error(event, "Error searching. Try again later"); |
| 51 | + } |
| 52 | + var builder = EmbedCreateSpec.builder() |
| 53 | + .title("Name Search") |
| 54 | + .addField("Searched Name", DiscordMarkdownEscape.escape(player), true) |
| 55 | + .addField("\u200B", "\u200B", true) |
| 56 | + .addField("\u200B", "\u200B", true) |
| 57 | + .addField("Source", "[LabyMod API](https://laby.net/@" + player + ")", true) |
| 58 | + .color(Color.CYAN); |
| 59 | + var sb = new StringBuilder(); |
| 60 | + var currentProfile = searchResponse.currentProfile(); |
| 61 | + sb.append("**Current Profile**\n\n"); |
| 62 | + if (currentProfile != null) { |
| 63 | + sb.append(currentProfile.toDiscordFieldValue()).append("\n"); |
| 64 | + builder.thumbnail(currentProfile.getAvatarURL()); |
| 65 | + } else { |
| 66 | + sb.append("(none)\n"); |
| 67 | + } |
| 68 | + var prevUsernames = searchResponse.previousUsernames(); |
| 69 | + sb.append("\n**Previous Usernames**\n\n"); |
| 70 | + if (!prevUsernames.isEmpty()) { |
| 71 | + for (var name : prevUsernames) { |
| 72 | + sb.append(DiscordMarkdownEscape.escape(name)).append("\n"); |
| 73 | + } |
| 74 | + } else { |
| 75 | + sb.append("(none)\n"); |
| 76 | + } |
| 77 | + var historicalProfiles = searchResponse.historicalProfiles(); |
| 78 | + sb.append("\n**Previous Accounts**\n\n"); |
| 79 | + if (!historicalProfiles.isEmpty()) { |
| 80 | + for (var historicalProfile : historicalProfiles) { |
| 81 | + sb.append(historicalProfile.toDiscordFieldValue()).append("\n"); |
| 82 | + } |
| 83 | + } else { |
| 84 | + sb.append("(none)\n"); |
| 85 | + } |
| 86 | + var names = searchResponse.associatedUsernames(); |
| 87 | + sb.append("\n**Previous Account Usernames**\n\n"); |
| 88 | + if (!names.isEmpty()) { |
| 89 | + for (var name : names) { |
| 90 | + sb.append(name).append("\n"); |
| 91 | + } |
| 92 | + } else { |
| 93 | + sb.append("(none)\n"); |
| 94 | + } |
| 95 | + builder.description(sb.toString()); |
| 96 | + return event.createFollowup().withEmbeds(builder.build()); |
| 97 | + } |
| 98 | +} |
0 commit comments