Skip to content

Commit 49bda92

Browse files
Merge remote-tracking branch 'origin/master'
2 parents e7144c9 + fe6e81e commit 49bda92

File tree

3 files changed

+141
-13
lines changed

3 files changed

+141
-13
lines changed

src/main/java/net/modgarden/gardenbot/GardenBotCommands.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.modgarden.gardenbot.commands.event.RegisterHandler;
66
import net.modgarden.gardenbot.commands.event.SubmitHandler;
77
import net.modgarden.gardenbot.commands.event.UnregisterHandler;
8+
import net.modgarden.gardenbot.commands.event.UnsubmitHandler;
89
import net.modgarden.gardenbot.interaction.command.SlashCommand;
910
import net.modgarden.gardenbot.interaction.command.SlashCommandDispatcher;
1011
import net.modgarden.gardenbot.interaction.command.SlashCommandOption;
@@ -75,15 +76,23 @@ public static void registerAll() {
7576
"unregister",
7677
"Unregisters you from a current Mod Garden event.",
7778
UnregisterHandler::handleEventUnregister
78-
)));
79-
SlashCommandDispatcher.register(new SlashCommand("submit", "Submit your project to a current Mod Garden event.",
79+
),
8080
new SlashCommand.SubCommand(
81-
"modrinth",
82-
"Submits your Modrinth project to a current Mod Garden event.",
83-
SubmitHandler::handleSubmitModrinth,
81+
"submit",
82+
"Submit your Modrinth project to a current Mod Garden event.",
83+
SubmitHandler::handleSubmit,
8484
SubmitHandler::getChoices,
85-
new SlashCommandOption(OptionType.STRING, "slug", "The slug of the Modrinth project to submit.", true, true),
86-
new SlashCommandOption(OptionType.STRING, "event", "The event to submit to. Does not have to be specified ", false, true)
85+
new SlashCommandOption(OptionType.STRING, "source", "The source of your project.", true, true),
86+
new SlashCommandOption(OptionType.STRING, "slug", "The slug of the project to submit.", true, false),
87+
new SlashCommandOption(OptionType.STRING, "event", "A specific event to submit to.", false, true)
88+
),
89+
new SlashCommand.SubCommand(
90+
"unsubmit",
91+
"Unsubmit your Modrinth project from a current Mod Garden event.",
92+
UnsubmitHandler::handleUnsubmit,
93+
UnsubmitHandler::getChoices,
94+
new SlashCommandOption(OptionType.STRING, "slug", "The slug of the project to unsubmit.", true, false),
95+
new SlashCommandOption(OptionType.STRING, "event", "A specific event to unsubmit from.", false, true)
8796
)));
8897

8998
// TODO: Implement Ban command.

src/main/java/net/modgarden/gardenbot/commands/event/SubmitHandler.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.util.List;
2424

2525
public class SubmitHandler {
26-
public static Response handleSubmitModrinth(SlashCommandInteraction interaction) {
26+
public static Response handleSubmit(SlashCommandInteraction interaction) {
2727
interaction.event().deferReply(true).queue();
2828
User user = interaction.event().getUser();
2929

@@ -35,11 +35,19 @@ public static Response handleSubmitModrinth(SlashCommandInteraction interaction)
3535
inputJson.addProperty("event", event);
3636

3737
String slug = interaction.event().getOption("slug", OptionMapping::getAsString);
38-
inputJson.addProperty("modrinth_slug", slug);
38+
inputJson.addProperty("slug", slug);
39+
40+
String source = interaction.event().getOption("source", OptionMapping::getAsString);
41+
if (!"modrinth".equals(source)) {
42+
return new EmbedResponse()
43+
.setTitle("Could not submit your project to Mod Garden.")
44+
.setDescription("Invalid mod source.")
45+
.setColor(0x5D3E40);
46+
}
3947

4048
try {
4149
HttpResponse<InputStream> stream = ModGardenAPIClient.post(
42-
"discord/submission/create",
50+
"discord/submission/create/" + source,
4351
HttpRequest.BodyPublishers.ofString(inputJson.toString()),
4452
HttpResponse.BodyHandlers.ofInputStream(),
4553
"Authorization", "Basic " + GardenBot.DOTENV.get("OAUTH_SECRET"),
@@ -51,15 +59,15 @@ public static Response handleSubmitModrinth(SlashCommandInteraction interaction)
5159
json.getAsJsonObject().getAsJsonPrimitive("description").getAsString() :
5260
"Undefined Error.";
5361
return new EmbedResponse()
54-
.setTitle("Could not submit your Modrinth project to Mod Garden.")
62+
.setTitle("Could not submit your project to Mod Garden.")
5563
.setDescription(errorDescription)
5664
.setColor(0x5D3E40);
5765
} else if (stream.statusCode() < 200 || stream.statusCode() > 299) {
5866
String errorDescription = json.isJsonObject() && json.getAsJsonObject().has("description") ?
5967
json.getAsJsonObject().getAsJsonPrimitive("description").getAsString() :
6068
"Undefined Error.";
6169
return new EmbedResponse()
62-
.setTitle("Encountered an exception whilst attempting to submit your Modrinth project to Mod Garden.")
70+
.setTitle("Encountered an exception whilst attempting to submit your project to Mod Garden.")
6371
.setDescription(stream.statusCode() + ": " + errorDescription + "\nPlease report this to a team member.")
6472
.setColor(0xFF0000);
6573
}
@@ -73,7 +81,7 @@ public static Response handleSubmitModrinth(SlashCommandInteraction interaction)
7381
} catch (IOException | InterruptedException ex) {
7482
GardenBot.LOG.error("", ex);
7583
return new EmbedResponse()
76-
.setTitle("Encountered an exception whilst attempting to submit your Modrinth project to Mod Garden.")
84+
.setTitle("Encountered an exception whilst attempting to submit your project to Mod Garden.")
7785
.setDescription(ex.getMessage() + "\nPlease report this to a team member.")
7886
.setColor(0xFF0000);
7987
}
@@ -83,6 +91,9 @@ public static List<Command.Choice> getChoices(String focusedOption) {
8391
if (focusedOption.equals("slug")) {
8492
return Collections.emptyList();
8593
}
94+
if (focusedOption.equals("source")) {
95+
return List.of(new Command.Choice("Modrinth", "modrinth"));
96+
}
8697
try {
8798
var activeEventsResult = ModGardenAPIClient.get("events/active", HttpResponse.BodyHandlers.ofInputStream());
8899
if (activeEventsResult.statusCode() == 200) {
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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

Comments
 (0)