From 454f1aa3b4704f7e70adf9641cbc2672c158e61b Mon Sep 17 00:00:00 2001 From: Taz <73871477+Tanish-Azad@users.noreply.github.com> Date: Thu, 21 Jul 2022 18:11:17 +0530 Subject: [PATCH] Auto create threads for suggestions (#469) * Auto create threads for suggestions * added a condition to general checks * made a method for thread creation * added notnull annotation to message argument in createThread method * removed unnecessary checks * changed algorithm to generate thread titles * removed unnecessary variable --- .../commands/basic/SuggestionsUpDownVoter.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java b/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java index e3d7b5b29a..280f32599e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java @@ -22,6 +22,7 @@ */ public final class SuggestionsUpDownVoter extends MessageReceiverAdapter { private static final Logger logger = LoggerFactory.getLogger(SuggestionsUpDownVoter.class); + private static final int TITLE_MAX_LENGTH = 60; private static final String FALLBACK_UP_VOTE = "👍"; private static final String FALLBACK_DOWN_VOTE = "👎"; @@ -47,10 +48,27 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) { Guild guild = event.getGuild(); Message message = event.getMessage(); + createThread(message); reactWith(config.getUpVoteEmoteName(), FALLBACK_UP_VOTE, guild, message); reactWith(config.getDownVoteEmoteName(), FALLBACK_DOWN_VOTE, guild, message); } + private static void createThread(@NotNull Message message) { + String title = message.getContentRaw(); + + if (title.length() >= TITLE_MAX_LENGTH) { + int lastWordEnd = title.lastIndexOf(' ', TITLE_MAX_LENGTH); + + if (lastWordEnd == -1) { + lastWordEnd = TITLE_MAX_LENGTH; + } + + title = title.substring(0, lastWordEnd); + } + + message.createThreadChannel(title).queue(); + } + private static void reactWith(@NotNull String emoteName, @NotNull String fallbackUnicodeEmote, @NotNull Guild guild, @NotNull Message message) { getEmoteByName(emoteName, guild).map(message::addReaction).orElseGet(() -> {