-
-
Notifications
You must be signed in to change notification settings - Fork 104
Update to ChatGPTcommand.java #818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a72b5db
Update ChatGPTCommand.java to include character limiting and rate lim…
11f3e8a
Requested changes to ChatGptCommand.java:
cf43e51
Requested changes to ChatGptCommand.java:
b223792
Add new catch for RuntimeException to ChatGptService.java in order to…
103868b
Requested changes to time calculation for cooldown in onSlashCommand(…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
62 changes: 55 additions & 7 deletions
62
application/src/main/java/org/togetherjava/tjbot/features/chaptgpt/ChatGptCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,85 @@ | ||
| package org.togetherjava.tjbot.features.chaptgpt; | ||
|
|
||
| import com.github.benmanes.caffeine.cache.Cache; | ||
| import com.github.benmanes.caffeine.cache.Caffeine; | ||
| import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; | ||
| import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; | ||
| import net.dv8tion.jda.api.interactions.commands.OptionType; | ||
| import net.dv8tion.jda.api.interactions.components.Modal; | ||
| import net.dv8tion.jda.api.interactions.components.text.TextInput; | ||
| import net.dv8tion.jda.api.interactions.components.text.TextInputStyle; | ||
|
|
||
| import org.togetherjava.tjbot.features.CommandVisibility; | ||
| import org.togetherjava.tjbot.features.SlashCommandAdapter; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * The implemented command is {@code /chatgpt}, which allows users to ask ChatGPT a question, upon | ||
| * which it will respond with an AI generated answer. | ||
| */ | ||
| public final class ChatGptCommand extends SlashCommandAdapter { | ||
| private static final String QUESTION_OPTION = "question"; | ||
| private static final String QUESTION_INPUT = "question"; | ||
| private static final int MAX_MESSAGE_INPUT_LENGTH = 200; | ||
| private static final int MIN_MESSAGE_INPUT_LENGTH = 4; | ||
| private static final Duration COMMAND_COOLDOWN = Duration.of(10, ChronoUnit.SECONDS); | ||
| private final ChatGptService chatGptService; | ||
|
|
||
| private final Cache<String, Instant> userIdToAskedAtCache = | ||
| Caffeine.newBuilder().maximumSize(1_000).expireAfterWrite(COMMAND_COOLDOWN).build(); | ||
|
|
||
| /** | ||
| * Creates an instance of the chatgpt command. | ||
| * | ||
| * | ||
| * @param chatGptService ChatGptService - Needed to make calls to ChatGPT API | ||
| */ | ||
| public ChatGptCommand(ChatGptService chatGptService) { | ||
| super("chatgpt", "Ask the ChatGPT AI a question!", CommandVisibility.GUILD); | ||
|
|
||
| this.chatGptService = chatGptService; | ||
|
|
||
| getData().addOption(OptionType.STRING, QUESTION_OPTION, "What do you want to ask?", true); | ||
| } | ||
|
|
||
| @Override | ||
| public void onSlashCommand(SlashCommandInteractionEvent event) { | ||
| Instant previousAskTime = userIdToAskedAtCache.getIfPresent(event.getMember().getId()); | ||
| if (previousAskTime != null) { | ||
| long timeRemainingUntilNextAsk = COMMAND_COOLDOWN.getSeconds() | ||
| - Duration.between(previousAskTime, Instant.now()).get(ChronoUnit.SECONDS); | ||
|
|
||
| event | ||
| .reply("Sorry, you need to wait another " + timeRemainingUntilNextAsk | ||
| + " second(s) before asking chatGPT another question.") | ||
| .setEphemeral(true) | ||
| .queue(); | ||
| return; | ||
| } | ||
|
|
||
| TextInput body = TextInput | ||
| .create(QUESTION_INPUT, "Ask ChatGPT a question or get help with code", | ||
| TextInputStyle.PARAGRAPH) | ||
| .setPlaceholder("Put your question for ChatGPT here") | ||
| .setRequiredRange(MIN_MESSAGE_INPUT_LENGTH, MAX_MESSAGE_INPUT_LENGTH) | ||
| .build(); | ||
|
|
||
| Modal modal = Modal.create(generateComponentId(), "ChatGPT").addActionRow(body).build(); | ||
| event.replyModal(modal).queue(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onModalSubmitted(ModalInteractionEvent event, List<String> args) { | ||
| event.deferReply().queue(); | ||
| String response = chatGptService.ask(event.getOption(QUESTION_OPTION).getAsString()) | ||
| .orElse("An error has occurred while trying to communication with ChatGPT. Please try again later"); | ||
|
|
||
| Optional<String> optional = | ||
| chatGptService.ask(event.getValue(QUESTION_INPUT).getAsString()); | ||
| if (optional.isPresent()) { | ||
| userIdToAskedAtCache.put(event.getMember().getId(), Instant.now()); | ||
| } | ||
|
|
||
| String response = optional.orElse( | ||
| "An error has occurred while trying to communicate with ChatGPT. Please try again later"); | ||
| event.getHook().sendMessage(response).queue(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.