Skip to content
Merged
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 @@ -43,8 +43,14 @@ public final class GitHubReference extends MessageReceiverAdapter {
*/
static final Pattern ISSUE_REFERENCE_PATTERN =
Pattern.compile("#(?<%s>\\d{1,5})".formatted(ID_GROUP));
private static final int ISSUE_OPEN = Color.green.getRGB();
private static final int ISSUE_CLOSE = Color.red.getRGB();

// Representing different GitHub states of an Issue/PR
private static final Color OPEN_STATE = Color.green;
private static final Color CLOSE_STATE = Color.red;
private static final Color MERGED_STATE = new Color(141, 106, 187);
private static final Color NOT_PLANNED_STATE = new Color(72, 72, 72);
private static final Color DRAFT_STATE = Color.gray;


/**
* A constant representing the date and time formatter used for formatting the creation date of
Expand Down Expand Up @@ -167,9 +173,7 @@ MessageEmbed generateReply(GHIssue issue) throws UncheckedIOException {
String dateOfCreation = FORMATTER.format(createdAt);

String footer = "%s • %s • %s".formatted(labels, assignees, dateOfCreation);

return new EmbedBuilder()
.setColor(issue.getState() == GHIssueState.OPEN ? ISSUE_OPEN : ISSUE_CLOSE)
return new EmbedBuilder().setColor(getIssueStateColor(issue))
.setTitle(title, titleUrl)
.setDescription(description)
.setAuthor(issue.getUser().getName(), null, issue.getUser().getAvatarUrl())
Expand All @@ -181,6 +185,26 @@ MessageEmbed generateReply(GHIssue issue) throws UncheckedIOException {
}
}

/**
* Returns the color based on the state of the issue/PR
*/
private Color getIssueStateColor(GHIssue issue) throws IOException {
if (issue instanceof GHPullRequest pr) {
if (pr.isMerged()) {
return MERGED_STATE;
} else if (pr.isDraft()) {
return DRAFT_STATE;
}
} else {
if (issue.getStateReason() == GHIssueStateReason.COMPLETED) {
return MERGED_STATE;
} else if (issue.getStateReason() == GHIssueStateReason.NOT_PLANNED) {
return NOT_PLANNED_STATE;
}
}
return issue.getState() == GHIssueState.OPEN ? OPEN_STATE : CLOSE_STATE;
}

/**
* Either properly gathers the name of a user or throws a UncheckedIOException.
*/
Expand All @@ -199,6 +223,9 @@ Optional<GHIssue> findIssue(int id, String targetIssueTitle) {
return repositories.stream().map(repository -> {
try {
GHIssue issue = repository.getIssue(id);
if (issue.isPullRequest()) {
issue = repository.getPullRequest(id);
}
if (issue.getTitle().equals(targetIssueTitle)) {
return Optional.of(issue);
}
Expand All @@ -216,7 +243,11 @@ Optional<GHIssue> findIssue(int id, long defaultRepoId) {
.filter(repository -> repository.getId() == defaultRepoId)
.map(repository -> {
try {
return Optional.of(repository.getIssue(id));
GHIssue issue = repository.getIssue(id);
if (issue.isPullRequest()) {
issue = repository.getPullRequest(id);
}
return Optional.of(issue);
} catch (FileNotFoundException ignored) {
return Optional.<GHIssue>empty();
} catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.channel.Channel;
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
import net.dv8tion.jda.api.entities.channel.forums.ForumTag;
import net.dv8tion.jda.api.events.channel.ChannelCreateEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.requests.RestAction;
import org.jetbrains.annotations.NotNull;

import org.togetherjava.tjbot.features.EventReceiver;
import org.togetherjava.tjbot.features.UserInteractionType;
Expand Down Expand Up @@ -56,20 +58,18 @@ public HelpThreadCreatedListener(HelpSystemHelper helper) {
}

@Override
public void onChannelCreate(ChannelCreateEvent createEvent) {
if (!createEvent.getChannelType().isThread()) {
return;
}
ThreadChannel threadChannel = createEvent.getChannel().asThreadChannel();

if (wasThreadAlreadyHandled(threadChannel.getIdLong())) {
return;
}

if (!helper.isHelpForumName(threadChannel.getParentChannel().getName())) {
return;
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
if (event.isFromThread()) {
Channel parentChannel = event.getChannel().asThreadChannel().getParentChannel();
if (helper.isHelpForumName(parentChannel.getName())) {
ThreadChannel threadChannel = event.getChannel().asThreadChannel();
int messageCount = threadChannel.getMessageCount();
if (messageCount > 1 || wasThreadAlreadyHandled(threadChannel.getIdLong())) {
return;
}
handleHelpThreadCreated(threadChannel);
}
}
handleHelpThreadCreated(threadChannel);
}

private boolean wasThreadAlreadyHandled(long threadChannelId) {
Expand All @@ -82,23 +82,10 @@ private boolean wasThreadAlreadyHandled(long threadChannelId) {
}

private void handleHelpThreadCreated(ThreadChannel threadChannel) {
threadChannel.retrieveMessageById(threadChannel.getIdLong()).queue(message -> {

long authorId = threadChannel.getOwnerIdLong();

if (isPostedBySelfUser(message)) {
// When transfer-command is used
authorId = getMentionedAuthorByMessage(message).getIdLong();
}

helper.writeHelpThreadToDatabase(authorId, threadChannel);
});

// The creation is delayed, because otherwise it could be too fast and be executed
// after Discord created the thread, but before Discord send OPs initial message.
// Sending messages at that moment is not allowed.
createMessages(threadChannel).and(pinOriginalQuestion(threadChannel))
.queueAfter(5, TimeUnit.SECONDS);
threadChannel.retrieveStartMessage().flatMap(message -> {
registerThreadDataInDB(message, threadChannel);
return generateAutomatedResponse(threadChannel);
}).flatMap(message -> pinOriginalQuestion(threadChannel)).queue();
}

private static User getMentionedAuthorByMessage(Message message) {
Expand Down Expand Up @@ -126,7 +113,7 @@ private RestAction<Void> pinOriginalQuestion(ThreadChannel threadChannel) {
return threadChannel.retrieveMessageById(threadChannel.getIdLong()).flatMap(Message::pin);
}

private RestAction<Message> createMessages(ThreadChannel threadChannel) {
private RestAction<Message> generateAutomatedResponse(ThreadChannel threadChannel) {
return sendHelperHeadsUp(threadChannel).flatMap(any -> createAIResponse(threadChannel));
}

Expand Down Expand Up @@ -228,4 +215,15 @@ private void handleDismiss(Member interactionUser, ThreadChannel channel,
}
deleteMessages.queue();
}

private void registerThreadDataInDB(Message message, ThreadChannel threadChannel) {
long authorId = threadChannel.getOwnerIdLong();

if (isPostedBySelfUser(message)) {
// When transfer-command is used
authorId = getMentionedAuthorByMessage(message).getIdLong();
}

helper.writeHelpThreadToDatabase(authorId, threadChannel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.Interaction;
import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
Expand All @@ -16,7 +15,6 @@
import javax.annotation.CheckReturnValue;

import java.awt.*;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
Expand Down Expand Up @@ -77,10 +75,9 @@ private static ReplyCallbackAction handleWhoIsUser(final IReplyCallback event, f
+ userFlagsToStringItem(user.getFlags()) + "\n**Registration date:** "
+ DATE_TIME_FORMAT.format(user.getTimeCreated());

EmbedBuilder embedBuilder =
generateEmbedBuilder(event, user, profile, profile.getAccentColor()).setAuthor(
user.getName(), user.getEffectiveAvatarUrl(), user.getEffectiveAvatarUrl())
.setDescription(description);
EmbedBuilder embedBuilder = generateEmbedBuilder(user, profile, profile.getAccentColor())
.setAuthor(user.getName(), user.getEffectiveAvatarUrl(), user.getEffectiveAvatarUrl())
.setDescription(description);

return sendEmbedWithProfileAction(event, embedBuilder.build(), user.getId());
}
Expand All @@ -100,7 +97,7 @@ private static ReplyCallbackAction handleWhoIsMember(final IReplyCallback event,
+ DATE_TIME_FORMAT.format(user.getTimeCreated()) + "\n**Roles:** "
+ formatRoles(member);

EmbedBuilder embedBuilder = generateEmbedBuilder(event, user, profile, effectiveColor)
EmbedBuilder embedBuilder = generateEmbedBuilder(user, profile, effectiveColor)
.setAuthor(member.getEffectiveName(), member.getEffectiveAvatarUrl(),
member.getEffectiveAvatarUrl())
.setDescription(description);
Expand Down Expand Up @@ -129,20 +126,15 @@ private static String voiceStateToStringItem(final Member member) {
/**
* Generates whois embed based on the given parameters.
*
* @param event the {@link SlashCommandInteractionEvent}
* @param user the {@link User} getting whois'd
* @param profile the {@link net.dv8tion.jda.api.entities.User.Profile} of the whois'd user
* @param effectiveColor the {@link Color} that the embed will become
* @return the generated {@link EmbedBuilder}
*/
private static EmbedBuilder generateEmbedBuilder(final Interaction event, final User user,
final User.Profile profile, final Color effectiveColor) {

private static EmbedBuilder generateEmbedBuilder(final User user, final User.Profile profile,
final Color effectiveColor) {
EmbedBuilder embedBuilder = new EmbedBuilder().setThumbnail(user.getEffectiveAvatarUrl())
.setColor(effectiveColor)
.setFooter("Requested by " + event.getUser().getName(),
event.getMember().getEffectiveAvatarUrl())
.setTimestamp(Instant.now());
.setColor(effectiveColor);

if (null != profile.getBannerId()) {
embedBuilder.setImage(profile.getBannerUrl() + "?size=" + USER_PROFILE_PICTURE_SIZE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import javax.annotation.Nullable;

import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.temporal.TemporalAccessor;
import java.util.*;
import java.util.function.BiConsumer;
Expand Down Expand Up @@ -108,8 +107,6 @@ private static void sendSuccessMessage(IReplyCallback event, String id, String a
event
.replyEmbeds(new EmbedBuilder().setTitle("Success")
.setDescription("Successfully %s tag '%s'.".formatted(actionVerb, id))
.setFooter(event.getUser().getName())
.setTimestamp(Instant.now())
.setColor(TagSystem.AMBIENT_COLOR)
.build())
.queue();
Expand Down