Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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.RewriteMsgCommand;
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 RewriteMsgCommand(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,153 @@
package org.togetherjava.tjbot.features.messages;

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 ChatGPT AI.
Comment thread
tj-wazei marked this conversation as resolved.
Outdated
* <p>
* The rewritten message is shown as an ephemeral message visible only to the user who triggered the
* command, making it perfect for getting quick writing improvements without cluttering the channel.
Comment thread
tj-wazei marked this conversation as resolved.
Outdated
* <p>
* Users can optionally specify a tone/style for the rewrite. If not provided, defaults to CLEAR.
Comment thread
tj-wazei marked this conversation as resolved.
Outdated
*/
public final class RewriteMsgCommand extends SlashCommandAdapter {
Comment thread
Zabuzard marked this conversation as resolved.
Outdated
private static final Logger logger = LoggerFactory.getLogger(RewriteMsgCommand.class);
Comment thread
Zabuzard marked this conversation as resolved.
Outdated
public static final String COMMAND_NAME = "rewrite-msg";
Comment thread
tj-wazei marked this conversation as resolved.
Outdated
private static final String MESSAGE_OPTION = "message";
private static final String TONE_OPTION = "tone";
private static final int MAX_MESSAGE_LENGTH = 500;
private static final int MIN_MESSAGE_LENGTH = 3;
private static final ChatGptModel CHAT_GPT_MODEL = ChatGptModel.HIGH_QUALITY;
Comment thread
tj-wazei marked this conversation as resolved.
Outdated

private final ChatGptService chatGptService;

private static String buildResponse(String userMessage, String rewrittenMessage, MsgTone tone) {
final String toneLabel = tone.displayName;
Comment thread
tj-wazei marked this conversation as resolved.
Outdated

return """
**Rewritten message (%s)**
Comment thread
Taz03 marked this conversation as resolved.
Outdated

**Original:**
%s

**Rewritten:**
Comment thread
Taz03 marked this conversation as resolved.
Outdated
%s""".formatted(toneLabel, userMessage, rewrittenMessage);
Comment thread
Taz03 marked this conversation as resolved.
Outdated
}

private static String buildChatGptPrompt(String userMessage, MsgTone tone) {
Comment thread
tj-wazei marked this conversation as resolved.
Outdated
return """
Please rewrite the following message to make it clearer, more professional, \
Comment thread
tj-wazei marked this conversation as resolved.
Outdated
and better structured. Maintain the original meaning while improving the quality \
of the writing. Do NOT use em-dashes (—). %s

If the message is already well-written, provide minor improvements.
Comment thread
Taz03 marked this conversation as resolved.
Outdated

Original message:
Comment thread
Taz03 marked this conversation as resolved.
Outdated
%s""".formatted(tone.description, userMessage);
Comment thread
Taz03 marked this conversation as resolved.
Outdated
}

/**
* Creates the slash command definition and configures available options for rewriting messages.
*
* @param chatGptService service for interacting with ChatGPT
*/
public RewriteMsgCommand(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: " + MsgTone.CLEAR.displayName
+ ")",
false);

Arrays.stream(MsgTone.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();

final MsgTone tone = parseTone(event.getOption(TONE_OPTION), event.getUser().getId());

event.deferReply(true).queue();

final Optional<String> rewrittenMessage = this.rewrite(userMessage, tone);
Comment thread
tj-wazei marked this conversation as resolved.
Outdated

if (rewrittenMessage.isEmpty()) {
logger.debug("Failed to obtain a response for /rewrite-msg, original message: '{}'",
Comment thread
tj-wazei marked this conversation as resolved.
Outdated
userMessage);
event.getHook()
.editOriginal(
"An error occurred while processing your request. Please try again later.")
.queue();
return;
}

final String response = buildResponse(userMessage, rewrittenMessage.orElseThrow(), tone);
Comment thread
tj-wazei marked this conversation as resolved.
Outdated

event.getHook().editOriginal(response).queue();
}

private MsgTone parseTone(@Nullable OptionMapping toneOption, String userId)
Comment thread
Taz03 marked this conversation as resolved.
Outdated
throws IllegalArgumentException {
if (toneOption == null) {
logger.debug("Tone option not provided for user: {}, using default CLEAR", userId);
return MsgTone.CLEAR;
}

final String toneValue = toneOption.getAsString();
final MsgTone tone = MsgTone.valueOf(toneValue);

logger.debug("Parsed tone '{}' for user: {}", tone.displayName, userId);

return tone;
}

private Optional<String> rewrite(String userMessage, MsgTone tone) {
final String rewritePrompt = buildChatGptPrompt(userMessage, tone);
Comment thread
tj-wazei marked this conversation as resolved.
Outdated

return chatGptService.ask(rewritePrompt, tone.displayName, CHAT_GPT_MODEL);
Comment thread
tj-wazei marked this conversation as resolved.
Outdated
}

private enum MsgTone {
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;

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