|
5 | 5 | import java.util.List; |
6 | 6 | import java.util.Optional; |
7 | 7 | import java.util.Set; |
8 | | - |
9 | 8 | import net.discordjug.javabot.systems.staff_commands.forms.FormInteractionManager; |
10 | 9 | import net.discordjug.javabot.systems.staff_commands.forms.dao.FormsRepository; |
11 | 10 | import net.discordjug.javabot.systems.staff_commands.forms.model.FormData; |
|
34 | 33 | */ |
35 | 34 | public class AttachFormSubcommand extends Subcommand implements AutoCompletable { |
36 | 35 |
|
37 | | - private final FormsRepository formsRepo; |
38 | | - |
39 | | - /** |
40 | | - * The main constructor of this subcommand. |
41 | | - * |
42 | | - * @param formsRepo the forms repository |
43 | | - */ |
44 | | - public AttachFormSubcommand(FormsRepository formsRepo) { |
45 | | - this.formsRepo = formsRepo; |
46 | | - setCommandData(new SubcommandData("attach", "Attach a form to a message").addOptions( |
47 | | - new OptionData(OptionType.INTEGER, "form-id", "ID of the form to attach", true, true), |
48 | | - new OptionData(OptionType.STRING, "message-id", "ID of the message to attach the form to", true), |
49 | | - new OptionData(OptionType.CHANNEL, "channel", |
50 | | - "Channel of the message. Required if the message is in a different channel"), |
51 | | - new OptionData(OptionType.STRING, "button-label", "Label of the submit button. Default is \"Submit\""), |
52 | | - new OptionData(OptionType.STRING, "button-style", "Submit button style. Defaults to primary", false, |
53 | | - true))); |
54 | | - } |
55 | | - |
56 | | - @Override |
57 | | - public void execute(SlashCommandInteractionEvent event) { |
58 | | - |
59 | | - event.deferReply().setEphemeral(true).queue(); |
60 | | - |
61 | | - Optional<FormData> formOpt = formsRepo.getForm(event.getOption("form-id", OptionMapping::getAsLong)); |
62 | | - if (formOpt.isEmpty()) { |
63 | | - event.getHook().sendMessage("A form with this ID was not found.").queue(); |
64 | | - return; |
65 | | - } |
66 | | - FormData form = formOpt.get(); |
67 | | - |
68 | | - if (form.isAttached()) { |
69 | | - event.getHook() |
70 | | - .sendMessage("The form seems to already be attached to a message. Detach it before continuing.") |
71 | | - .queue(); |
72 | | - return; |
73 | | - } |
74 | | - |
75 | | - if (form.fields().isEmpty()) { |
76 | | - event.getHook().sendMessage("You can't attach a form with no fields.").queue(); |
77 | | - return; |
78 | | - } |
79 | | - |
80 | | - String messageId = event.getOption("message-id", OptionMapping::getAsString); |
81 | | - GuildChannel channel = event.getOption("channel", event.getChannel().asGuildMessageChannel(), |
82 | | - OptionMapping::getAsChannel); |
83 | | - |
84 | | - if (channel == null) { |
85 | | - event.getHook().sendMessage("A channel with this ID was not found.").setEphemeral(true).queue(); |
86 | | - return; |
87 | | - } |
88 | | - |
89 | | - if (!(channel instanceof MessageChannel msgChannel)) { |
90 | | - event.getHook().sendMessage("You must specify a message channel").setEphemeral(true).queue(); |
91 | | - return; |
92 | | - } |
93 | | - |
94 | | - String buttonLabel = event.getOption("button-label", "Submit", OptionMapping::getAsString); |
95 | | - net.dv8tion.jda.api.components.buttons.ButtonStyle style = event.getOption("button-style", ButtonStyle.PRIMARY, |
96 | | - t -> { |
97 | | - try { |
98 | | - return ButtonStyle.valueOf(t.getAsString().toUpperCase()); |
99 | | - } catch (IllegalArgumentException e) { |
100 | | - return ButtonStyle.PRIMARY; |
101 | | - } |
102 | | - }); |
103 | | - |
104 | | - msgChannel.retrieveMessageById(messageId).queue(message -> { |
105 | | - attachFormToMessage(message, buttonLabel, style, form); |
106 | | - formsRepo.attachForm(form, msgChannel, message); |
107 | | - event.getHook() |
108 | | - .sendMessage("Successfully attached the form to the [message](" + message.getJumpUrl() + ")!") |
109 | | - .queue(); |
110 | | - }, _ -> event.getHook().sendMessage("A message with this ID was not found").queue()); |
111 | | - } |
112 | | - |
113 | | - @Override |
114 | | - public void handleAutoComplete(CommandAutoCompleteInteractionEvent event, AutoCompleteQuery target) { |
115 | | - switch (target.getName()) { |
116 | | - case "form-id" -> event.replyChoices( |
117 | | - formsRepo.getAllForms().stream().map(form -> new Choice(form.toString(), form.id())).toList()) |
118 | | - .queue(); |
119 | | - case "button-style" -> event.replyChoices( |
120 | | - Set.of(ButtonStyle.DANGER, ButtonStyle.PRIMARY, ButtonStyle.SECONDARY, ButtonStyle.SUCCESS).stream() |
121 | | - .map(style -> new Choice(style.name(), style.name())).toList()) |
122 | | - .queue(); |
123 | | - default -> {} |
124 | | - } |
125 | | - } |
126 | | - |
127 | | - private static void attachFormToMessage(Message message, String buttonLabel, ButtonStyle style, FormData form) { |
128 | | - List<ActionRow> rows = new ArrayList<>( |
129 | | - message.getComponents().stream().map(MessageTopLevelComponentUnion::asActionRow).toList()); |
130 | | - |
131 | | - Button button = Button.of(style, ComponentIdBuilder.build(FormInteractionManager.FORM_COMPONENT_ID, form.id()), |
132 | | - buttonLabel); |
133 | | - |
134 | | - if (form.closed() || form.hasExpired()) { |
135 | | - button = button.asDisabled(); |
136 | | - } |
137 | | - |
138 | | - if (rows.isEmpty() || rows.get(rows.size() - 1).getActionComponents().size() >= 5) { |
139 | | - rows.add(ActionRow.of(button)); |
140 | | - } else { |
141 | | - ActionRow lastRow = rows.get(rows.size() - 1); |
142 | | - List<ActionRowChildComponent> components = new ArrayList<>(lastRow.getComponents().size() + 1); |
143 | | - Collections.copy(components, lastRow.getComponents()); |
144 | | - components.set(components.size() - 1, button); |
145 | | - rows.set(rows.size() - 1, ActionRow.of(components)); |
146 | | - } |
147 | | - |
148 | | - message.editMessageComponents(rows.toArray(new ActionRow[0])).queue(); |
149 | | - } |
| 36 | + private final FormsRepository formsRepo; |
| 37 | + |
| 38 | + /** |
| 39 | + * The main constructor of this subcommand. |
| 40 | + * |
| 41 | + * @param formsRepo the forms repository |
| 42 | + */ |
| 43 | + public AttachFormSubcommand(FormsRepository formsRepo) { |
| 44 | + this.formsRepo = formsRepo; |
| 45 | + setCommandData(new SubcommandData("attach", "Attach a form to a message").addOptions( |
| 46 | + new OptionData(OptionType.INTEGER, "form-id", "ID of the form to attach", true, true), |
| 47 | + new OptionData(OptionType.STRING, "message-id", "ID of the message to attach the form to", true), |
| 48 | + new OptionData(OptionType.CHANNEL, "channel", |
| 49 | + "Channel of the message. Required if the message is in a different channel"), |
| 50 | + new OptionData(OptionType.STRING, "button-label", "Label of the submit button. Default is \"Submit\""), |
| 51 | + new OptionData(OptionType.STRING, "button-style", "Submit button style. Defaults to primary", false, |
| 52 | + true))); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void execute(SlashCommandInteractionEvent event) { |
| 57 | + |
| 58 | + event.deferReply().setEphemeral(true).queue(); |
| 59 | + |
| 60 | + Optional<FormData> formOpt = formsRepo.getForm(event.getOption("form-id", OptionMapping::getAsLong)); |
| 61 | + if (formOpt.isEmpty()) { |
| 62 | + event.getHook().sendMessage("A form with this ID was not found.").queue(); |
| 63 | + return; |
| 64 | + } |
| 65 | + FormData form = formOpt.get(); |
| 66 | + |
| 67 | + if (form.isAttached()) { |
| 68 | + event.getHook() |
| 69 | + .sendMessage("The form seems to already be attached to a message. Detach it before continuing.") |
| 70 | + .queue(); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if (form.fields().isEmpty()) { |
| 75 | + event.getHook().sendMessage("You can't attach a form with no fields.").queue(); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + String messageId = event.getOption("message-id", OptionMapping::getAsString); |
| 80 | + GuildChannel channel = event.getOption("channel", event.getChannel().asGuildMessageChannel(), |
| 81 | + OptionMapping::getAsChannel); |
| 82 | + |
| 83 | + if (channel == null) { |
| 84 | + event.getHook().sendMessage("A channel with this ID was not found.").setEphemeral(true).queue(); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + if (!(channel instanceof MessageChannel msgChannel)) { |
| 89 | + event.getHook().sendMessage("You must specify a message channel").setEphemeral(true).queue(); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + String buttonLabel = event.getOption("button-label", "Submit", OptionMapping::getAsString); |
| 94 | + net.dv8tion.jda.api.components.buttons.ButtonStyle style = event.getOption("button-style", ButtonStyle.PRIMARY, |
| 95 | + t -> { |
| 96 | + try { |
| 97 | + return ButtonStyle.valueOf(t.getAsString().toUpperCase()); |
| 98 | + } catch (IllegalArgumentException e) { |
| 99 | + return ButtonStyle.PRIMARY; |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + msgChannel.retrieveMessageById(messageId).queue(message -> { |
| 104 | + attachFormToMessage(message, buttonLabel, style, form); |
| 105 | + formsRepo.attachForm(form, msgChannel, message); |
| 106 | + event.getHook() |
| 107 | + .sendMessage("Successfully attached the form to the [message](" + message.getJumpUrl() + ")!") |
| 108 | + .queue(); |
| 109 | + }, _ -> event.getHook().sendMessage("A message with this ID was not found").queue()); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void handleAutoComplete(CommandAutoCompleteInteractionEvent event, AutoCompleteQuery target) { |
| 114 | + switch (target.getName()) { |
| 115 | + case "form-id" -> event.replyChoices( |
| 116 | + formsRepo.getAllForms().stream().map(form -> new Choice(form.toString(), form.id())).toList()) |
| 117 | + .queue(); |
| 118 | + case "button-style" -> event.replyChoices( |
| 119 | + Set.of(ButtonStyle.DANGER, ButtonStyle.PRIMARY, ButtonStyle.SECONDARY, ButtonStyle.SUCCESS).stream() |
| 120 | + .map(style -> new Choice(style.name(), style.name())).toList()) |
| 121 | + .queue(); |
| 122 | + default -> {} |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private static void attachFormToMessage(Message message, String buttonLabel, ButtonStyle style, FormData form) { |
| 127 | + List<ActionRow> rows = new ArrayList<>( |
| 128 | + message.getComponents().stream().map(MessageTopLevelComponentUnion::asActionRow).toList()); |
| 129 | + |
| 130 | + Button button = Button.of(style, ComponentIdBuilder.build(FormInteractionManager.FORM_COMPONENT_ID, form.id()), |
| 131 | + buttonLabel); |
| 132 | + |
| 133 | + if (form.closed() || form.hasExpired()) { |
| 134 | + button = button.asDisabled(); |
| 135 | + } |
| 136 | + |
| 137 | + if (rows.isEmpty() || rows.get(rows.size() - 1).getActionComponents().size() >= 5) { |
| 138 | + rows.add(ActionRow.of(button)); |
| 139 | + } else { |
| 140 | + ActionRow lastRow = rows.get(rows.size() - 1); |
| 141 | + List<ActionRowChildComponent> components = new ArrayList<>(lastRow.getComponents().size() + 1); |
| 142 | + Collections.copy(components, lastRow.getComponents()); |
| 143 | + components.set(components.size() - 1, button); |
| 144 | + rows.set(rows.size() - 1, ActionRow.of(components)); |
| 145 | + } |
| 146 | + |
| 147 | + message.editMessageComponents(rows.toArray(new ActionRow[0])).queue(); |
| 148 | + } |
150 | 149 |
|
151 | 150 | } |
0 commit comments