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 ;
68import net .dv8tion .jda .api .entities .channel .ChannelType ;
79import net .dv8tion .jda .api .entities .channel .concrete .ForumChannel ;
810import net .dv8tion .jda .api .entities .channel .concrete .ThreadChannel ;
11+ import net .dv8tion .jda .api .entities .channel .unions .ChannelUnion ;
12+ import net .dv8tion .jda .api .entities .channel .unions .MessageChannelUnion ;
13+ import net .dv8tion .jda .api .entities .emoji .Emoji ;
14+ import net .dv8tion .jda .api .events .channel .ChannelCreateEvent ;
915import net .dv8tion .jda .api .events .interaction .component .ButtonInteractionEvent ;
1016import net .dv8tion .jda .api .events .message .MessageReceivedEvent ;
1117import net .dv8tion .jda .api .hooks .ListenerAdapter ;
18+ import net .dv8tion .jda .api .interactions .callbacks .IReplyCallback ;
1219import net .dv8tion .jda .api .interactions .components .ActionComponent ;
20+ import net .dv8tion .jda .api .interactions .components .ActionRow ;
1321import net .dv8tion .jda .api .interactions .components .buttons .Button ;
1422import net .javadiscord .javabot .Bot ;
15- import net .javadiscord .javabot .data .config .GuildConfig ;
1623import net .javadiscord .javabot .data .config .guild .HelpConfig ;
24+ import net .javadiscord .javabot .data .config .guild .HelpForumConfig ;
1725import net .javadiscord .javabot .systems .help .HelpChannelManager ;
1826import net .javadiscord .javabot .systems .help .HelpExperienceService ;
1927import net .javadiscord .javabot .systems .help .model .HelpTransactionMessage ;
28+ import net .javadiscord .javabot .systems .user_preferences .UserPreferenceService ;
29+ import net .javadiscord .javabot .systems .user_preferences .model .Preference ;
30+ import net .javadiscord .javabot .systems .user_preferences .model .UserPreference ;
2031import net .javadiscord .javabot .util .ExceptionLogger ;
2132import net .javadiscord .javabot .util .Responses ;
2233import org .jetbrains .annotations .NotNull ;
@@ -39,19 +50,12 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
3950 if (event .getMessage ().getAuthor ().isSystem () || event .getMessage ().getAuthor ().isBot ()) {
4051 return ;
4152 }
42- // check for guild & channel type
43- if (! event . isFromGuild () || event .getChannelType () != ChannelType . GUILD_PUBLIC_THREAD ) {
53+ // check for forum post
54+ if (isInvalidForumPost ( event .getChannel ()) ) {
4455 return ;
4556 }
46- // get post & check parent channel
4757 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 ()) {
58+ if (isInvalidHelpForumChannel (post .getParentChannel ().asForumChannel ())) {
5559 return ;
5660 }
5761 // cache messages
@@ -63,20 +67,63 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
6367 HELP_POST_MESSAGES .put (post .getIdLong (), messages );
6468 }
6569
70+ @ Override
71+ public void onChannelCreate (@ NotNull ChannelCreateEvent event ) {
72+ if (event .getGuild () == null || isInvalidForumPost (event .getChannel ())) {
73+ return ;
74+ }
75+ HelpForumConfig config = Bot .getConfig ().get (event .getGuild ()).getHelpForumConfig ();
76+ ThreadChannel post = event .getChannel ().asThreadChannel ();
77+ if (isInvalidHelpForumChannel (post .getParentChannel ().asForumChannel ())) {
78+ return ;
79+ }
80+ // send post buttons
81+ post .sendMessageComponents (ActionRow .of (
82+ Button .primary (ComponentIdBuilder .build (ForumHelpManager .HELP_CLOSE_IDENTIFIER , post .getIdLong ()), "Close Post" ),
83+ Button .secondary (ComponentIdBuilder .build (ForumHelpManager .HELP_GUIDELINES_IDENTIFIER ), "Help Guidelines" ).withEmoji (Emoji .fromUnicode ("📚" ))
84+ )).queue (success -> {
85+ // send /close reminder (if enabled)
86+ UserPreferenceService service = new UserPreferenceService (Bot .getDataSource ());
87+ UserPreference preference = service .getOrCreate (post .getOwnerIdLong (), Preference .FORUM_CLOSE_REMINDER );
88+ if (Boolean .parseBoolean (preference .getState ())) {
89+ post .sendMessageFormat (config .getCloseReminderText (), UserSnowflake .fromId (post .getOwnerIdLong ()).getAsMention ()).queue ();
90+ }
91+ });
92+ }
93+
6694 @ Override
6795 public void handleButton (@ NotNull ButtonInteractionEvent event , @ NotNull Button button ) {
6896 String [] id = ComponentIdBuilder .split (event .getComponentId ());
69- if (event .getChannelType () != ChannelType .GUILD_PUBLIC_THREAD
70- || event .getChannel ().asThreadChannel ().getParentChannel ().getType () != ChannelType .FORUM ) {
97+ if (isInvalidForumPost (event .getChannel ()) ||
98+ isInvalidHelpForumChannel (event .getChannel ().asThreadChannel ().getParentChannel ().asForumChannel ())
99+ ) {
71100 Responses .error (event , "This button may only be used inside help forum threads." ).queue ();
72101 return ;
73102 }
74- ForumHelpManager manager = new ForumHelpManager (event .getChannel ().asThreadChannel ());
103+ ThreadChannel post = event .getChannel ().asThreadChannel ();
104+ ForumHelpManager manager = new ForumHelpManager (post );
75105 switch (id [0 ]) {
76106 case ForumHelpManager .HELP_THANKS_IDENTIFIER -> handleHelpThanksInteraction (event , manager , id );
107+ case ForumHelpManager .HELP_GUIDELINES_IDENTIFIER -> handleReplyGuidelines (event , post .getParentChannel ().asForumChannel ());
108+ case ForumHelpManager .HELP_CLOSE_IDENTIFIER -> handlePostClose (event , manager );
77109 }
78110 }
79111
112+ private boolean isInvalidForumPost (@ NotNull MessageChannelUnion union ) {
113+ return union .getType () != ChannelType .GUILD_PUBLIC_THREAD ||
114+ union .asThreadChannel ().getParentChannel ().getType () != ChannelType .FORUM ;
115+ }
116+
117+ private boolean isInvalidForumPost (@ NotNull ChannelUnion union ) {
118+ return union .getType () != ChannelType .GUILD_PUBLIC_THREAD ||
119+ union .asThreadChannel ().getParentChannel ().getType () != ChannelType .FORUM ;
120+ }
121+
122+ private boolean isInvalidHelpForumChannel (@ NotNull ForumChannel forum ) {
123+ HelpForumConfig config = Bot .getConfig ().get (forum .getGuild ()).getHelpForumConfig ();
124+ return config .getHelpForumChannelId () != forum .getIdLong ();
125+ }
126+
80127 private void handleHelpThanksInteraction (@ NotNull ButtonInteractionEvent event , @ NotNull ForumHelpManager manager , String @ NotNull [] id ) {
81128 ThreadChannel post = manager .postThread ();
82129 HelpConfig config = Bot .getConfig ().get (event .getGuild ()).getHelpConfig ();
@@ -108,4 +155,21 @@ private void handleHelpThanksInteraction(@NotNull ButtonInteractionEvent event,
108155 default -> event .editButton (event .getButton ().asDisabled ()).queue ();
109156 }
110157 }
158+
159+ private void handleReplyGuidelines (@ NotNull IReplyCallback callback , @ NotNull ForumChannel channel ) {
160+ callback .replyEmbeds (new EmbedBuilder ()
161+ .setTitle ("Help Guidelines" )
162+ .setDescription (channel .getTopic ())
163+ .build ()
164+ ).setEphemeral (true )
165+ .queue ();
166+ }
167+
168+ private void handlePostClose (ButtonInteractionEvent event , @ NotNull ForumHelpManager manager ) {
169+ if (manager .isForumEligibleToBeUnreserved (event )) {
170+ manager .close (event , event .getUser ().getIdLong () == manager .postThread ().getOwnerIdLong (), null );
171+ } else {
172+ Responses .warning (event , "Could not close this post" , "You're not allowed to close this post." ).queue ();
173+ }
174+ }
111175}
0 commit comments