|
| 1 | +package org.togetherjava.tjbot.commands.moderation; |
| 2 | + |
| 3 | +import net.dv8tion.jda.api.EmbedBuilder; |
| 4 | +import net.dv8tion.jda.api.entities.Member; |
| 5 | +import net.dv8tion.jda.api.entities.Role; |
| 6 | +import net.dv8tion.jda.api.entities.User; |
| 7 | +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; |
| 8 | +import net.dv8tion.jda.api.interactions.commands.OptionMapping; |
| 9 | +import net.dv8tion.jda.api.interactions.commands.OptionType; |
| 10 | +import net.dv8tion.jda.api.interactions.components.Button; |
| 11 | +import net.dv8tion.jda.api.interactions.components.ButtonStyle; |
| 12 | +import org.jetbrains.annotations.NotNull; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | +import org.togetherjava.tjbot.commands.SlashCommandAdapter; |
| 16 | +import org.togetherjava.tjbot.commands.SlashCommandVisibility; |
| 17 | + |
| 18 | +import java.time.Instant; |
| 19 | +import java.time.OffsetDateTime; |
| 20 | +import java.time.format.DateTimeFormatter; |
| 21 | +import java.util.Objects; |
| 22 | +import java.util.stream.Collectors; |
| 23 | + |
| 24 | +public class WhoIsCommand extends SlashCommandAdapter { |
| 25 | + private static final Logger logger = LoggerFactory.getLogger(WhoIsCommand.class); |
| 26 | + |
| 27 | + private static final String USER_OPTION = "user"; |
| 28 | + private static final String HIDE_MEMBER_INFO = "hide_member_info"; |
| 29 | + private static final DateTimeFormatter DATE_TIME_FORMAT = |
| 30 | + DateTimeFormatter.ofPattern("E, MMMM d, u, HH:mm:ss"); |
| 31 | + |
| 32 | + public WhoIsCommand() { |
| 33 | + super("whois", "who is the given user?", SlashCommandVisibility.GUILD); |
| 34 | + |
| 35 | + getData().addOption(OptionType.USER, USER_OPTION, "the user to look up", true); |
| 36 | + getData().addOption(OptionType.BOOLEAN, HIDE_MEMBER_INFO, |
| 37 | + "whenever to show the member info of this user", false); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void onSlashCommand(@NotNull final SlashCommandEvent event) { |
| 42 | + OptionMapping userOption = Objects.requireNonNull(event.getOption(USER_OPTION), |
| 43 | + "The given user option cannot be null"); |
| 44 | + |
| 45 | + User user = userOption.getAsUser(); |
| 46 | + Member member = userOption.getAsMember(); |
| 47 | + |
| 48 | + if (null != member) { |
| 49 | + OptionMapping hideMemberInfoOption = event.getOption(HIDE_MEMBER_INFO); |
| 50 | + |
| 51 | + if (null != hideMemberInfoOption && hideMemberInfoOption.getAsBoolean()) { |
| 52 | + handleWhoIsUser(event, user); |
| 53 | + } else { |
| 54 | + handleWhoIsMember(event, member, user); |
| 55 | + } |
| 56 | + } else { |
| 57 | + handleWhoIsUser(event, user); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private static void handleWhoIsUser(final @NotNull SlashCommandEvent event, |
| 62 | + final @NotNull User user) { |
| 63 | + @SuppressWarnings("StringConcatenation") |
| 64 | + EmbedBuilder embedBuilder = new EmbedBuilder() |
| 65 | + .setAuthor(user.getName(), user.getEffectiveAvatarUrl(), user.getEffectiveAvatarUrl()) |
| 66 | + .setThumbnail(user.getEffectiveAvatarUrl()) |
| 67 | + .appendDescription("**Mention:** " + user.getAsMention()) |
| 68 | + .appendDescription("\n**Tag:** " + user.getAsTag()) |
| 69 | + .appendDescription("\n**ID:** " + user.getId()) |
| 70 | + .appendDescription("\n**Is bot:** " + (user.isBot())) |
| 71 | + .appendDescription( |
| 72 | + "\n**Registration date:** " + DATE_TIME_FORMAT.format(user.getTimeCreated())) |
| 73 | + .setTimestamp(Instant.now()) |
| 74 | + .setFooter("Requested by " + event.getUser().getAsTag(), |
| 75 | + event.getMember().getEffectiveAvatarUrl()); |
| 76 | + |
| 77 | + |
| 78 | + event.replyEmbeds(embedBuilder.build()) |
| 79 | + .addActionRow(Button.of(ButtonStyle.LINK, "discord://-/users/" + user.getId(), |
| 80 | + "Click to see profile")) |
| 81 | + .queue(); |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + private static void handleWhoIsMember(final @NotNull SlashCommandEvent event, |
| 86 | + final @NotNull Member member, final @NotNull User user) { |
| 87 | + |
| 88 | + @SuppressWarnings("StringConcatenation") |
| 89 | + EmbedBuilder embedBuilder = new EmbedBuilder() |
| 90 | + .setAuthor(member.getEffectiveName(), member.getEffectiveAvatarUrl(), |
| 91 | + member.getEffectiveAvatarUrl()) |
| 92 | + .setThumbnail(user.getEffectiveAvatarUrl()) |
| 93 | + .appendDescription("**Mention:** " + user.getAsMention()) |
| 94 | + .appendDescription("\n**Tag:** " + user.getAsTag()) |
| 95 | + .appendDescription("\n**ID:** " + user.getId()) |
| 96 | + .appendDescription("\n**Is bot:** " + (user.isBot())) |
| 97 | + .appendDescription(boostingMemberToString(member)) |
| 98 | + .appendDescription( |
| 99 | + "\n**Join date:** " + DATE_TIME_FORMAT.format(member.getTimeJoined())) |
| 100 | + .appendDescription( |
| 101 | + "\n**Registration date:** " + DATE_TIME_FORMAT.format(user.getTimeCreated())) |
| 102 | + .appendDescription("\n**Roles:** " + formatRoles(member)) |
| 103 | + .setColor(member.getColorRaw()) |
| 104 | + .setTimestamp(Instant.now()) |
| 105 | + .setFooter("Requested by " + event.getUser().getAsTag(), |
| 106 | + event.getMember().getEffectiveAvatarUrl()); |
| 107 | + |
| 108 | + |
| 109 | + event.replyEmbeds(embedBuilder.build()) |
| 110 | + .addActionRow(Button.of(ButtonStyle.LINK, "discord://-/users/" + user.getId(), |
| 111 | + "Click to see profile")) |
| 112 | + .queue(); |
| 113 | + } |
| 114 | + |
| 115 | + private static @NotNull String boostingMemberToString(final @NotNull Member member) { |
| 116 | + OffsetDateTime timeBoosted = member.getTimeBoosted(); |
| 117 | + if (null != timeBoosted) { |
| 118 | + return "\n**Is booster:** true" + "\n**Boosting since:** " |
| 119 | + + DATE_TIME_FORMAT.format(timeBoosted); |
| 120 | + } else { |
| 121 | + return "\n**Is booster:** false"; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /* |
| 126 | + * .addField("Mention:", user.getAsMention(), false) .addField("Tag:", user.getAsTag(), false) |
| 127 | + * .addField("ID", user.getId(), false) .addField("Join date", |
| 128 | + * DATE_TIME_FORMAT.format(member.getTimeJoined()), true) .addField("Registration date", |
| 129 | + * DATE_TIME_FORMAT.format(user.getTimeCreated()), true) .addField("Roles", formatRoles(member), |
| 130 | + * false) |
| 131 | + */ |
| 132 | + |
| 133 | + private static String formatRoles(final Member member) { |
| 134 | + return member.getRoles().stream().map(Role::getAsMention).collect(Collectors.joining(", ")); |
| 135 | + } |
| 136 | +} |
0 commit comments