|
| 1 | +package rocks.learnercouncil.cameron.commands; |
| 2 | + |
| 3 | +import net.dv8tion.jda.api.EmbedBuilder; |
| 4 | +import net.dv8tion.jda.api.entities.MessageEmbed; |
| 5 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 6 | +import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; |
| 7 | +import net.dv8tion.jda.api.hooks.ListenerAdapter; |
| 8 | +import net.dv8tion.jda.api.interactions.components.buttons.Button; |
| 9 | +import okhttp3.OkHttpClient; |
| 10 | +import okhttp3.Request; |
| 11 | +import okhttp3.Response; |
| 12 | +import org.jetbrains.annotations.NotNull; |
| 13 | +import rocks.learnercouncil.cameron.Cameron; |
| 14 | + |
| 15 | +import java.awt.Color; |
| 16 | +import java.io.IOException; |
| 17 | +import java.time.Instant; |
| 18 | +import java.util.*; |
| 19 | +import java.util.List; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | +import java.util.regex.Matcher; |
| 22 | +import java.util.regex.Pattern; |
| 23 | + |
| 24 | +public class JoinlistCommand extends ListenerAdapter { |
| 25 | + |
| 26 | + private static final OkHttpClient client = new OkHttpClient(); |
| 27 | + |
| 28 | + private static final Button acceptButton = Button.success("jl_accept", "Accept"); |
| 29 | + private static final Button denyButton = Button.danger("jl_deny", "Deny"); |
| 30 | + |
| 31 | + @Override |
| 32 | + public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) { |
| 33 | + if(event.getOption("username") == null) return; |
| 34 | + String username = Objects.requireNonNull(event.getOption("username")).getAsString(); |
| 35 | + String jsonString = sendRequest(username); |
| 36 | + if(jsonString.startsWith("#ERR INVALID_CHARS")) { |
| 37 | + String invalid_characters = jsonString.split(":")[1]; |
| 38 | + StringBuilder stringBuilder = new StringBuilder(); |
| 39 | + for(char character : invalid_characters.toCharArray()) { |
| 40 | + stringBuilder.append('\''); |
| 41 | + stringBuilder.append(character); |
| 42 | + stringBuilder.append("', "); |
| 43 | + } |
| 44 | + event.reply("The username you typed contains characters that can't be in a Minecraft username: " + stringBuilder.substring(0, stringBuilder.length() - 2) + |
| 45 | + "Please check to make sure you spelled your username correctly.").setEphemeral(true).queue(); |
| 46 | + return; |
| 47 | + } |
| 48 | + if(jsonString.isEmpty()) { |
| 49 | + event.reply("I can't find the minecraft username '" + username + "', check that it's spelled correctly.").setEphemeral(true).queue(); |
| 50 | + return; |
| 51 | + } |
| 52 | + Map<String, String> json = parseJson(jsonString); |
| 53 | + if(json.containsKey("error")) { |
| 54 | + event.reply("I can't find the minecraft username '" + username + "', check that it's spelled correctly.").setEphemeral(true).queue(); |
| 55 | + return; |
| 56 | + } |
| 57 | + if (!json.containsKey("id")) return; |
| 58 | + if(event.getMember() == null) return; |
| 59 | + |
| 60 | + Cameron.getExistingChannel("mc-joinlist").sendMessageEmbeds( |
| 61 | + new EmbedBuilder() |
| 62 | + .setColor(Color.YELLOW) |
| 63 | + .setAuthor("Request Pending...") |
| 64 | + .setDescription(event.getUser().getAsMention() + " is requesting to join the Minecraft server.\n" + |
| 65 | + "If you wish to accept the request, add them to the joinlist and click **Accept**. Otherwise, click **Deny**.") |
| 66 | + .addField(new MessageEmbed.Field("> **Name:**", event.getUser().getAsMention(), false)) |
| 67 | + .addField(new MessageEmbed.Field("> **Username:**", json.get("name"), false)) |
| 68 | + .addField(new MessageEmbed.Field("> **UUID:**", formatUUID(json.get("id")), false)) |
| 69 | + .build() |
| 70 | + ).setActionRow(acceptButton, denyButton).queue(); |
| 71 | + |
| 72 | + event.reply("Request sent! A council member will review it soon.").setEphemeral(true).queue(); |
| 73 | + } |
| 74 | + |
| 75 | + private String formatUUID(String uuid) { |
| 76 | + return new StringBuilder(uuid).insert(8, '-').insert(13, '-').insert(18, '-').insert(23, '-').toString(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void onButtonInteraction(@NotNull ButtonInteractionEvent event) { |
| 81 | + if(event.getComponentId().equals("jl_accept")) { |
| 82 | + List<MessageEmbed.Field> fields = new ArrayList<>(); |
| 83 | + MessageEmbed embed = event.getMessage().getEmbeds().get(0); |
| 84 | + if(!embed.getFields().isEmpty()) |
| 85 | + fields = embed.getFields(); |
| 86 | + |
| 87 | + event.editMessageEmbeds( |
| 88 | + new EmbedBuilder() |
| 89 | + .setColor(Color.GREEN) |
| 90 | + .setAuthor("Accepted.") |
| 91 | + .setDescription("Request accepted.\n") |
| 92 | + .addField(fields.get(0)) |
| 93 | + .addField(fields.get(1)) |
| 94 | + .addField(fields.get(2)) |
| 95 | + .setTimestamp(Instant.now()) |
| 96 | + .build()).setActionRows().queue(); |
| 97 | + return; |
| 98 | + } |
| 99 | + if(event.getComponentId().equals("jl_deny")) { |
| 100 | + event.editMessageEmbeds(new EmbedBuilder() |
| 101 | + .setAuthor("Denied.") |
| 102 | + .setColor(Color.RED) |
| 103 | + .setDescription("Request Denied.") |
| 104 | + .build()).setActionRows().queue(m -> m.deleteOriginal().queueAfter(120, TimeUnit.SECONDS)); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private String sendRequest(String username) { |
| 109 | + Pattern pattern = Pattern.compile("[a-zA-Z0-9\\-_]+"); |
| 110 | + Matcher matcher = pattern.matcher(username); |
| 111 | + if(!matcher.matches()) return "#ERR INVALID_CHARS:" + matcher.replaceAll(""); |
| 112 | + String url = "https://api.mojang.com/users/profiles/minecraft/" + username; |
| 113 | + Request request = new Request.Builder() |
| 114 | + .url(url) |
| 115 | + .header("User-Agent", "application/json") |
| 116 | + .build(); |
| 117 | + try { |
| 118 | + Response response = client.newCall(request).execute(); |
| 119 | + //System.out.println(Objects.requireNonNull(response.body()).string()); |
| 120 | + return Objects.requireNonNull(response.body()).string(); |
| 121 | + } catch (IOException e) { |
| 122 | + System.out.println("IOException occured via /joinlist"); |
| 123 | + return "#ERR IOEXCEPTION"; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private Map<String, String> parseJson(String json) { |
| 128 | + json = json.replace("\s*", ""); |
| 129 | + Map<String, String> map = new LinkedHashMap<>(); |
| 130 | + String[] pairs = json.substring(1, json.length() - 1).split(","); |
| 131 | + for(String pair : pairs) { |
| 132 | + String[] parts = pair.split(":"); |
| 133 | + if(parts.length != 2) continue; |
| 134 | + map.put(parts[0].substring(1, parts[0].length() - 1), parts[1].substring(1, parts[1].length() - 1)); |
| 135 | + } |
| 136 | + return map; |
| 137 | + } |
| 138 | +} |
0 commit comments