Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.togetherjava.tjbot.features.mathcommands.wolframalpha.WolframAlphaCommand;
import org.togetherjava.tjbot.features.mediaonly.MediaOnlyChannelListener;
import org.togetherjava.tjbot.features.messages.MessageCommand;
import org.togetherjava.tjbot.features.messages.RewriteCommand;
import org.togetherjava.tjbot.features.moderation.BanCommand;
import org.togetherjava.tjbot.features.moderation.KickCommand;
import org.togetherjava.tjbot.features.moderation.ModerationActionsStore;
Expand Down Expand Up @@ -207,6 +208,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
features.add(new ChatGptCommand(chatGptService, helpSystemHelper));
features.add(new JShellCommand(jshellEval));
features.add(new MessageCommand());
features.add(new RewriteCommand(chatGptService));

FeatureBlacklist<Class<?>> blacklist = blacklistConfig.normal();
return blacklist.filterStream(features.stream(), Object::getClass).toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package org.togetherjava.tjbot.features.messages;

import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.togetherjava.tjbot.features.CommandVisibility;
import org.togetherjava.tjbot.features.SlashCommandAdapter;
import org.togetherjava.tjbot.features.chatgpt.ChatGptModel;
import org.togetherjava.tjbot.features.chatgpt.ChatGptService;

import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

/**
* The implemented command is {@code /rewrite-msg}, which allows users to have their message
* rewritten in a clearer, more professional, or better structured form using AI.
* <p>
* The rewritten message is shown as an ephemeral message visible only to the user who triggered the
* command.
* <p>
* Users can optionally specify a tone/style for the rewrite.
*/
public final class RewriteCommand extends SlashCommandAdapter {
private static final Logger logger = LoggerFactory.getLogger(RewriteCommand.class);
private static final String COMMAND_NAME = "rewrite";
private static final String MESSAGE_OPTION = "message";
private static final String TONE_OPTION = "tone";

private static final int MAX_MESSAGE_LENGTH = Message.MAX_CONTENT_LENGTH;
private static final int MIN_MESSAGE_LENGTH = 3;
private static final ChatGptModel CHAT_GPT_MODEL = ChatGptModel.FASTEST;

private final ChatGptService chatGptService;

private static String createAiPrompt(String userMessage, MessageTone tone) {
return """
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The response to the user cannot be a single message. We discussed on Discord that users on mobile cannot copy sections of an embed. This is actually also the case for regular text messages.

Rewrite the following message to make it clearer, more professional, \
and better structured. Maintain the original meaning while improving the quality \
of the writing. Do NOT use em-dashes (—). %s

IMPORTANT: The rewritten text MUST be no more than 2000 characters. \
If needed, compress wording while preserving key details and intent.

If the message is already well-written, provide minor improvements.

Original message:
%s""".stripIndent().formatted(tone.description, userMessage);
}

private static String buildOriginalMsgResponse(String userMessage, MessageTone tone) {
return """
**Original message (%s)**

%s
""".stripIndent().formatted(tone.displayName, userMessage);
}

private static String buildRewrittenMsgResponse(String aiMessage, MessageTone tone) {
return """
**Rewritten message (%s)**

%s
""".stripIndent().formatted(tone.displayName, aiMessage);
}

/**
* Creates the slash command definition and configures available options for rewriting messages.
*
* @param chatGptService service for interacting with ChatGPT
*/
public RewriteCommand(ChatGptService chatGptService) {
super(COMMAND_NAME, "Let AI rephrase and improve your message", CommandVisibility.GUILD);

this.chatGptService = chatGptService;

final OptionData messageOption =
new OptionData(OptionType.STRING, MESSAGE_OPTION, "The message you want to rewrite",
true)
.setMinLength(MIN_MESSAGE_LENGTH)
.setMaxLength(MAX_MESSAGE_LENGTH);

final OptionData toneOption = new OptionData(OptionType.STRING, TONE_OPTION,
"The tone/style for the rewritten message (default: "
+ MessageTone.CLEAR.displayName + ")",
false);

Arrays.stream(MessageTone.values())
.forEach(tone -> toneOption.addChoice(tone.displayName, tone.name()));

getData().addOptions(messageOption, toneOption);
}

@Override
public void onSlashCommand(SlashCommandInteractionEvent event) {

final String userMessage =
Objects.requireNonNull(event.getOption(MESSAGE_OPTION)).getAsString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for requireNonNull

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the getOption() return is Nullable based on its docs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but in this case its a required parameter, so it wont be null.

Also even if it was null, this is the worst way to handle it

Copy link
Member

@Zabuzard Zabuzard Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the api declares it as nullable and thats correct. but the question here is: what do you expect to get?
since u declared this parameter as required beforehand, it must not be null. if it is null, there is a severe bug in JDA and the bot should crash! (fail-fast).

so what you want is "crash if null". and for that you can either write Objects.requireNonNull(foo).bar() or simply just foo.bar(). Both are valid options to handle it and you can make an argument for either option.

we have had this discussion roughly two years back with a bigger audience and decided that Objects.requireNonNull adds too much visual clutter for this and foo.bar() is generally preferred.

but ultimately its a bit NIT.

situations where Objects.requireNonNull is really important to use though is when you do not have a situation like foo.bar() but bar(foo), as in this case a NPE wouldnt be thrown instantly. so bar(Objects.requireNonNull(foo)) would be reasonable in that situation to achieve fail-fast.

final MessageTone tone = parseTone(event.getOption(TONE_OPTION));

event.deferReply(true).queue();

Optional<String> rewrittenMessage = rewrite(userMessage, tone);

if (rewrittenMessage.isEmpty()) {
logger.debug("Failed to obtain a response for /{}, original message: '{}'",
COMMAND_NAME, userMessage);

event.getHook()
.editOriginal(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't good UX. If the user spent a fair bit of time trying to craft their message and an error occurs, they lose it. Instead, return back the original message - an embed might be useful here to add the fact there was an "error" but the user can still copy their original message and send that, should they wish.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI discord shows the original prompt on slash commands if u click on the "foo used /bar" that didcord shows in small blue text above the message

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI discord shows the original prompt on slash commands if u click on the "foo used /bar" that didcord shows in small blue text above the message

"An error occurred while processing your request. Please try again later.")
.queue();

return;
}

final String rewrittenText = rewrittenMessage.orElseThrow();

logger.debug("Rewrite successful; rewritten message length: {}", rewrittenText.length());

event.getHook()
.sendMessage(buildOriginalMsgResponse(userMessage, tone))
.setEphemeral(true)
.queue();

event.getHook()
.sendMessage(buildRewrittenMsgResponse(rewrittenText, tone))
.setEphemeral(true)
.queue();
}

private MessageTone parseTone(@Nullable OptionMapping toneOption)
throws IllegalArgumentException {

if (toneOption == null) {
logger.debug("Tone option not provided, using default '{}'", MessageTone.CLEAR.name());
return MessageTone.CLEAR;
}

final String toneValue = toneOption.getAsString();

return MessageTone.valueOf(toneValue);
}

private Optional<String> rewrite(String userMessage, MessageTone tone) {

final String rewritePrompt = createAiPrompt(userMessage, tone);

Optional<String> attempt =
chatGptService.ask(rewritePrompt, tone.displayName, CHAT_GPT_MODEL);

if (attempt.isEmpty()) {
return attempt;
}

final String response = attempt.get();

if (response.length() <= Message.MAX_CONTENT_LENGTH) {
return attempt;
}

logger.debug("Rewritten message exceeded {} characters; retrying with stricter constraint",
Message.MAX_CONTENT_LENGTH);

final String shortenPrompt = rewritePrompt
+ "\n\nConstraint reminder: Your previous rewrite exceeded "
+ Message.MAX_CONTENT_LENGTH
+ " characters. Provide a revised rewrite strictly under "
+ Message.MAX_CONTENT_LENGTH + " characters while preserving meaning and tone.";

return chatGptService.ask(shortenPrompt, tone.displayName, CHAT_GPT_MODEL);
}

private enum MessageTone {
CLEAR("Clear", "Make it clear and easy to understand."),
PRO("Pro", "Use a professional and polished tone."),
DETAILED("Detailed", "Expand with more detail and explanation."),
TECHNICAL("Technical", "Use technical and specialized language where appropriate.");

private final String displayName;
private final String description;

MessageTone(String displayName, String description) {
this.displayName = displayName;
this.description = description;
}
}
}
Loading