Skip to content

Commit

Permalink
merge: Add modal to set reason for denying application. (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmitch215 authored Nov 12, 2024
2 parents af5fab4 + 9aa203b commit d02fa78
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
19 changes: 16 additions & 3 deletions src/main/java/io/codemc/bot/listeners/ButtonListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@
import io.codemc.bot.utils.CommandUtil;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.text.TextInput;
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle;
import net.dv8tion.jda.api.interactions.modals.Modal;
import org.jetbrains.annotations.NotNull;

import java.util.List;
Expand Down Expand Up @@ -98,9 +103,17 @@ public void onButtonInteraction(@NotNull ButtonInteractionEvent event){
return;
}

event.deferReply(true).queue(
hook -> CmdApplication.handle(bot, hook, guild, event.getMessageIdLong(), "Reason not Provided", false)
);
TextInput reason = TextInput.create("reason", "Reason", TextInputStyle.PARAGRAPH)
.setPlaceholder("(Leave empty for no reason)")
.setMaxLength(MessageEmbed.VALUE_MAX_LENGTH)
.setRequired(false)
.build();

Modal modal = Modal.create("deny_application:" + event.getMessageId(), "Deny Application")
.addComponents(ActionRow.of(reason))
.build();

event.replyModal(modal).queue();
}
}

Expand Down
35 changes: 33 additions & 2 deletions src/main/java/io/codemc/bot/listeners/ModalListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

import io.codemc.api.jenkins.JenkinsAPI;
import io.codemc.bot.CodeMCBot;
import io.codemc.bot.commands.CmdApplication;
import io.codemc.bot.utils.CommandUtil;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.exceptions.ContextException;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
Expand Down Expand Up @@ -67,6 +67,11 @@ public void onModalInteraction(@NotNull ModalInteractionEvent event){
case "submit" -> event.deferReply(true).queue(hook -> {
String user = value(event, "user");

if(user == null || user.isEmpty()){
CommandUtil.EmbedReply.from(hook).error("The provided user was invalid.").send();
return;
}

if (!JenkinsAPI.getJenkinsUser(user).isEmpty()) {
CommandUtil.EmbedReply.from(hook)
.error("A Jenkins User named '" + user + "' already exists!")
Expand All @@ -77,7 +82,7 @@ public void onModalInteraction(@NotNull ModalInteractionEvent event){
String repo = value(event, "repo");
String description = value(event, "description");

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

case "deny_application" -> event.deferReply(true).queue(hook -> {
if(args.length == 1){
CommandUtil.EmbedReply.from(hook).error("Received invalid Deny Application modal!").send();
return;
}

long messageId;
try{
messageId = Long.parseLong(args[1]);
}catch(NumberFormatException ex){
messageId = -1L;
}

if(messageId == -1L){
CommandUtil.EmbedReply.from(hook).error("Received invalid message ID: " + args[1]).send();
return;
}

String reason = value(event, "reason");

if(reason == null || reason.isEmpty())
reason = "*No reason provided*";

CmdApplication.handle(this.bot, hook, guild, messageId, reason, false);
});

default -> CommandUtil.EmbedReply.from(event)
.error("Received Modal with unknown ID `" + event.getModalId() + "`.")
.send();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/codemc/bot/utils/CommandUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void send(){
modalEvent.replyEmbeds(builder.build()).setEphemeral(true).queue();
}else
if(type instanceof ButtonInteractionEvent buttonEvent){
buttonEvent.replyEmbeds(builder.build()).queue();
buttonEvent.replyEmbeds(builder.build()).setEphemeral(true).queue();
}else
if(type instanceof InteractionHook hook){
hook.editOriginal(EmbedBuilder.ZERO_WIDTH_SPACE).setEmbeds(builder.build()).queue();
Expand Down

0 comments on commit d02fa78

Please sign in to comment.