44import com .dynxsty .dih4jda .interactions .components .ButtonHandler ;
55
66import lombok .RequiredArgsConstructor ;
7- import net .dv8tion .jda .api .EmbedBuilder ;
87import net .dv8tion .jda .api .entities .Message ;
9- import net .dv8tion .jda .api .entities .UserSnowflake ;
10- import net .dv8tion .jda .api .entities .channel .Channel ;
118import net .dv8tion .jda .api .entities .channel .ChannelType ;
129import net .dv8tion .jda .api .entities .channel .concrete .ForumChannel ;
1310import net .dv8tion .jda .api .entities .channel .concrete .ThreadChannel ;
14- import net .dv8tion .jda .api .events .channel .ChannelCreateEvent ;
1511import net .dv8tion .jda .api .events .interaction .component .ButtonInteractionEvent ;
1612import net .dv8tion .jda .api .events .message .MessageReceivedEvent ;
1713import net .dv8tion .jda .api .hooks .ListenerAdapter ;
18- import net .dv8tion .jda .api .interactions .callbacks .IReplyCallback ;
1914import net .dv8tion .jda .api .interactions .components .ActionComponent ;
20- import net .dv8tion .jda .api .interactions .components .ActionRow ;
2115import net .dv8tion .jda .api .interactions .components .buttons .Button ;
2216import net .javadiscord .javabot .data .config .BotConfig ;
2317import net .javadiscord .javabot .data .config .GuildConfig ;
2418import net .javadiscord .javabot .data .config .guild .HelpConfig ;
2519import 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 ;
2920import net .javadiscord .javabot .systems .help .HelpChannelManager ;
3021import net .javadiscord .javabot .systems .help .HelpExperienceService ;
3122import net .javadiscord .javabot .systems .help .dao .HelpAccountRepository ;
3223import net .javadiscord .javabot .systems .help .dao .HelpTransactionRepository ;
3324import 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 ;
3725import net .javadiscord .javabot .util .ExceptionLogger ;
3826import net .javadiscord .javabot .util .Responses ;
3927import org .jetbrains .annotations .NotNull ;
@@ -65,12 +53,19 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
6553 if (event .getMessage ().getAuthor ().isSystem () || event .getMessage ().getAuthor ().isBot ()) {
6654 return ;
6755 }
68- // check for forum post
69- if (isInvalidForumPost ( event .getChannel ()) ) {
56+ // check for guild & channel type
57+ if (! event . isFromGuild () || event .getChannelType () != ChannelType . GUILD_PUBLIC_THREAD ) {
7058 return ;
7159 }
60+ // get post & check parent channel
7261 ThreadChannel post = event .getChannel ().asThreadChannel ();
73- if (isInvalidHelpForumChannel (post .getParentChannel ().asForumChannel ())) {
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 ()) {
7469 return ;
7570 }
7671 // cache messages
@@ -82,59 +77,20 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
8277 HELP_POST_MESSAGES .put (post .getIdLong (), messages );
8378 }
8479
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-
10980 @ Override
11081 public void handleButton (@ NotNull ButtonInteractionEvent event , @ NotNull Button button ) {
11182 String [] id = ComponentIdBuilder .split (event .getComponentId ());
112- if (isInvalidForumPost (event .getChannel ()) ||
113- isInvalidHelpForumChannel (event .getChannel ().asThreadChannel ().getParentChannel ().asForumChannel ())
114- ) {
83+ if (event .getChannelType () != ChannelType .GUILD_PUBLIC_THREAD
84+ || event .getChannel ().asThreadChannel ().getParentChannel ().getType () != ChannelType .FORUM ) {
11585 Responses .error (event , "This button may only be used inside help forum threads." ).queue ();
11686 return ;
11787 }
11888 ForumHelpManager manager = new ForumHelpManager (event .getChannel ().asThreadChannel (), dbActions , botConfig , dataSource , helpAccountRepository , helpTransactionRepository );
119- ThreadChannel post = event .getChannel ().asThreadChannel ();
120- ForumHelpManager manager = new ForumHelpManager (post );
12189 switch (id [0 ]) {
12290 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 );
12591 }
12692 }
12793
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-
13894 private void handleHelpThanksInteraction (@ NotNull ButtonInteractionEvent event , @ NotNull ForumHelpManager manager , String @ NotNull [] id ) {
13995 ThreadChannel post = manager .getPostThread ();
14096 HelpConfig config = botConfig .get (event .getGuild ()).getHelpConfig ();
@@ -166,21 +122,4 @@ private void handleHelpThanksInteraction(@NotNull ButtonInteractionEvent event,
166122 default -> event .editButton (event .getButton ().asDisabled ()).queue ();
167123 }
168124 }
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- }
186125}
0 commit comments