33import net .dv8tion .jda .api .EmbedBuilder ;
44import net .dv8tion .jda .api .entities .Guild ;
55import net .dv8tion .jda .api .entities .Member ;
6+ import net .dv8tion .jda .api .entities .MessageEmbed ;
67import net .dv8tion .jda .api .entities .Role ;
78import net .dv8tion .jda .api .entities .channel .ChannelType ;
89import net .dv8tion .jda .api .entities .channel .concrete .TextChannel ;
1112import net .dv8tion .jda .api .entities .emoji .EmojiUnion ;
1213import net .dv8tion .jda .api .events .interaction .command .SlashCommandInteractionEvent ;
1314import net .dv8tion .jda .api .events .interaction .component .ButtonInteractionEvent ;
15+ import net .dv8tion .jda .api .interactions .commands .OptionMapping ;
16+ import net .dv8tion .jda .api .interactions .commands .OptionType ;
1417import net .dv8tion .jda .api .interactions .commands .build .SubcommandData ;
1518import net .dv8tion .jda .api .interactions .components .buttons .Button ;
1619import net .dv8tion .jda .api .interactions .components .buttons .ButtonStyle ;
2124import net .javadiscord .javabot .data .config .guild .HelpConfig ;
2225import net .javadiscord .javabot .util .Pair ;
2326import net .javadiscord .javabot .util .Responses ;
27+ import net .javadiscord .javabot .util .StringUtils ;
28+
2429import org .jetbrains .annotations .NotNull ;
2530import xyz .dynxsty .dih4jda .interactions .commands .application .SlashCommand ;
2631import xyz .dynxsty .dih4jda .interactions .components .ButtonHandler ;
@@ -55,7 +60,8 @@ public class HelpPingSubcommand extends SlashCommand.Subcommand implements Butto
5560 * @param botConfig The main configuration of the bot
5661 */
5762 public HelpPingSubcommand (BotConfig botConfig , ScheduledExecutorService asyncPool ) {
58- setCommandData (new SubcommandData ("ping" , "Notify potential helpers that your question is urgent." ));
63+ setCommandData (new SubcommandData ("ping" , "Notify potential helpers that your question is urgent." )
64+ .addOption (OptionType .STRING , "comment" , "Optionally enter the reason you used this to be seen by helpers (e.g. 'no response')" , false ));
5965 lastPingTimes = new ConcurrentHashMap <>();
6066 this .botConfig = botConfig ;
6167 asyncPool .scheduleWithFixedDelay (this ::cleanTimeoutCache , CACHE_CLEANUP_DELAY , CACHE_CLEANUP_DELAY , TimeUnit .SECONDS );
@@ -89,32 +95,19 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
8995 Responses .warning (event , "Sorry, this command cannot be used directly after a post has been created." ).queue ();
9096 return ;
9197 }
98+
99+ String comment = event .getOption ("comment" , null , OptionMapping ::getAsString );
100+
92101 if (isHelpPingTimeoutElapsed (member .getIdLong (), config )) {
93102 lastPingTimes .put (event .getMember ().getIdLong (), new Pair <>(System .currentTimeMillis (), config .getGuild ()));
94103 TextChannel notifChannel = config .getHelpConfig ().getHelpNotificationChannel ();
95- notifChannel .sendMessageEmbeds (new EmbedBuilder ().setDescription ("""
96- %s requested help in %s
97-
98- Tags:
99- %s
100-
101- [Click to view](%s)
102- """
103- .formatted (
104- event .getUser ().getAsMention (),
105- post .getAsMention (),
106- getTagString (post ),
107- post .getJumpUrl ()
108- ))
109- .setAuthor (member .getEffectiveName (), null , member .getEffectiveAvatarUrl ())
110- .setFooter (event .getUser ().getId ())
111- .setColor (Color .YELLOW )
112- .build ())
104+
105+ notifChannel .sendMessageEmbeds (createHelpEmbed (comment , post , member ))
113106 .addActionRow (createAcknowledgementButton ())
114107 .queue ();
115108 event .reply ("""
116109 Successfully requested help.
117-
110+
118111 Note that this does NOT gurantee that anybody here has the time and knowledge to help you.
119112 Abusing this command might result in moderative action taken against you.
120113 """ )
@@ -125,23 +118,52 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
125118 }
126119 }
127120
128- private String getTagString (ThreadChannel post ) {
129- String text = post
130- .getAppliedTags ()
131- .stream ()
132- .map (this ::getForumTagText )
133- .map (tag -> "- " + tag )
134- .collect (Collectors .joining ("\n " ));
135- if (text .isEmpty ()) {
136- text = "- <no tags>" ;
121+ private MessageEmbed createHelpEmbed (String comment , ThreadChannel post , Member member ) {
122+ EmbedBuilder eb = createBasicHelpEmbedBuilder (post , member );
123+ appendComment (eb , comment );
124+ appendTags (eb , post );
125+ eb .appendDescription ("\n \n [Click to view](" +post .getJumpUrl ()+")" );
126+ return eb .build ();
127+ }
128+
129+ private EmbedBuilder createBasicHelpEmbedBuilder (ThreadChannel post , Member member ) {
130+ EmbedBuilder eb = new EmbedBuilder ()
131+ .setDescription ("%s requested help in %s"
132+ .formatted (
133+ member .getAsMention (),
134+ post .getAsMention ()
135+ ))
136+ .setAuthor (member .getEffectiveName (), null , member .getEffectiveAvatarUrl ())
137+ .setFooter (member .getId ())
138+ .setColor (Color .YELLOW );
139+ return eb ;
140+ }
141+
142+ private void appendTags (EmbedBuilder eb , ThreadChannel post ) {
143+ List <ForumTag > tags = post .getAppliedTags ();
144+ if (!tags .isEmpty ()) {
145+ String text = tags
146+ .stream ()
147+ .map (this ::getForumTagText )
148+ .map (tag -> "- " + tag )
149+ .collect (Collectors .joining ("\n " ));
150+ eb .appendDescription ("\n \n Tags:\n " )
151+ .appendDescription (text );
152+ }
153+ }
154+
155+ private void appendComment (EmbedBuilder eb , String comment ) {
156+ if (comment != null ) {
157+ eb
158+ .appendDescription ("\n \n comment:\n " )
159+ .appendDescription ("```\n " +StringUtils .standardSanitizer ().compute (comment ) + "\n ```" );
137160 }
138- return text ;
139161 }
140162
141163 private Button createAcknowledgementButton () {
142164 return Button .of (ButtonStyle .SECONDARY , ComponentIdBuilder .build ("help-ping" , "acknowledge" ), "Mark as acknowledged" );
143165 }
144-
166+
145167 private Button createUndoAcknowledgementButton () {
146168 return Button .of (ButtonStyle .SECONDARY , ComponentIdBuilder .build ("help-ping" , "unacknowledge" ), "Mark as unacknowledged" );
147169 }
@@ -202,11 +224,11 @@ public void handleButton(ButtonInteractionEvent event, Button button) {
202224 switch (id [1 ]) {
203225 case "acknowledge" ->
204226 acknowledgeChangeAction (event , true );
205- case "unacknowledge" ->
227+ case "unacknowledge" ->
206228 acknowledgeChangeAction (event , false );
207229 default -> event .reply ("Unknown button" ).setEphemeral (true ).queue ();
208230 }
209-
231+
210232 }
211233
212234 private void acknowledgeChangeAction (ButtonInteractionEvent event , boolean acknowledged ) {
@@ -224,7 +246,7 @@ private void acknowledgeChangeAction(ButtonInteractionEvent event, boolean ackno
224246 .setActionRow (acknowledged ?createUndoAcknowledgementButton ():createAcknowledgementButton ())
225247 .queue ();
226248 }
227-
249+
228250 private String getCurrentFormattedTimestamp () {
229251 return TimeFormat .RELATIVE .format (Instant .now ().toEpochMilli ());
230252 }
@@ -238,7 +260,7 @@ private String getForumTagText(ForumTag tag) {
238260 .append (" " );
239261 }
240262 sb .append (tag .getName ());
241-
263+
242264 return sb .toString ();
243265 }
244266
0 commit comments