Skip to content

Commit af48aab

Browse files
feat: Add /team commands for handling users within a Mod Garden project.
1 parent 4c1d845 commit af48aab

File tree

11 files changed

+1042
-10
lines changed

11 files changed

+1042
-10
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = 1.1.1
1+
version = 1.2.0

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ public class GardenBot {
2626
public static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient();
2727

2828
private static final int DATABASE_SCHEMA_VERSION = 1;
29+
public static final String SAFE_URL_REGEX = "[a-zA-Z0-9!@$()`.+,_\"-]+";
2930

3031
public static JDA jda;
3132

3233
public static void main(String[] args) {
3334
if ("development".equals(System.getenv("env")))
3435
((ch.qos.logback.classic.Logger)LOG).setLevel(Level.DEBUG);
3536

36-
jda = JDABuilder.create(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_MESSAGES, GatewayIntent.MESSAGE_CONTENT)
37+
jda = JDABuilder.create(GatewayIntent.GUILD_MEMBERS, GatewayIntent.DIRECT_MESSAGES, GatewayIntent.GUILD_MESSAGES, GatewayIntent.MESSAGE_CONTENT)
3738
.disableCache(CacheFlag.ACTIVITY, CacheFlag.VOICE_STATE, CacheFlag.EMOJI, CacheFlag.STICKER, CacheFlag.CLIENT_STATUS, CacheFlag.ONLINE_STATUS, CacheFlag.SCHEDULED_EVENTS)
3839
.setToken(DOTENV.get("TOKEN"))
3940
.addEventListeners(new GardenBotEvents())

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

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public static void registerAll() {
2525
ButtonDispatcher.register("unlinkModrinth", GardenBotButtonHandlers::unlinkModrinth);
2626
ButtonDispatcher.register("linkMinecraft", new ModalResponse(GardenBotModals.LINK_MINECRAFT));
2727
ButtonDispatcher.register("unlinkMinecraft?%s", GardenBotButtonHandlers::unlinkMinecraft);
28+
29+
ButtonDispatcher.register("acceptInvite?%s", GardenBotButtonHandlers::acceptInvite);
30+
ButtonDispatcher.register("declineInvite?%s", GardenBotButtonHandlers::declineInvite);
2831
}
2932

3033
public static Response unlinkModrinth(ButtonInteraction interaction) {
@@ -47,13 +50,18 @@ public static Response unlinkModrinth(ButtonInteraction interaction) {
4750
json.getAsJsonObject().getAsJsonPrimitive("description").getAsString() :
4851
"Undefined Error.";
4952
return new EmbedResponse()
50-
.setTitle("Encountered an exception whilst attempting to unlink your Modrinth from your Mod Garden account.")
53+
.setTitle("Encountered an exception whilst attempting to unlink your Modrinth account from your Mod Garden account.")
5154
.setDescription(stream.statusCode() + ": " + errorDescription + "\nPlease report this to a team member.")
5255
.setColor(0xFF0000)
5356
.markEphemeral();
5457
}
5558
} catch (IOException | InterruptedException ex) {
5659
GardenBot.LOG.error("", ex);
60+
return new EmbedResponse()
61+
.setTitle("Encountered an exception whilst attempting to unlink your Modrinth account from your Mod Garden account.")
62+
.setDescription(ex.getMessage() + "\nPlease report this to a team member.")
63+
.setColor(0xFF0000)
64+
.markEphemeral();
5765
}
5866

5967
return new EmbedResponse()
@@ -91,11 +99,16 @@ public static Response unlinkMinecraft(ButtonInteraction interaction) {
9199
}
92100
if (stream.statusCode() == 200) {
93101
return new MessageResponse()
94-
.setMessage("You already .")
102+
.setMessage("You have already linked this Minecraft account to your Mod Garden account.")
95103
.markEphemeral();
96104
}
97105
} catch (IOException | InterruptedException ex) {
98106
GardenBot.LOG.error("", ex);
107+
return new EmbedResponse()
108+
.setTitle("Encountered an exception whilst attempting to unlink your Minecraft account from your Mod Garden account.")
109+
.setDescription(ex.getMessage() + "\nPlease report this to a team member.")
110+
.setColor(0xFF0000)
111+
.markEphemeral();
99112
}
100113

101114
String username = MinecraftAccountUtil.getMinecraftUsernameFromUuid(uuid);
@@ -107,6 +120,75 @@ public static Response unlinkMinecraft(ButtonInteraction interaction) {
107120
.setTitle("Successfully unlinked your Minecraft account (" + username + ") from Mod Garden!")
108121
.setColor(0xA9FFA7)
109122
.markEphemeral();
123+
}
124+
125+
public static Response acceptInvite(ButtonInteraction interaction) {
126+
String inviteCode = interaction.arguments()[0];
127+
128+
JsonObject publisher = new JsonObject();
129+
publisher.addProperty("invite_code", inviteCode);
130+
131+
try {
132+
HttpResponse<Void> acceptResult = ModGardenAPIClient.post(
133+
"discord/project/user/accept",
134+
HttpRequest.BodyPublishers.ofString(publisher.toString()),
135+
HttpResponse.BodyHandlers.discarding(),
136+
"Content-Type", "application/json"
137+
);
138+
if (acceptResult.statusCode() != 201) {
139+
return new EmbedResponse()
140+
.setTitle("Could not accept invite to project.")
141+
.setDescription("This invite is invalid or has expired.")
142+
.setColor(0x5D3E40)
143+
.markEphemeral();
144+
}
145+
} catch (IOException | InterruptedException ex) {
146+
GardenBot.LOG.error("", ex);
147+
return new EmbedResponse()
148+
.setTitle("Encountered an exception whilst attempting to accept invite.")
149+
.setDescription(ex.getMessage() + "\nPlease report this to a team member.")
150+
.setColor(0xFF0000)
151+
.markEphemeral();
152+
}
153+
154+
return new EmbedResponse()
155+
.setTitle("Successfully accepted invite to project!")
156+
.setColor(0xA9FFA7)
157+
.markEphemeral();
158+
}
159+
160+
public static Response declineInvite(ButtonInteraction interaction) {
161+
String inviteCode = interaction.arguments()[0];
110162

163+
JsonObject publisher = new JsonObject();
164+
publisher.addProperty("invite_code", inviteCode);
165+
166+
try {
167+
HttpResponse<Void> declineResult = ModGardenAPIClient.post(
168+
"discord/project/user/decline",
169+
HttpRequest.BodyPublishers.ofString(publisher.toString()),
170+
HttpResponse.BodyHandlers.discarding(),
171+
"Content-Type", "application/json"
172+
);
173+
if (declineResult.statusCode() != 201) {
174+
return new EmbedResponse()
175+
.setTitle("Could not decline invite to project.")
176+
.setDescription("This invite is invalid or has expired.")
177+
.setColor(0x5D3E40)
178+
.markEphemeral();
179+
}
180+
} catch (IOException | InterruptedException ex) {
181+
GardenBot.LOG.error("", ex);
182+
return new EmbedResponse()
183+
.setTitle("Encountered an exception whilst attempting to decline invite.")
184+
.setDescription(ex.getMessage() + "\nPlease report this to a team member.")
185+
.setColor(0xFF0000)
186+
.markEphemeral();
187+
}
188+
189+
return new EmbedResponse()
190+
.setTitle("Successfully declined invite to project!")
191+
.setColor(0xA9FFA7)
192+
.markEphemeral();
111193
}
112194
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import net.modgarden.gardenbot.commands.account.*;
55
import net.modgarden.gardenbot.commands.event.*;
66
import net.modgarden.gardenbot.commands.image.UploadHandler;
7+
import net.modgarden.gardenbot.commands.team.InviteHandler;
8+
import net.modgarden.gardenbot.commands.team.KickHandler;
9+
import net.modgarden.gardenbot.commands.team.LeaveHandler;
710
import net.modgarden.gardenbot.interaction.command.SlashCommand;
811
import net.modgarden.gardenbot.interaction.command.SlashCommandDispatcher;
912
import net.modgarden.gardenbot.interaction.command.SlashCommandOption;
@@ -112,6 +115,33 @@ public static void registerAll() {
112115
new SlashCommandOption(OptionType.STRING, "version", "The version of the project to update to.", false, true)
113116
)));
114117

118+
SlashCommandDispatcher.register(new SlashCommand("team", "Actions relating to your Mod Garden projects.",
119+
new SlashCommand.SubCommand(
120+
"invite",
121+
"Invites a user to your Mod Garden project.",
122+
InviteHandler::handleInvite,
123+
InviteHandler::getChoices,
124+
new SlashCommandOption(OptionType.STRING, "project", "The project to invite the user to.", true, true),
125+
new SlashCommandOption(OptionType.STRING, "role", "The role to provide the user.", true, true),
126+
new SlashCommandOption(OptionType.USER, "user", "The user to invite.", true, false)
127+
),
128+
new SlashCommand.SubCommand(
129+
"leave",
130+
"Leaves a Mod Garden project.",
131+
LeaveHandler::handleLeave,
132+
LeaveHandler::getChoices,
133+
new SlashCommandOption(OptionType.STRING, "project", "The project to leave.", true, true)
134+
),
135+
new SlashCommand.SubCommand(
136+
"kick",
137+
"Kicks a user from a Mod Garden project.",
138+
KickHandler::handleKick,
139+
KickHandler::getChoices,
140+
new SlashCommandOption(OptionType.STRING, "project", "The project to kick the user from.", true, true),
141+
new SlashCommandOption(OptionType.USER, "user", "The user to kick.", true, false)
142+
)
143+
));
144+
115145
SlashCommandDispatcher.register(new SlashCommand("image", "Actions relating to images for Mod Garden's showcase worlds.",
116146
new SlashCommand.SubCommand(
117147
"upload",

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,29 @@ public void onGuildMemberRemove(@NotNull GuildMemberRemoveEvent event) {
103103

104104
@Override
105105
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
106-
if (GardenBot.DOTENV.get("MODERATION_LOGS_CHANNEL_ID") == null || !event.getGuild().getId().equals(GardenBot.DOTENV.get("GUILD_ID")) || event.getAuthor().isSystem() || event.getAuthor().isBot() || event.getMessage().getContentRaw().isEmpty() || event.isWebhookMessage())
106+
if (GardenBot.DOTENV.get("MODERATION_LOGS_CHANNEL_ID") == null ||
107+
!event.isFromGuild() ||
108+
!event.getGuild().getId().equals(GardenBot.DOTENV.get("GUILD_ID")) ||
109+
event.getAuthor().isSystem() ||
110+
event.getAuthor().isBot() ||
111+
event.getMessage().getContentRaw().isEmpty() ||
112+
event.getMessage().isWebhookMessage())
107113
return;
108114

109115
MessageCacheUtil.cacheMessage(event.getAuthor().getId(), event.getMessageId(), event.getMessage().getContentRaw());
110116
}
111117

112118
@Override
113119
public void onMessageUpdate(@NotNull MessageUpdateEvent event) {
114-
if (GardenBot.DOTENV.get("MODERATION_LOGS_CHANNEL_ID") == null || !event.getGuild().getId().equals(GardenBot.DOTENV.get("GUILD_ID")) || event.getAuthor().isSystem() || event.getAuthor().isBot() || event.getMessage().getContentRaw().isEmpty() || event.getMessage().isWebhookMessage())
120+
if (
121+
GardenBot.DOTENV.get("MODERATION_LOGS_CHANNEL_ID") == null ||
122+
!event.isFromGuild() ||
123+
!event.getGuild().getId().equals(GardenBot.DOTENV.get("GUILD_ID")) ||
124+
event.getAuthor().isSystem() ||
125+
event.getAuthor().isBot() ||
126+
event.getMessage().getContentRaw().isEmpty() ||
127+
event.getMessage().isWebhookMessage()
128+
)
115129
return;
116130

117131
String moderationLogsChannelId = GardenBot.DOTENV.get("MODERATION_LOGS_CHANNEL_ID");

0 commit comments

Comments
 (0)