Added a way to make modals and added a suggestion modal
This commit is contained in:
parent
d6b631e232
commit
6a05dec5b9
|
|
@ -2,13 +2,14 @@ package com.alttd.commandManager;
|
|||
|
||||
import com.alttd.commandManager.commands.AddCommand.CommandManage;
|
||||
import com.alttd.commandManager.commands.CommandHelp;
|
||||
import com.alttd.commandManager.commands.CommandSuggestion;
|
||||
import com.alttd.commandManager.commands.PollCommand.CommandPoll;
|
||||
import com.alttd.database.Database;
|
||||
import com.alttd.modalManager.ModalManager;
|
||||
import com.alttd.util.Logger;
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
|
|
@ -27,14 +28,15 @@ public class CommandManager extends ListenerAdapter {
|
|||
private final List<DiscordCommand> commands;
|
||||
private final HashMap<String, List<ScopeInfo>> commandList = new HashMap<>();
|
||||
|
||||
public CommandManager(JDA jda) {
|
||||
public CommandManager(JDA jda, ModalManager modalManager) {
|
||||
commandList.put("manage", new ArrayList<>(List.of(new ScopeInfo(CommandScope.GLOBAL, 0))));
|
||||
loadCommands();
|
||||
Logger.info("Loading commands...");
|
||||
commands = List.of(
|
||||
new CommandManage(jda, this),
|
||||
new CommandHelp(jda, this),
|
||||
new CommandPoll(jda, this));
|
||||
new CommandPoll(jda, this),
|
||||
new CommandSuggestion(jda, modalManager, this));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -0,0 +1,61 @@
|
|||
package com.alttd.commandManager.commands;
|
||||
|
||||
import com.alttd.commandManager.CommandManager;
|
||||
import com.alttd.commandManager.DiscordCommand;
|
||||
import com.alttd.config.MessagesConfig;
|
||||
import com.alttd.modalManager.ModalManager;
|
||||
import com.alttd.util.Util;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.Commands;
|
||||
import net.dv8tion.jda.api.interactions.components.Modal;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
public class CommandSuggestion extends DiscordCommand {
|
||||
|
||||
private final CommandManager commandManager;
|
||||
private final CommandData commandData;
|
||||
private final ModalManager modalManager;
|
||||
|
||||
public CommandSuggestion(JDA jda, ModalManager modalManager, CommandManager commandManager) {
|
||||
this.commandManager = commandManager;
|
||||
this.modalManager = modalManager;
|
||||
|
||||
commandData = Commands.slash(getName(), "Open suggestion form.");
|
||||
Util.registerCommand(commandManager, jda, commandData, getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "suggestion";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(SlashCommandInteractionEvent event) {
|
||||
Modal modal = modalManager.getModalFor("suggestion");
|
||||
if (modal == null) {
|
||||
event.replyEmbeds(Util.genericErrorEmbed("Error",
|
||||
"Unable to find suggestion modal, please report this issue to Teri")).queue();
|
||||
return;
|
||||
}
|
||||
event.replyModal(modal).queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void suggest(CommandAutoCompleteInteractionEvent event) {
|
||||
event.replyChoices(Collections.emptyList()).queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return MessagesConfig.HELP_SUGGESTION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandData getCommandData() {
|
||||
return commandData;
|
||||
}
|
||||
}
|
||||
|
|
@ -15,9 +15,11 @@ public class MessagesConfig extends AbstractConfig {
|
|||
|
||||
|
||||
public static String HELP_HELP = "`/help`: Shows help menu";
|
||||
public static String HELP_SUGGESTION = "`/suggestion`: Opens suggestion form";
|
||||
public static String HELP_MESSAGE_TEMPLATE = "<commands>";
|
||||
private static void loadHelp() {
|
||||
HELP_HELP = messagesConfig.getString("help.help", HELP_HELP);
|
||||
HELP_SUGGESTION = messagesConfig.getString("help.suggestion", HELP_SUGGESTION);
|
||||
HELP_MESSAGE_TEMPLATE = messagesConfig.getString("help.message-template", HELP_MESSAGE_TEMPLATE);
|
||||
}
|
||||
private static void loadPollHelp() {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.alttd.commandManager.listeners;
|
||||
package com.alttd.listeners;
|
||||
|
||||
import com.alttd.commandManager.CommandManager;
|
||||
import com.alttd.modalManager.ModalManager;
|
||||
import com.alttd.util.Logger;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.events.ReadyEvent;
|
||||
|
|
@ -18,7 +19,7 @@ public class JDAListener extends ListenerAdapter {
|
|||
@Override
|
||||
public void onReady(@NotNull ReadyEvent event) {
|
||||
Logger.info("JDA ready to register commands.");
|
||||
jda.addEventListener(new CommandManager(jda));
|
||||
jda.addEventListener(new CommandManager(jda, new ModalManager()));
|
||||
}
|
||||
|
||||
}
|
||||
14
src/main/java/com/alttd/modalManager/DiscordModal.java
Normal file
14
src/main/java/com/alttd/modalManager/DiscordModal.java
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package com.alttd.modalManager;
|
||||
|
||||
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
|
||||
import net.dv8tion.jda.api.interactions.components.Modal;
|
||||
|
||||
public abstract class DiscordModal {
|
||||
|
||||
public abstract String getModalId();
|
||||
|
||||
public abstract void execute(ModalInteractionEvent event);
|
||||
|
||||
public abstract Modal getModal();
|
||||
|
||||
}
|
||||
51
src/main/java/com/alttd/modalManager/ModalManager.java
Normal file
51
src/main/java/com/alttd/modalManager/ModalManager.java
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package com.alttd.modalManager;
|
||||
|
||||
import com.alttd.modalManager.modals.ModalSuggestion;
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import net.dv8tion.jda.api.interactions.components.Modal;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ModalManager extends ListenerAdapter {
|
||||
|
||||
private final List<DiscordModal> modals;
|
||||
|
||||
public ModalManager() {
|
||||
modals = List.of(
|
||||
new ModalSuggestion());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onModalInteraction(@NotNull ModalInteractionEvent event) {
|
||||
String modalId = event.getModalId();
|
||||
Optional<DiscordModal> first = modals.stream()
|
||||
.filter(discordModal -> discordModal.getModalId().equalsIgnoreCase(modalId))
|
||||
.findFirst();
|
||||
if (first.isEmpty()) {
|
||||
event.replyEmbeds(new EmbedBuilder()
|
||||
.setTitle("Invalid command")
|
||||
.setDescription("Unable to process modal with id: [" + modalId + "], please report this issue to a Teri")
|
||||
.setColor(Color.RED)
|
||||
.build())
|
||||
.setEphemeral(true)
|
||||
.queue();
|
||||
return;
|
||||
}
|
||||
first.get().execute(event);
|
||||
}
|
||||
|
||||
public @Nullable Modal getModalFor(String modalId) {
|
||||
Optional<DiscordModal> first = modals.stream()
|
||||
.filter(discordModal -> discordModal.getModalId().equalsIgnoreCase(modalId))
|
||||
.findFirst();
|
||||
if (first.isEmpty())
|
||||
return null;
|
||||
return first.get().getModal();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package com.alttd.modalManager.modals;
|
||||
|
||||
import com.alttd.modalManager.DiscordModal;
|
||||
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
|
||||
import net.dv8tion.jda.api.interactions.components.ActionRow;
|
||||
import net.dv8tion.jda.api.interactions.components.Modal;
|
||||
import net.dv8tion.jda.api.interactions.components.text.TextInput;
|
||||
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle;
|
||||
|
||||
public class ModalSuggestion extends DiscordModal {
|
||||
@Override
|
||||
public String getModalId() {
|
||||
return "suggestion";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(ModalInteractionEvent event) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Modal getModal() {
|
||||
TextInput title = TextInput.create("title", "Title", TextInputStyle.SHORT)
|
||||
.setPlaceholder("You suggestion in one sentence")
|
||||
.setRequiredRange(10, 100)
|
||||
.setRequired(true)
|
||||
.build();
|
||||
|
||||
TextInput body = TextInput.create("body", "Body", TextInputStyle.PARAGRAPH)
|
||||
.setPlaceholder("Your concerns go here")
|
||||
.setRequiredRange(30, 1024)
|
||||
.setRequired(true)
|
||||
.build();
|
||||
|
||||
return Modal.create(getModalId(), "Suggestion Form")
|
||||
.addActionRows(ActionRow.of(title), ActionRow.of(body))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user