Skip to content

Commit d02fa78

Browse files
authored
merge: Add modal to set reason for denying application. (#18)
2 parents af5fab4 + 9aa203b commit d02fa78

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

src/main/java/io/codemc/bot/listeners/ButtonListener.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@
2323
import io.codemc.bot.utils.CommandUtil;
2424
import net.dv8tion.jda.api.entities.Guild;
2525
import net.dv8tion.jda.api.entities.Member;
26+
import net.dv8tion.jda.api.entities.MessageEmbed;
2627
import net.dv8tion.jda.api.entities.Role;
2728
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
2829
import net.dv8tion.jda.api.hooks.ListenerAdapter;
30+
import net.dv8tion.jda.api.interactions.components.ActionRow;
31+
import net.dv8tion.jda.api.interactions.components.text.TextInput;
32+
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle;
33+
import net.dv8tion.jda.api.interactions.modals.Modal;
2934
import org.jetbrains.annotations.NotNull;
3035

3136
import java.util.List;
@@ -98,9 +103,17 @@ public void onButtonInteraction(@NotNull ButtonInteractionEvent event){
98103
return;
99104
}
100105

101-
event.deferReply(true).queue(
102-
hook -> CmdApplication.handle(bot, hook, guild, event.getMessageIdLong(), "Reason not Provided", false)
103-
);
106+
TextInput reason = TextInput.create("reason", "Reason", TextInputStyle.PARAGRAPH)
107+
.setPlaceholder("(Leave empty for no reason)")
108+
.setMaxLength(MessageEmbed.VALUE_MAX_LENGTH)
109+
.setRequired(false)
110+
.build();
111+
112+
Modal modal = Modal.create("deny_application:" + event.getMessageId(), "Deny Application")
113+
.addComponents(ActionRow.of(reason))
114+
.build();
115+
116+
event.replyModal(modal).queue();
104117
}
105118
}
106119

src/main/java/io/codemc/bot/listeners/ModalListener.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020

2121
import io.codemc.api.jenkins.JenkinsAPI;
2222
import io.codemc.bot.CodeMCBot;
23+
import io.codemc.bot.commands.CmdApplication;
2324
import io.codemc.bot.utils.CommandUtil;
2425
import net.dv8tion.jda.api.entities.Guild;
2526
import net.dv8tion.jda.api.entities.Message;
2627
import net.dv8tion.jda.api.entities.MessageEmbed;
2728
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
2829
import net.dv8tion.jda.api.entities.emoji.Emoji;
2930
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
30-
import net.dv8tion.jda.api.exceptions.ContextException;
3131
import net.dv8tion.jda.api.hooks.ListenerAdapter;
3232
import net.dv8tion.jda.api.interactions.InteractionHook;
3333
import net.dv8tion.jda.api.interactions.components.buttons.Button;
@@ -67,6 +67,11 @@ public void onModalInteraction(@NotNull ModalInteractionEvent event){
6767
case "submit" -> event.deferReply(true).queue(hook -> {
6868
String user = value(event, "user");
6969

70+
if(user == null || user.isEmpty()){
71+
CommandUtil.EmbedReply.from(hook).error("The provided user was invalid.").send();
72+
return;
73+
}
74+
7075
if (!JenkinsAPI.getJenkinsUser(user).isEmpty()) {
7176
CommandUtil.EmbedReply.from(hook)
7277
.error("A Jenkins User named '" + user + "' already exists!")
@@ -77,7 +82,7 @@ public void onModalInteraction(@NotNull ModalInteractionEvent event){
7782
String repo = value(event, "repo");
7883
String description = value(event, "description");
7984

80-
if(user == null || user.isEmpty() || repo == null || repo.isEmpty() || description == null || description.isEmpty()){
85+
if(repo == null || repo.isEmpty() || description == null || description.isEmpty()){
8186
CommandUtil.EmbedReply.from(hook).error(
8287
"The option User, Repo and/or Description was not set properly!")
8388
.send();
@@ -225,6 +230,32 @@ public void onModalInteraction(@NotNull ModalInteractionEvent event){
225230
}
226231
});
227232

233+
case "deny_application" -> event.deferReply(true).queue(hook -> {
234+
if(args.length == 1){
235+
CommandUtil.EmbedReply.from(hook).error("Received invalid Deny Application modal!").send();
236+
return;
237+
}
238+
239+
long messageId;
240+
try{
241+
messageId = Long.parseLong(args[1]);
242+
}catch(NumberFormatException ex){
243+
messageId = -1L;
244+
}
245+
246+
if(messageId == -1L){
247+
CommandUtil.EmbedReply.from(hook).error("Received invalid message ID: " + args[1]).send();
248+
return;
249+
}
250+
251+
String reason = value(event, "reason");
252+
253+
if(reason == null || reason.isEmpty())
254+
reason = "*No reason provided*";
255+
256+
CmdApplication.handle(this.bot, hook, guild, messageId, reason, false);
257+
});
258+
228259
default -> CommandUtil.EmbedReply.from(event)
229260
.error("Received Modal with unknown ID `" + event.getModalId() + "`.")
230261
.send();

src/main/java/io/codemc/bot/utils/CommandUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void send(){
9292
modalEvent.replyEmbeds(builder.build()).setEphemeral(true).queue();
9393
}else
9494
if(type instanceof ButtonInteractionEvent buttonEvent){
95-
buttonEvent.replyEmbeds(builder.build()).queue();
95+
buttonEvent.replyEmbeds(builder.build()).setEphemeral(true).queue();
9696
}else
9797
if(type instanceof InteractionHook hook){
9898
hook.editOriginal(EmbedBuilder.ZERO_WIDTH_SPACE).setEmbeds(builder.build()).queue();

0 commit comments

Comments
 (0)