|
| 1 | +package net.modgarden.gardenbot.commands.event; |
| 2 | + |
| 3 | +import com.google.gson.JsonElement; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import com.google.gson.JsonParser; |
| 6 | +import net.dv8tion.jda.api.entities.User; |
| 7 | +import net.dv8tion.jda.api.interactions.commands.Command; |
| 8 | +import net.dv8tion.jda.api.interactions.commands.OptionMapping; |
| 9 | +import net.modgarden.gardenbot.GardenBot; |
| 10 | +import net.modgarden.gardenbot.interaction.SlashCommandInteraction; |
| 11 | +import net.modgarden.gardenbot.interaction.response.EmbedResponse; |
| 12 | +import net.modgarden.gardenbot.interaction.response.Response; |
| 13 | +import net.modgarden.gardenbot.util.ModGardenAPIClient; |
| 14 | +import org.jetbrains.annotations.Nullable; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.io.InputStreamReader; |
| 19 | +import java.net.http.HttpRequest; |
| 20 | +import java.net.http.HttpResponse; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +public class UnsubmitHandler { |
| 26 | + public static Response handleUnsubmit(SlashCommandInteraction interaction) { |
| 27 | + interaction.event().deferReply(true).queue(); |
| 28 | + User user = interaction.event().getUser(); |
| 29 | + |
| 30 | + JsonObject inputJson = new JsonObject(); |
| 31 | + inputJson.addProperty("discord_id", user.getId()); |
| 32 | + |
| 33 | + @Nullable String event = interaction.event().getOption("event", OptionMapping::getAsString); |
| 34 | + if (event != null) |
| 35 | + inputJson.addProperty("event", event); |
| 36 | + |
| 37 | + String slug = interaction.event().getOption("slug", OptionMapping::getAsString); |
| 38 | + inputJson.addProperty("slug", slug); |
| 39 | + |
| 40 | + try { |
| 41 | + HttpResponse<InputStream> stream = ModGardenAPIClient.post( |
| 42 | + "discord/submission/delete", |
| 43 | + HttpRequest.BodyPublishers.ofString(inputJson.toString()), |
| 44 | + HttpResponse.BodyHandlers.ofInputStream(), |
| 45 | + "Authorization", "Basic " + GardenBot.DOTENV.get("OAUTH_SECRET"), |
| 46 | + "Content-Type", "application/json" |
| 47 | + ); |
| 48 | + JsonElement json = JsonParser.parseReader(new InputStreamReader(stream.body())); |
| 49 | + if (stream.statusCode() == 401 || stream.statusCode() == 422) { |
| 50 | + String errorDescription = json.isJsonObject() && json.getAsJsonObject().has("description") ? |
| 51 | + json.getAsJsonObject().getAsJsonPrimitive("description").getAsString() : |
| 52 | + "Undefined Error."; |
| 53 | + return new EmbedResponse() |
| 54 | + .setTitle("Could not unsubmit your Modrinth project from Mod Garden.") |
| 55 | + .setDescription(errorDescription) |
| 56 | + .setColor(0x5D3E40); |
| 57 | + } else if (stream.statusCode() < 200 || stream.statusCode() > 299) { |
| 58 | + String errorDescription = json.isJsonObject() && json.getAsJsonObject().has("description") ? |
| 59 | + json.getAsJsonObject().getAsJsonPrimitive("description").getAsString() : |
| 60 | + "Undefined Error."; |
| 61 | + return new EmbedResponse() |
| 62 | + .setTitle("Encountered an exception whilst attempting to unsubmit your Modrinth project from Mod Garden.") |
| 63 | + .setDescription(stream.statusCode() + ": " + errorDescription + "\nPlease report this to a team member.") |
| 64 | + .setColor(0xFF0000); |
| 65 | + } |
| 66 | + |
| 67 | + String resultDescription = json.isJsonObject() && json.getAsJsonObject().has("description") ? |
| 68 | + json.getAsJsonObject().getAsJsonPrimitive("description").getAsString() : |
| 69 | + "Undefined Result."; |
| 70 | + return new EmbedResponse() |
| 71 | + .setTitle(resultDescription) |
| 72 | + .setColor(0xA9FFA7); |
| 73 | + } catch (IOException | InterruptedException ex) { |
| 74 | + GardenBot.LOG.error("", ex); |
| 75 | + return new EmbedResponse() |
| 76 | + .setTitle("Encountered an exception whilst attempting to unsubmit your Modrinth project from Mod Garden.") |
| 77 | + .setDescription(ex.getMessage() + "\nPlease report this to a team member.") |
| 78 | + .setColor(0xFF0000); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public static List<Command.Choice> getChoices(String focusedOption) { |
| 83 | + if (focusedOption.equals("slug")) { |
| 84 | + // TODO: Get a user's projects that can be unsubmitted. |
| 85 | + return Collections.emptyList(); |
| 86 | + } |
| 87 | + try { |
| 88 | + var activeEventsResult = ModGardenAPIClient.get("events/active", HttpResponse.BodyHandlers.ofInputStream()); |
| 89 | + if (activeEventsResult.statusCode() == 200) { |
| 90 | + List<Command.Choice> choices = new ArrayList<>(); |
| 91 | + try (InputStreamReader activeEventsReader = new InputStreamReader(activeEventsResult.body())) { |
| 92 | + JsonElement activeEventsJson = JsonParser.parseReader(activeEventsReader); |
| 93 | + if (activeEventsJson.isJsonArray()) { |
| 94 | + for (JsonElement eventJson : activeEventsJson.getAsJsonArray()) { |
| 95 | + if (!eventJson.isJsonObject()) |
| 96 | + continue; |
| 97 | + choices.add(new Command.Choice(eventJson.getAsJsonObject().get("display_name").getAsString(), eventJson.getAsJsonObject().get("slug").getAsString())); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + return choices; |
| 102 | + } |
| 103 | + } catch (IOException | InterruptedException ex) { |
| 104 | + GardenBot.LOG.error("Could not get active events.", ex); |
| 105 | + } |
| 106 | + return Collections.emptyList(); |
| 107 | + } |
| 108 | +} |
0 commit comments