44import com .dynxsty .dih4jda .interactions .components .ButtonHandler ;
55
66import lombok .RequiredArgsConstructor ;
7+ import net .dv8tion .jda .api .EmbedBuilder ;
78import net .dv8tion .jda .api .entities .Message ;
9+ import net .dv8tion .jda .api .entities .UserSnowflake ;
10+ import net .dv8tion .jda .api .entities .channel .Channel ;
811import net .dv8tion .jda .api .entities .channel .ChannelType ;
912import net .dv8tion .jda .api .entities .channel .concrete .ForumChannel ;
1013import net .dv8tion .jda .api .entities .channel .concrete .ThreadChannel ;
14+ import net .dv8tion .jda .api .events .channel .ChannelCreateEvent ;
1115import net .dv8tion .jda .api .events .interaction .component .ButtonInteractionEvent ;
1216import net .dv8tion .jda .api .events .message .MessageReceivedEvent ;
1317import net .dv8tion .jda .api .hooks .ListenerAdapter ;
18+ import net .dv8tion .jda .api .interactions .callbacks .IReplyCallback ;
1419import net .dv8tion .jda .api .interactions .components .ActionComponent ;
20+ import net .dv8tion .jda .api .interactions .components .ActionRow ;
1521import net .dv8tion .jda .api .interactions .components .buttons .Button ;
1622import net .javadiscord .javabot .data .config .BotConfig ;
1723import net .javadiscord .javabot .data .config .GuildConfig ;
1824import net .javadiscord .javabot .data .config .guild .HelpConfig ;
1925import net .javadiscord .javabot .data .h2db .DbActions ;
26+ import net .javadiscord .javabot .Bot ;
27+ import net .javadiscord .javabot .data .config .guild .HelpConfig ;
28+ import net .javadiscord .javabot .data .config .guild .HelpForumConfig ;
2029import net .javadiscord .javabot .systems .help .HelpChannelManager ;
2130import net .javadiscord .javabot .systems .help .HelpExperienceService ;
2231import net .javadiscord .javabot .systems .help .dao .HelpAccountRepository ;
2332import net .javadiscord .javabot .systems .help .dao .HelpTransactionRepository ;
2433import net .javadiscord .javabot .systems .help .model .HelpTransactionMessage ;
34+ import net .javadiscord .javabot .systems .user_preferences .UserPreferenceService ;
35+ import net .javadiscord .javabot .systems .user_preferences .model .Preference ;
36+ import net .javadiscord .javabot .systems .user_preferences .model .UserPreference ;
2537import net .javadiscord .javabot .util .ExceptionLogger ;
2638import net .javadiscord .javabot .util .Responses ;
2739import org .jetbrains .annotations .NotNull ;
@@ -53,19 +65,12 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
5365 if (event .getMessage ().getAuthor ().isSystem () || event .getMessage ().getAuthor ().isBot ()) {
5466 return ;
5567 }
56- // check for guild & channel type
57- if (! event . isFromGuild () || event .getChannelType () != ChannelType . GUILD_PUBLIC_THREAD ) {
68+ // check for forum post
69+ if (isInvalidForumPost ( event .getChannel ()) ) {
5870 return ;
5971 }
60- // get post & check parent channel
6172 ThreadChannel post = event .getChannel ().asThreadChannel ();
62- if (post .getParentChannel ().getType () != ChannelType .FORUM ) {
63- return ;
64- }
65- ForumChannel forum = post .getParentChannel ().asForumChannel ();
66- GuildConfig config = botConfig .get (event .getGuild ());
67- // check for channel id
68- if (forum .getIdLong () != config .getHelpForumConfig ().getHelpForumChannelId ()) {
73+ if (isInvalidHelpForumChannel (post .getParentChannel ().asForumChannel ())) {
6974 return ;
7075 }
7176 // cache messages
@@ -77,20 +82,59 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
7782 HELP_POST_MESSAGES .put (post .getIdLong (), messages );
7883 }
7984
85+ @ Override
86+ public void onChannelCreate (@ NotNull ChannelCreateEvent event ) {
87+ if (event .getGuild () == null || isInvalidForumPost (event .getChannel ())) {
88+ return ;
89+ }
90+ HelpForumConfig config = Bot .getConfig ().get (event .getGuild ()).getHelpForumConfig ();
91+ ThreadChannel post = event .getChannel ().asThreadChannel ();
92+ if (isInvalidHelpForumChannel (post .getParentChannel ().asForumChannel ())) {
93+ return ;
94+ }
95+ // send post buttons
96+ post .sendMessageComponents (ActionRow .of (
97+ Button .primary (ComponentIdBuilder .build (ForumHelpManager .HELP_CLOSE_IDENTIFIER , post .getIdLong ()), "Close Post" ),
98+ Button .secondary (ComponentIdBuilder .build (ForumHelpManager .HELP_GUIDELINES_IDENTIFIER ), "View Help Guidelines" )
99+ )).queue (success -> {
100+ // send /close reminder (if enabled)
101+ UserPreferenceService service = new UserPreferenceService (Bot .getDataSource ());
102+ UserPreference preference = service .getOrCreate (post .getOwnerIdLong (), Preference .FORUM_CLOSE_REMINDER );
103+ if (Boolean .parseBoolean (preference .getState ())) {
104+ post .sendMessageFormat (config .getCloseReminderText (), UserSnowflake .fromId (post .getOwnerIdLong ()).getAsMention ()).queue ();
105+ }
106+ });
107+ }
108+
80109 @ Override
81110 public void handleButton (@ NotNull ButtonInteractionEvent event , @ NotNull Button button ) {
82111 String [] id = ComponentIdBuilder .split (event .getComponentId ());
83- if (event .getChannelType () != ChannelType .GUILD_PUBLIC_THREAD
84- || event .getChannel ().asThreadChannel ().getParentChannel ().getType () != ChannelType .FORUM ) {
112+ if (isInvalidForumPost (event .getChannel ()) ||
113+ isInvalidHelpForumChannel (event .getChannel ().asThreadChannel ().getParentChannel ().asForumChannel ())
114+ ) {
85115 Responses .error (event , "This button may only be used inside help forum threads." ).queue ();
86116 return ;
87117 }
88118 ForumHelpManager manager = new ForumHelpManager (event .getChannel ().asThreadChannel (), dbActions , botConfig , dataSource , helpAccountRepository , helpTransactionRepository );
119+ ThreadChannel post = event .getChannel ().asThreadChannel ();
120+ ForumHelpManager manager = new ForumHelpManager (post );
89121 switch (id [0 ]) {
90122 case ForumHelpManager .HELP_THANKS_IDENTIFIER -> handleHelpThanksInteraction (event , manager , id );
123+ case ForumHelpManager .HELP_GUIDELINES_IDENTIFIER -> handleReplyGuidelines (event , post .getParentChannel ().asForumChannel ());
124+ case ForumHelpManager .HELP_CLOSE_IDENTIFIER -> handlePostClose (event , manager );
91125 }
92126 }
93127
128+ private boolean isInvalidForumPost (@ NotNull Channel channel ) {
129+ return channel .getType () != ChannelType .GUILD_PUBLIC_THREAD ||
130+ ((ThreadChannel ) channel ).getParentChannel ().getType () != ChannelType .FORUM ;
131+ }
132+
133+ private boolean isInvalidHelpForumChannel (@ NotNull ForumChannel forum ) {
134+ HelpForumConfig config = Bot .getConfig ().get (forum .getGuild ()).getHelpForumConfig ();
135+ return config .getHelpForumChannelId () != forum .getIdLong ();
136+ }
137+
94138 private void handleHelpThanksInteraction (@ NotNull ButtonInteractionEvent event , @ NotNull ForumHelpManager manager , String @ NotNull [] id ) {
95139 ThreadChannel post = manager .getPostThread ();
96140 HelpConfig config = botConfig .get (event .getGuild ()).getHelpConfig ();
@@ -122,4 +166,21 @@ private void handleHelpThanksInteraction(@NotNull ButtonInteractionEvent event,
122166 default -> event .editButton (event .getButton ().asDisabled ()).queue ();
123167 }
124168 }
169+
170+ private void handleReplyGuidelines (@ NotNull IReplyCallback callback , @ NotNull ForumChannel channel ) {
171+ callback .replyEmbeds (new EmbedBuilder ()
172+ .setTitle ("Help Guidelines" )
173+ .setDescription (channel .getTopic ())
174+ .build ()
175+ ).setEphemeral (true )
176+ .queue ();
177+ }
178+
179+ private void handlePostClose (ButtonInteractionEvent event , @ NotNull ForumHelpManager manager ) {
180+ if (manager .isForumEligibleToBeUnreserved (event )) {
181+ manager .close (event , event .getUser ().getIdLong () == manager .postThread ().getOwnerIdLong (), null );
182+ } else {
183+ Responses .warning (event , "Could not close this post" , "You're not allowed to close this post." ).queue ();
184+ }
185+ }
125186}
0 commit comments