33import com .dynxsty .dih4jda .interactions .commands .SlashCommand ;
44import net .dv8tion .jda .api .EmbedBuilder ;
55import net .dv8tion .jda .api .entities .*;
6+ import net .dv8tion .jda .api .entities .channel .ChannelType ;
7+ import net .dv8tion .jda .api .entities .channel .concrete .ThreadChannel ;
68import net .dv8tion .jda .api .events .interaction .command .SlashCommandInteractionEvent ;
79import net .dv8tion .jda .api .interactions .commands .build .SubcommandData ;
810import net .javadiscord .javabot .Bot ;
2325 * helpers.
2426 */
2527public class HelpPingSubcommand extends SlashCommand .Subcommand {
26- private static final String WRONG_CHANNEL_MSG = "This command can only be used in **reserved help channels**." ;
28+ private static final String WRONG_CHANNEL_MSG = "This command can only be used in **reserved help channels** OR **help forum posts.** ." ;
2729 private static final long CACHE_CLEANUP_DELAY = 60L ;
2830
2931 private final Map <Long , Pair <Long , Guild >> lastPingTimes ;
@@ -45,6 +47,43 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
4547 return ;
4648 }
4749 GuildConfig config = Bot .getConfig ().get (guild );
50+ if (event .getChannelType () == ChannelType .TEXT ) {
51+ handleTextBasedHelpPing (event , config );
52+ } else if (event .getChannelType () == ChannelType .GUILD_PUBLIC_THREAD ) {
53+ handleForumBasedHelpPing (event , config , event .getChannel ().asThreadChannel ());
54+ }
55+ }
56+
57+ private void handleForumBasedHelpPing (@ NotNull SlashCommandInteractionEvent event , @ NotNull GuildConfig config , @ NotNull ThreadChannel post ) {
58+ if (post .getParentChannel ().getType () != ChannelType .FORUM ||
59+ post .getParentChannel ().getIdLong () != config .getHelpForumConfig ().getHelpForumChannelId ()
60+ ) {
61+ Responses .error (event , WRONG_CHANNEL_MSG ).queue ();
62+ return ;
63+ }
64+ Member member = event .getMember ();
65+ if (member == null ) {
66+ Responses .warning (event , "No member information was available for this event." ).queue ();
67+ return ;
68+ }
69+ if (isHelpPingForbiddenForMember (post .getOwnerIdLong (), member , config )) {
70+ Responses .warning (event , "Sorry, but only the person who reserved this channel, or staff and helpers, may use this command." ).queue ();
71+ return ;
72+ }
73+ if (isHelpPingTimeoutElapsed (member .getIdLong (), config )) {
74+ lastPingTimes .put (event .getMember ().getIdLong (), new Pair <>(System .currentTimeMillis (), config .getGuild ()));
75+ Role role = config .getHelpConfig ().getHelpPingRole ();
76+ event .getChannel ().sendMessage (role .getAsMention ())
77+ .setAllowedMentions (EnumSet .of (Message .MentionType .ROLE ))
78+ .setEmbeds (buildAuthorEmbed (event .getUser ()))
79+ .queue ();
80+ event .replyFormat ("Successfully pinged " + role .getAsMention ()).setEphemeral (true ).queue ();
81+ } else {
82+ Responses .warning (event , "Sorry, but you can only use this command occasionally. Please try again later." ).queue ();
83+ }
84+ }
85+
86+ private void handleTextBasedHelpPing (@ NotNull SlashCommandInteractionEvent event , @ NotNull GuildConfig config ) {
4887 HelpChannelManager channelManager = new HelpChannelManager (config .getHelpConfig ());
4988 if (channelManager .isReserved (event .getChannel ().asTextChannel ())) {
5089 Optional <ChannelReservation > optionalReservation = channelManager .getReservationForChannel (event .getChannel ().getIdLong ());
@@ -63,7 +102,7 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
63102 return ;
64103 }
65104 if (isHelpPingTimeoutElapsed (member .getIdLong (), config )) {
66- lastPingTimes .put (event .getMember ().getIdLong (), new Pair <>(System .currentTimeMillis (), guild ));
105+ lastPingTimes .put (event .getMember ().getIdLong (), new Pair <>(System .currentTimeMillis (), config . getGuild () ));
67106 Role role = channelManager .getConfig ().getHelpPingRole ();
68107 event .getChannel ().sendMessage (role .getAsMention ())
69108 .setAllowedMentions (EnumSet .of (Message .MentionType .ROLE ))
@@ -103,6 +142,24 @@ private boolean isHelpPingForbiddenForMember(@NotNull ChannelReservation reserva
103142 );
104143 }
105144
145+ /**
146+ * Determines if a user is forbidden from sending a help-ping command due
147+ * to their status in the server.
148+ *
149+ * @param postOwnerId The posts' owner id.
150+ * @param member The member.
151+ * @param config The guild config.
152+ * @return True if the user is forbidden from sending the command.
153+ */
154+ private boolean isHelpPingForbiddenForMember (long postOwnerId , @ NotNull Member member , @ NotNull GuildConfig config ) {
155+ Set <Role > allowedRoles = Set .of (config .getModerationConfig ().getStaffRole (), config .getHelpConfig ().getHelperRole ());
156+ return !(
157+ postOwnerId == member .getUser ().getIdLong () ||
158+ member .getRoles ().stream ().anyMatch (allowedRoles ::contains ) ||
159+ member .isOwner ()
160+ );
161+ }
162+
106163 /**
107164 * Determines if the user's timeout has elapsed (or doesn't exist), which
108165 * implies that it's fine for the user to send the command.
0 commit comments