22
33import lombok .extern .slf4j .Slf4j ;
44import net .dv8tion .jda .api .entities .Role ;
5+ import net .dv8tion .jda .api .entities .ThreadChannel ;
56import net .dv8tion .jda .api .events .interaction .ButtonClickEvent ;
67import net .dv8tion .jda .api .events .interaction .SelectionMenuEvent ;
78import net .dv8tion .jda .api .hooks .ListenerAdapter ;
89import net .javadiscord .javabot .Bot ;
10+ import net .javadiscord .javabot .command .Responses ;
911import net .javadiscord .javabot .systems .help .HelpChannelInteractionManager ;
1012import net .javadiscord .javabot .systems .moderation .ModerationService ;
11- import net .javadiscord .javabot .systems .qotw .SubmissionManager ;
13+ import net .javadiscord .javabot .systems .qotw .submissions .SubmissionControlsManager ;
14+ import net .javadiscord .javabot .systems .qotw .submissions .SubmissionManager ;
15+ import net .javadiscord .javabot .systems .qotw .submissions .dao .QOTWSubmissionRepository ;
16+
17+ import java .sql .SQLException ;
1218
1319/**
1420 * Listens for Interaction Events and handles them.
1521 */
1622@ Slf4j
1723public class InteractionListener extends ListenerAdapter {
24+ // TODO: add Context-Menu Commands (once they're available in JDA)
1825
1926 @ Override
2027 public void onSelectionMenu (SelectionMenuEvent event ) {
2128 if (event .getUser ().isBot ()) return ;
2229 event .deferEdit ().queue ();
30+ String [] id = event .getComponentId ().split (":" );
2331 var config = Bot .config .get (event .getGuild ());
24- if (event .getComponentId ().equals ("submission-controls:decline" )) {
25- new SubmissionManager (config .getQotw ()).handleSubmissionControlSelectInteraction (event .getComponentId (), event ).queue ();
32+ switch (id [0 ]) {
33+ case "submission-controls-select" -> {
34+ var thread = (ThreadChannel ) event .getGuildChannel ();
35+ try (var con = Bot .dataSource .getConnection ()) {
36+ var repo = new QOTWSubmissionRepository (con );
37+ var submissionOptional = repo .getSubmissionByThreadId (thread .getIdLong ());
38+ submissionOptional .ifPresent (qotwSubmission -> new SubmissionControlsManager (event .getGuild (), config .getQotw (), qotwSubmission ).handleSelectMenus (id , event ));
39+ } catch (SQLException e ) {
40+ e .printStackTrace ();
41+ }
42+ }
43+ default -> Responses .error (event .getHook (), "Unknown Interaction" ).queue ();
2644 }
2745 }
2846
29- // TODO: add Context-Menu Commands (once they're available in JDA)
30- // TODO: Clean up button ids. "qotw-submission" & "qotw-submission-delete" is just a temporary solution.
3147 @ Override
3248 public void onButtonClick (ButtonClickEvent event ) {
3349 if (event .getUser ().isBot ()) return ;
@@ -38,27 +54,35 @@ public void onButtonClick(ButtonClickEvent event) {
3854 case "qotw-submission" -> {
3955 var manager = new SubmissionManager (config .getQotw ());
4056 if (!id [1 ].isEmpty () && id [1 ].equals ("delete" )) manager .handleThreadDeletion (event );
41- else manager .handleSubmission (event , Long .parseLong (id [1 ])).queue ();
57+ else manager .handleSubmission (event , Integer .parseInt (id [1 ])).queue ();
58+ }
59+ case "submission-controls" -> {
60+ var thread = (ThreadChannel ) event .getGuildChannel ();
61+ try (var con = Bot .dataSource .getConnection ()) {
62+ var repo = new QOTWSubmissionRepository (con );
63+ var submissionOptional = repo .getSubmissionByThreadId (thread .getIdLong ());
64+ submissionOptional .ifPresent (qotwSubmission -> new SubmissionControlsManager (event .getGuild (), config .getQotw (), qotwSubmission ).handleButtons (id , event ));
65+ } catch (SQLException e ) {
66+ e .printStackTrace ();
67+ }
4268 }
43- case "submission-controls" -> new SubmissionManager (config .getQotw ()).handleSubmissionControlButtonInteractions (id , event ).queue ();
44- // Deprecated: Remove this next week
45- case "qotw-submission-delete" -> new SubmissionManager (config .getQotw ()).handleThreadDeletion (event );
46- case "reaction-role" -> this .handleReactionRoles (event );
69+ case "reaction-role" -> this .handleReactionRoles (id , event );
4770 case "help-channel" -> new HelpChannelInteractionManager ().handleHelpChannel (event , id [1 ], id [2 ]);
4871 case "help-thank" -> new HelpChannelInteractionManager ().handleHelpThank (event , id [1 ], id [2 ]);
49- case "utils" -> this .handleUtils (event );
72+ case "utils" -> this .handleUtils (id , event );
73+ default -> Responses .error (event .getHook (), "Unknown Interaction" ).queue ();
5074 }
5175 }
5276
5377 /**
5478 * Some utility methods for interactions.
5579 * + May be useful for Context Menu Interactions.
5680 *
81+ * @param id The button's id, split by ":".
5782 * @param event The {@link ButtonClickEvent} that is fired upon use.
5883 */
59- private void handleUtils (ButtonClickEvent event ) {
84+ private void handleUtils (String [] id , ButtonClickEvent event ) {
6085 var service = new ModerationService (event .getInteraction ());
61- String [] id = event .getComponentId ().split (":" );
6286 switch (id [1 ]) {
6387 case "delete" -> event .getHook ().deleteOriginal ().queue ();
6488 case "kick" -> service .kick (
@@ -81,8 +105,7 @@ private void handleUtils(ButtonClickEvent event) {
81105 }
82106 }
83107
84- private void handleReactionRoles (ButtonClickEvent event ) {
85- String [] id = event .getComponentId ().split (":" );
108+ private void handleReactionRoles (String [] id , ButtonClickEvent event ) {
86109 String roleID = id [1 ];
87110 boolean permanent = Boolean .parseBoolean (id [2 ]);
88111
0 commit comments