22
33import com .dynxsty .dih4jda .interactions .ComponentIdBuilder ;
44import com .dynxsty .dih4jda .interactions .components .ButtonHandler ;
5+ import net .dv8tion .jda .api .EmbedBuilder ;
56import net .dv8tion .jda .api .entities .Message ;
7+ import net .dv8tion .jda .api .entities .UserSnowflake ;
8+ import net .dv8tion .jda .api .entities .channel .Channel ;
69import net .dv8tion .jda .api .entities .channel .ChannelType ;
710import net .dv8tion .jda .api .entities .channel .concrete .ForumChannel ;
811import net .dv8tion .jda .api .entities .channel .concrete .ThreadChannel ;
12+ import net .dv8tion .jda .api .events .channel .ChannelCreateEvent ;
913import net .dv8tion .jda .api .events .interaction .component .ButtonInteractionEvent ;
1014import net .dv8tion .jda .api .events .message .MessageReceivedEvent ;
1115import net .dv8tion .jda .api .hooks .ListenerAdapter ;
16+ import net .dv8tion .jda .api .interactions .callbacks .IReplyCallback ;
1217import net .dv8tion .jda .api .interactions .components .ActionComponent ;
18+ import net .dv8tion .jda .api .interactions .components .ActionRow ;
1319import net .dv8tion .jda .api .interactions .components .buttons .Button ;
1420import net .javadiscord .javabot .Bot ;
15- import net .javadiscord .javabot .data .config .GuildConfig ;
1621import net .javadiscord .javabot .data .config .guild .HelpConfig ;
22+ import net .javadiscord .javabot .data .config .guild .HelpForumConfig ;
1723import net .javadiscord .javabot .systems .help .HelpChannelManager ;
1824import net .javadiscord .javabot .systems .help .HelpExperienceService ;
1925import net .javadiscord .javabot .systems .help .model .HelpTransactionMessage ;
26+ import net .javadiscord .javabot .systems .user_preferences .UserPreferenceService ;
27+ import net .javadiscord .javabot .systems .user_preferences .model .Preference ;
28+ import net .javadiscord .javabot .systems .user_preferences .model .UserPreference ;
2029import net .javadiscord .javabot .util .ExceptionLogger ;
2130import net .javadiscord .javabot .util .Responses ;
2231import org .jetbrains .annotations .NotNull ;
@@ -39,19 +48,12 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
3948 if (event .getMessage ().getAuthor ().isSystem () || event .getMessage ().getAuthor ().isBot ()) {
4049 return ;
4150 }
42- // check for guild & channel type
43- if (! event . isFromGuild () || event .getChannelType () != ChannelType . GUILD_PUBLIC_THREAD ) {
51+ // check for forum post
52+ if (isInvalidForumPost ( event .getChannel ()) ) {
4453 return ;
4554 }
46- // get post & check parent channel
4755 ThreadChannel post = event .getChannel ().asThreadChannel ();
48- if (post .getParentChannel ().getType () != ChannelType .FORUM ) {
49- return ;
50- }
51- ForumChannel forum = post .getParentChannel ().asForumChannel ();
52- GuildConfig config = Bot .getConfig ().get (event .getGuild ());
53- // check for channel id
54- if (forum .getIdLong () != config .getHelpForumConfig ().getHelpForumChannelId ()) {
56+ if (isInvalidHelpForumChannel (post .getParentChannel ().asForumChannel ())) {
5557 return ;
5658 }
5759 // cache messages
@@ -63,20 +65,58 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
6365 HELP_POST_MESSAGES .put (post .getIdLong (), messages );
6466 }
6567
68+ @ Override
69+ public void onChannelCreate (@ NotNull ChannelCreateEvent event ) {
70+ if (event .getGuild () == null || isInvalidForumPost (event .getChannel ())) {
71+ return ;
72+ }
73+ HelpForumConfig config = Bot .getConfig ().get (event .getGuild ()).getHelpForumConfig ();
74+ ThreadChannel post = event .getChannel ().asThreadChannel ();
75+ if (isInvalidHelpForumChannel (post .getParentChannel ().asForumChannel ())) {
76+ return ;
77+ }
78+ // send post buttons
79+ post .sendMessageComponents (ActionRow .of (
80+ Button .primary (ComponentIdBuilder .build (ForumHelpManager .HELP_CLOSE_IDENTIFIER , post .getIdLong ()), "Close Post" ),
81+ Button .secondary (ComponentIdBuilder .build (ForumHelpManager .HELP_GUIDELINES_IDENTIFIER ), "View Help Guidelines" )
82+ )).queue (success -> {
83+ // send /close reminder (if enabled)
84+ UserPreferenceService service = new UserPreferenceService (Bot .getDataSource ());
85+ UserPreference preference = service .getOrCreate (post .getOwnerIdLong (), Preference .FORUM_CLOSE_REMINDER );
86+ if (Boolean .parseBoolean (preference .getState ())) {
87+ post .sendMessageFormat (config .getCloseReminderText (), UserSnowflake .fromId (post .getOwnerIdLong ()).getAsMention ()).queue ();
88+ }
89+ });
90+ }
91+
6692 @ Override
6793 public void handleButton (@ NotNull ButtonInteractionEvent event , @ NotNull Button button ) {
6894 String [] id = ComponentIdBuilder .split (event .getComponentId ());
69- if (event .getChannelType () != ChannelType .GUILD_PUBLIC_THREAD
70- || event .getChannel ().asThreadChannel ().getParentChannel ().getType () != ChannelType .FORUM ) {
95+ if (isInvalidForumPost (event .getChannel ()) ||
96+ isInvalidHelpForumChannel (event .getChannel ().asThreadChannel ().getParentChannel ().asForumChannel ())
97+ ) {
7198 Responses .error (event , "This button may only be used inside help forum threads." ).queue ();
7299 return ;
73100 }
74- ForumHelpManager manager = new ForumHelpManager (event .getChannel ().asThreadChannel ());
101+ ThreadChannel post = event .getChannel ().asThreadChannel ();
102+ ForumHelpManager manager = new ForumHelpManager (post );
75103 switch (id [0 ]) {
76104 case ForumHelpManager .HELP_THANKS_IDENTIFIER -> handleHelpThanksInteraction (event , manager , id );
105+ case ForumHelpManager .HELP_GUIDELINES_IDENTIFIER -> handleReplyGuidelines (event , post .getParentChannel ().asForumChannel ());
106+ case ForumHelpManager .HELP_CLOSE_IDENTIFIER -> handlePostClose (event , manager );
77107 }
78108 }
79109
110+ private boolean isInvalidForumPost (@ NotNull Channel channel ) {
111+ return channel .getType () != ChannelType .GUILD_PUBLIC_THREAD ||
112+ ((ThreadChannel ) channel ).getParentChannel ().getType () != ChannelType .FORUM ;
113+ }
114+
115+ private boolean isInvalidHelpForumChannel (@ NotNull ForumChannel forum ) {
116+ HelpForumConfig config = Bot .getConfig ().get (forum .getGuild ()).getHelpForumConfig ();
117+ return config .getHelpForumChannelId () != forum .getIdLong ();
118+ }
119+
80120 private void handleHelpThanksInteraction (@ NotNull ButtonInteractionEvent event , @ NotNull ForumHelpManager manager , String @ NotNull [] id ) {
81121 ThreadChannel post = manager .postThread ();
82122 HelpConfig config = Bot .getConfig ().get (event .getGuild ()).getHelpConfig ();
@@ -108,4 +148,21 @@ private void handleHelpThanksInteraction(@NotNull ButtonInteractionEvent event,
108148 default -> event .editButton (event .getButton ().asDisabled ()).queue ();
109149 }
110150 }
151+
152+ private void handleReplyGuidelines (@ NotNull IReplyCallback callback , @ NotNull ForumChannel channel ) {
153+ callback .replyEmbeds (new EmbedBuilder ()
154+ .setTitle ("Help Guidelines" )
155+ .setDescription (channel .getTopic ())
156+ .build ()
157+ ).setEphemeral (true )
158+ .queue ();
159+ }
160+
161+ private void handlePostClose (ButtonInteractionEvent event , @ NotNull ForumHelpManager manager ) {
162+ if (manager .isForumEligibleToBeUnreserved (event )) {
163+ manager .close (event , event .getUser ().getIdLong () == manager .postThread ().getOwnerIdLong (), null );
164+ } else {
165+ Responses .warning (event , "Could not close this post" , "You're not allowed to close this post." ).queue ();
166+ }
167+ }
111168}
0 commit comments