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 @@ -32,6 +32,7 @@
import org.togetherjava.tjbot.features.CommandVisibility;
import org.togetherjava.tjbot.features.MessageContextCommand;
import org.togetherjava.tjbot.features.chatgpt.ChatGptService;
import org.togetherjava.tjbot.features.utils.ForumPoster;
import org.togetherjava.tjbot.features.utils.StringDistances;

import java.awt.Color;
Expand Down Expand Up @@ -233,7 +234,8 @@ private RestAction<ForumPostData> createForumPost(ModalInteractionEvent event,
ForumTag tag = getTagOrDefault(questionsForum.getAvailableTagsByName(queryTag, true),
() -> questionsForum.getAvailableTagsByName(mostCommonTag, true).getFirst());

return questionsForum.createForumPost(forumTitle, forumMessage)
return ForumPoster.using(event.getJDA())
.createPost(questionsForum, forumTitle, forumMessage)
.setTags(ForumTagSnowflake.fromId(tag.getId()))
.map(createdPost -> new ForumPostData(createdPost, originalUser));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.togetherjava.tjbot.features.utils;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel;
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
import net.dv8tion.jda.api.requests.restaction.ForumPostAction;
import net.dv8tion.jda.api.requests.restaction.MessageCreateAction;
import net.dv8tion.jda.api.utils.messages.MessageCreateData;

import java.util.*;

public final class ForumPoster {
Copy link
Member

Choose a reason for hiding this comment

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

i find the name of the class confusing. for quite some time it sounded to me like its referring to a person posting in the forum, i.e. Poster being something like OP, author of a post.

but it is referring to this being an utility to post stuff in forums. i would probably have renamed it to Forums, but since the API is builder-like and wants to create instances, a more suitable name is perhaps ForumPostBuilder, also similar to JDAs MessageCreateBuilder and MessageEditBuilder that we are all used to.

or alternatively ForumPost, since its not actually a builder in the classic sense.

Copy link
Member

Choose a reason for hiding this comment

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

(public classes and methods require meaningful/useful JavaDoc in this project)

private final JDA jda;

private ForumPoster(JDA jda) {
this.jda = jda;
}

public static ForumPoster using(JDA jda) {
return new ForumPoster(jda);
}

private ForumChannel findForumChannel(String channelId) {
Copy link
Member

Choose a reason for hiding this comment

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

all these channelId and postIds in the class should probably be long instead

return Optional.ofNullable(jda.getForumChannelById(channelId))
.orElseThrow(() -> new IllegalArgumentException(
"Did not find a forum channel with ID %s while trying to create a forum post. Make sure the config is setup properly."));
}

private ThreadChannel findForumPost(String postId) {
return Optional.ofNullable(jda.getThreadChannelById(postId))
.orElseThrow(() -> new IllegalArgumentException(
"Did not find the forum post with ID %s while trying to reply to a post."));

Copy link
Member

Choose a reason for hiding this comment

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

empty line, can delete

}
Comment on lines +24 to +34
Copy link
Member

Choose a reason for hiding this comment

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

do we want this to throw if not found or return Optional instead, so the caller can deal with how to properly display error messages and the like to the user.


public MessageCreateAction sendPost(String postId, MessageCreateData messageData) {
return sendPost(findForumPost(postId), messageData);
}
Comment on lines +36 to +38
Copy link
Member

Choose a reason for hiding this comment

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

i dont quite understand this. does this reply to a given message? or is postId reffering to a channel and not a "message"?


public MessageCreateAction sendPost(ThreadChannel post, MessageCreateData messageData) {
return post.sendMessage(messageData);
}

public ForumPostAction createPost(String channelId, String title, MessageCreateData message) {
return createPost(findForumChannel(channelId), title, message);
}

public ForumPostAction createPost(ForumChannel channel, String title,
MessageCreateData message) {
return channel.createForumPost(title, message);
}
Comment on lines +40 to +51
Copy link
Member

Choose a reason for hiding this comment

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

potentially a more convenient API that also aligns more with what JDA already provides and all contributors are used to, would be to create an actual builder. so something where you can do

ForumPost.using(jda)
  .forum(channel)
  .title(title)
  .message(messageCreateData)
  .post();

given that you need at least one object coming from JDA within this chain, you could even drop the using(jda). For example, it could extract it from messageCreateData or from the channel it finds.

}